Python Str.split问题,需要找到字符串中每个组件的长度
Python Str.split问题,需要找到字符串中每个组件的长度,python,Python,str_list=[“你好”,“再见”,“好极了”,“我爱Python”] 我需要通过str.split打印出字符串每个组成部分的长度。如果有人能帮忙,我将不胜感激 [print(len(text)) for text in str_list] 产量是, 5 0 7 9 13 [None, None, None, None, None] 你可以这样使用列表理解 [print(len(text)) for text in str_list] 产量是, 5 0 7 9 13 [None, No
str_list=[“你好”,“再见”,“好极了”,“我爱Python”]
我需要通过
str.split
打印出字符串每个组成部分的长度。如果有人能帮忙,我将不胜感激
[print(len(text)) for text in str_list]
产量是,
5
0
7
9
13
[None, None, None, None, None]
你可以这样使用列表理解
[print(len(text)) for text in str_list]
产量是,
5
0
7
9
13
[None, None, None, None, None]
在将句子拆分为单词/短语(您已经这样做了)后,只需在其中循环:
for i in str_list:
print(len(i))
在将句子拆分为单词/短语(您已经这样做了)后,只需在其中循环:
for i in str_list:
print(len(i))
如果要获得每个字符串中每个单词的长度,可以执行以下操作:
str_list = ["hello", "", "goodbye", "wonderful", "I love Python"]
lengths = [[len(word) for word in my_string.split()] for my_string in str_list]
print(str_list)
print(lengths)
结果:
['hello', '', 'goodbye', 'wonderful', 'I love Python']
[[5], [], [7], [9], [1, 4, 6]]
如果要获得每个字符串中每个单词的长度,可以执行以下操作:
str_list = ["hello", "", "goodbye", "wonderful", "I love Python"]
lengths = [[len(word) for word in my_string.split()] for my_string in str_list]
print(str_list)
print(lengths)
结果:
['hello', '', 'goodbye', 'wonderful', 'I love Python']
[[5], [], [7], [9], [1, 4, 6]]
您需要字符串中的字符数还是字数?我需要它打印出每个组件中的字符数,即5,0,7等。您需要字符串中的字符数还是字数?我需要它打印出每个组件中的字符数,即5,0,7等。