比较列表中的两个列表,如果列表a中的列表b index [x]做某事

时间:2019-03-02 12:06:25

标签: python list

我有两个这样的列表(例如):

a = [['1','2'],['3','4'],['5','6','7']]
b = [['1'],['3','4'],['5','6'],['7']]

If b in a : #in this case is(['3','4']) 
    print 'same'
    #stop
else:
    print 'its different'

我已经在我的代码中这样做了:

for x in b:
    if x in a:
        print 'same', x
        break
    else:
        print 'its different', x

我生成大约100个列表,当结果不同时(否则),该过程将继续循环,直到b变长。

我只希望结果相同,请停止打印,并在结果不同时打印并继续检查下一个列表。

当我中断if和else语句,或者只是中断else语句时,它会影响结果的结尾

编辑

100个列表

a = [['1','2'],['3','4'],['5','6','7']]
b = [['1'],['3','4'],['5','6'],['7']]
c = [['5','6','7'],['2']]
d = [['3'],['8']]
e = [[...],[...]]
azz = [[...],[...]]
for x in b:
    if x in a:
        print 'same ==', x
        #break
    else:
        print 'its different', x
        #break

结果

different b ==  ['1']
same b == ['3', '4']
different b ==  ['5', '6']
different b ==  ['7']

继续

same c ==  ['5', '6', '7']
different c ==  ['2']

继续

different d ==  ['3']
different d ==  ['8']

继续列出100

我想要这样的结果:

same b == ['3', '4']
#the result stop because its same and ignored the different result

继续

same c ==  ['5', '6', '7']
#the result stop because its same and ignored the different result

继续

different d ==  ['3']
#the result stop BECAUSE ITS DIFFERENT AND IGNORED THE SAME RESULT

继续列出100

1 个答案:

答案 0 :(得分:0)

您应该有一个列表列表,例如:

Lists = [b, c, d, ...]

并且:

for item in Lists:

    flag = 0
    for x in item:
        if x in a:
            flag = 1
            print ('same ==', x)
            break

    if flag == 0:
        print('different ==', item[0])