输入一段英文串,去除其中的逗号和句号,再切成单词列表. 
时间: 2023-05-11 21:04:18 浏览: 20
Sure, here's the code to achieve that:
```python
import re
text = "This is a sample text, with some commas and periods. Let's remove them!"
text = re.sub(r'[,.]', '', text) # remove commas and periods
words = text.split() # split into word list
print(words)
```
This code uses regular expressions to remove commas and periods from the input text, and then splits the resulting string into a list of words.
相关问题
用python语言输入一个字符串,除去其中四位逗号和句号,在切成单词列表。用replace()和split()方法
可以这样实现:
```python
string = "Hello, world! This is a test, string."
string = string.replace(",", "").replace(".", "") # 去除逗号和句号
word_list = string.split() # 切成单词列表
print(word_list)
```
输出结果为:
```
['Hello', 'world', 'This', 'is', 'a', 'test', 'string']
```
输入一个用逗号分隔的字符串,统计其中出现次数最多的字符
可以使用Python中的字典来实现统计字符出现次数的功能。具体步骤如下:
1. 将输入的字符串按照逗号分隔成一个列表。
2. 遍历列表中的每个字符,使用字典来统计每个字符出现的次数。
3. 找出字典中出现次数最多的字符。
下面是示例代码:
```python
s = input("请输入一个用逗号分隔的字符串:")
lst = s.split(",") # 将字符串按逗号分隔成列表
count = {} # 定义一个空字典来统计字符出现次数
for c in lst:
if c in count:
count[c] += 1
else:
count[c] = 1
max_count = # 记录出现次数最多的字符的出现次数
max_char = "" # 记录出现次数最多的字符
for c in count:
if count[c] > max_count:
max_count = count[c]
max_char = c
print("出现次数最多的字符是:", max_char)
```
运行示例:
```
请输入一个用逗号分隔的字符串:a,b,c,a,a,d,e,e,e
出现次数最多的字符是: a
```
相关推荐
















