add*_*tes 5 python python-3.x jupyter-notebook
我在python中创建了一个函数来计算单词的长度,而忽略标点符号。例如,如果我有一个句子:"Haven't there been 3 blackouts today?结果应该是以下内容:[6,5,4,1,9,5]我可以使用我创建的以下函数获得这些结果:
import string
def word_length_list(given_string):
clean_string = given_string.translate(str.maketrans('','',string.punctuation))
word_lenght_list = list(map(len, clean_string.split()))
return word_lenght_list
given_string = "Haven't there been 3 blackouts today?"
word_length_list(given_string)
Run Code Online (Sandbox Code Playgroud)
此函数给我的预期结果是:[6, 5, 4, 1, 9, 5]
但是,如果给它一个很长的字符串,例如:
given_string = "Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function."
Run Code Online (Sandbox Code Playgroud)
它通过以下方式给我结果:
[7,
3,
6,
2,
3,
15,
3,
6,
2,
3,
15,
3,
6,
2,
3,
15,
3,
6,
2,
3,
15,
3,
6,
2,
3,
15,
3,
6,
2,
3,
15,
3,
6,
2,
3,
15,
3,
6,
2,
3,
15,
3,
6,
2,
3,
15,
3,
6,
2,
3,
15,
3,
6,
2,
3,
15,
3,
6,
2,
3,
15,
3,
6,
2,
3,
15,
3,
6,
2,
3,
15,
3,
6,
2,
3,
15,
3,
6,
2,
3,
15,
3,
6,
2,
3,
15,
3,
6,
2,
3,
15,
3,
6,
2,
3,
8]
Run Code Online (Sandbox Code Playgroud)
有人对我如何使我的列表水平返回短字符串和长字符串的结果有任何想法吗?任何建议将不胜感激。
我正在使用jupyter notebook作为.ipynb运行代码
| 归档时间: |
|
| 查看次数: |
1445 次 |
| 最近记录: |