Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有办法在字符串列表中打印选定的单词?_Python_Python 3.x - Fatal编程技术网

Python 有没有办法在字符串列表中打印选定的单词?

Python 有没有办法在字符串列表中打印选定的单词?,python,python-3.x,Python,Python 3.x,我很难找到一种方法来只打印字符串列表中的对象(汽车、雨伞、汽车、海滩、笔记本) 另外,是否有方法从字符串列表(汽车、雨伞、海滩、笔记本)打印独特元素 使用拆分: elements = [ 'cars, 1010, 1420', 'umbrellas, 1700, 1820', 'cars, 4010, 1220', 'beaches, 1800, 1120', 'notebooks, 0610, 0420'] print([string.split(',')[0] for string in

我很难找到一种方法来只打印字符串列表中的对象(汽车、雨伞、汽车、海滩、笔记本)

另外,是否有方法从字符串列表(汽车、雨伞、海滩、笔记本)打印独特元素


使用
拆分

elements = [
'cars, 1010, 1420',
'umbrellas, 1700, 1820',
'cars, 4010, 1220',
'beaches, 1800, 1120',
'notebooks, 0610, 0420']

print([string.split(',')[0] for string in elements])
# ['cars', 'umbrellas', 'cars', 'beaches', 'notebooks']
如果您想要唯一的名称,只需使用集合压缩而不是列表:

print({string.split(',')[0] for string in elements})
# {'cars', 'notebooks', 'umbrellas', 'beaches'}
或与:


使用
拆分

elements = [
'cars, 1010, 1420',
'umbrellas, 1700, 1820',
'cars, 4010, 1220',
'beaches, 1800, 1120',
'notebooks, 0610, 0420']

print([string.split(',')[0] for string in elements])
# ['cars', 'umbrellas', 'cars', 'beaches', 'notebooks']
如果您想要唯一的名称,只需使用集合压缩而不是列表:

print({string.split(',')[0] for string in elements})
# {'cars', 'notebooks', 'umbrellas', 'beaches'}
或与: