Get first word of all strings in lists

二次信任 提交于 2019-12-02 11:01:16

You can use a list comprehension , and split() function :

>>> l=['diffuse systemic sclerosis', 'back', 'public on july 15 2008']
>>> [i.split()[0] for i in l]
['diffuse', 'back', 'public']

You can use comprehension

>>> l = [['diffuse systemic sclerosis', 'back', 'public on july 15 2008']
,['diffuse systemic sclerosis', 'forearm', 'public on may 9 2014']]

>>> list({i.split()[0] for j in l for i in j})
['back', 'diffuse', 'forearm', 'public']
l = [
    ['diffuse systemic sclerosis', 'back', 'public on july 15 2008'],
    ['diffuse systemic sclerosis', 'forearm', 'public on may 9 2014']
    ]
d = lambda o: [a.split().pop(0) for a in o]
r = lambda a,b: d(a) + d(b)
print "\n".join(set(reduce(r, l)))
>>> 
public
forearm
diffuse
back

You can use str.split in a list comprehension, noting that you can specify maxsplit to reduce the number of operations:

L = ['diffuse systemic sclerosis', 'back', 'public on july 15 2008']

res = [i.split(maxsplit=1)[0] for i in L]
# ['diffuse', 'back', 'public']

You can also perform the same operations functionally:

from operator import itemgetter, methodcaller

splitter = methodcaller('split', maxsplit=1)
res = list(map(itemgetter(0), map(splitter, L)))

Across multiple lists, if you wish to maintain the order with which you observe unique first words, you can use the itertool unique_everseen recipe, also found in the more_itertools library:

from itertools import chain
from more_itertool import unique_everseen

L1 = ['diffuse systemic sclerosis', 'back', 'public on july 15 2008']
L2 = ['diffuse systemic sclerosis', 'forearm', 'public on may 9 2014']

res = list(unique_everseen(i.split(maxsplit=1)[0] for i in chain(L1, L2)))

# ['diffuse', 'back', 'public', 'forearm']
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!