1
我在python中創建了一個函數來計算單詞的長度,而忽略了標點符號。例如,如果我有一句話:"Haven't there been 3 blackouts today?
結果應該是以下幾點:[6,5,4,1,9,5]
我能夠得到這些結果與下面的函數,我創建:在python 3中水平顯示列表結果而不是垂直
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)
此功能給了我預期的結果是:[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."
它給我的結果通過以下方式:
[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]
有沒有人有任何想法,我怎麼可以讓我的列表水平返回結果爲短字符串和長字符串?任何建議將不勝感激。
使用IDE? IPython的? –
無法重現:輸出對我來說很好。 – Prune
只是一個拼寫更正:word_lenght_list - > word_length_list –