如何拆分列表的元素?

我有个清单:

my_list = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847']

我怎样才能删除 \t和之后的一切得到这个结果:

['element1', 'element2', 'element3']
813346 次浏览
myList = [i.split('\t')[0] for i in myList]

Something like:

>>> l = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847']
>>> [i.split('\t', 1)[0] for i in l]
['element1', 'element2', 'element3']

Try iterating through each element of the list, then splitting it at the tab character and adding it to a new list.

for i in list:
newList.append(i.split('\t')[0])

Do not use list as variable name. You can take a look at the following code too:

clist = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847', 'element5']
clist = [x[:x.index('\t')] if '\t' in x else x for x in clist]

Or in-place editing:

for i,x in enumerate(clist):
if '\t' in x:
clist[i] = x[:x.index('\t')]

I had to split a list for feature extraction in two parts lt,lc:

ltexts = ((df4.ix[0:,[3,7]]).values).tolist()
random.shuffle(ltexts)


featsets = [(act_features((lt)),lc)
for lc, lt in ltexts]


def act_features(atext):
features = {}
for word in nltk.word_tokenize(atext):
features['cont({})'.format(word.lower())]=True
return features

Solution with map and lambda expression:

my_list = list(map(lambda x: x.split('\t')[0], my_list))