答案 0 :(得分:0)
试试这个:
goalList = ['' for x in range(len(bigList)]
for elem in bigList:
if sequenceA in bigList[elem]:
goalList[elem] = sequenceA
if sequenceB in bigList[elem]:
goalList[elem] = sequenceB
if sequenceA in bigList[elem]:
goalList[elem] = sequenceC
当然,让它适应您在数据库中拥有的东西。
答案 1 :(得分:0)
你可以用一个简单的双循环来做到这一点:
sequences = ['sequenceA', 'sequenceB', 'sequenceC']
bigList = ['Garbage sequenceB blahblah', 'sequenceA nonsense', 'silliness sequenceC', 'total nonsense']
goalList = []
for element in bigList:
for seq in sequences:
if seq in element:
break
goalList.append(seq if seq in element else "")
print(goalList)
# prints: ['sequenceB', 'sequenceA', 'sequenceC', '']
答案 2 :(得分:0)
如果你喜欢神奇的单行。
>>>[ ''.join([x if x in y else '' for x in sequences]) for y in bigList]
['sequenceB', 'sequenceA', 'sequenceC', '']