在嵌套列表中查找匹配元素

时间:2018-05-10 07:35:45

标签: python python-3.x list search

我有一个这样的嵌套列表:

lst = [['one two', 'three', '10'], ['spam eggs', 'spam', '8'],
       ['two three', 'four', '5'], ['foo bar', 'foo', '7'],
       ['three four', 'five', '9']] 

最后一个元素是一种概率。 我需要的是找到元素,其中一个元素的第二个和第三个字匹配另一个元素的第一个和第二个字,例如:

['one two', 'three', '10'] match ['two three', 'four', '5'] match  ['three four', 'five', '9']

制作链条,如:

one two 10 three 5 four 9 five

我知道第一步必须是元素的设计:

lst = ([' '.join(x).split() for x in lst])
for i in lst: 
    print(i)

所以我得到

['one', 'two', 'three', '10']
['spam', 'eggs', 'spam', '8']
['two', 'three', 'four', '4']
['foo', 'bar', 'foo', '7']
['three', 'four', 'five', '9']

下一步应该是对列表中每个元素进行某种迭代搜索,但是我对这种搜索的Python实现有点困惑。 任何帮助,将不胜感激。

3 个答案:

答案 0 :(得分:1)

我建议以下列方式使用pandas:

import pandas as pd

lst = [['one two', 'three', '10'], ['spam eggs', 'spam', '8'],
   ['two three', 'four', '5'], ['foo bar', 'foo', '7'],
   ['three four', 'five', '9']]

lst = [' '.join(x).split() for x in lst]

#Create a dataframe and merge using the adequate columns

df = pd.DataFrame(lst)
matchedDF = df.merge(df,how='inner',left_on=[1,2],right_on=[0,1],suffixes=['left','right'])

# remove unneccessary columns
cols=matchedDF.columns.tolist()

matchedDF = matchedDF[cols[2:]]

print(matchedDF)

我明白了:

    0left  1left  2left 3left 0right 1right 2right 3right
0   one    two  three    10    two  three   four      5
1   two  three   four     5  three   four   five      9

答案 1 :(得分:1)

这也有效:

lst = [['one two', 'three', '10'],['spam eggs', 'spam', '8'], ['two three', 'four', '5'], ['foo bar', 'foo', '7'], ['three four', 'five', '9']] 
lst = ([' '.join(x).split() for x in lst])

match, first = [], True
for i in lst:
    for j in lst:
        if i[0] == j[1] and i[1] == j[2]:
            if first:
                match.append(j)
                first = False
            match.append(i)

for i in match:
    if i == match[len(match)-1]: print(i)
    else: print ("{} match ".format(i), end=' ')

for i in match:
    if i == match[0]: print (i[0], i[1], i[3], end=' ')
    elif i == match[len(match)-1]: print (i[1], i[3], i[2])
    else: print (i[1], i[3], end=' ')

第一个for i in match循环输出:

['one', 'two', 'three', '10'] match  ['two', 'three', 'four', '5'] match ['three', 'four', 'five', '9']

第二个:

one two 10 three 5 four 9 five

答案 2 :(得分:0)

试试这个: lst = [[' one two',' three',' 10'],['垃圾邮件鸡蛋''垃圾邮件&# 39;,' 8'],        ['两个三','四'' 5'],[' foo bar',' foo', ' 7&#39],        ['三四','五',' 9']] lst = [' ' .join(x).split()for x in lst] 我在lst:     打印(I) #------------------------------------------------- -------------- st = set() for in [set(x)for x in lst]:     st | = i 打印(ST) 打印(名单(ST)) 输出: [' one',' two',' three',' 10'] ['垃圾邮件''鸡蛋','垃圾邮件'' 8'] [' two',' three',' four',' 5'] [' foo',' bar',' foo',' 7'] ['三','四','五',' 9'] {' bar','垃圾邮件',' 9',' one',' five','三个','两个'' 8','四',' 5',' foo', ' 10',' 7','鸡蛋'} [' bar','垃圾邮件',' 9',' one',' five','三个','两个'' 8','四',' 5',' foo', ' 10',' 7','鸡蛋']