Python比较两个长度不同的列表

时间:2017-07-19 04:05:40

标签: python list loops

您好我想比较两个不同长度的列表,并打印一个已排序的表,其中包含每个表中缺少的项目。我部分能够完成此操作并打印list_2中缺少的值。但是我无法在list_2中打印list_1中缺少的值,即字母“z”。如何执行此操作以获得下面的所需输出?

list_1 = ['a', 'b', 'c', 'd', 'e', 'f']
list_2 = ['b', 'c', 'f', 'z']

table_format = '{:<10} {:<10}'
print(table_format.format('list_1', 'list_2'))
print('-' * 20)
for x in list_1:
    for y in list_2:
        if x in y:
            print(table_format.format(x, y))
            break
    else:
        print(table_format.format(x,'Missing'))

当前输出:

list_1     list_2    
--------------------
a          Missing   
b          b         
c          c         
d          Missing   
e          Missing   
f          f        

期望的输出:

list_1     list_2    
--------------------
a          Missing   
b          b         
c          c         
d          Missing   
e          Missing   
f          f    
Missing    z

3 个答案:

答案 0 :(得分:2)

一种解决方案可能是使用包含两个原始列表的所有元素的第三个列表。然后我们可以对新列表进行排序,并在迭代它时,我们可以检查原始列表中第三个列表的元素是否存在。实际上将第三个列表设置为一组会更好。根据{{​​1}}的建议,我们应该在迭代之前将原始列表转换为集合。因此,该过程将更有效。为什么?关注这篇文章。 Which is faster and why? Set or List?

Patrick Haugh

输出:

list_1 = set(['a', 'b', 'c', 'd', 'e', 'f'])  # Or list_1 = {'a', 'b', 'c', 'd', 'e', 'f'}
list_2 = set(['b', 'c', 'f', 'z'])  # list_2 = {'b', 'c', 'f', 'z'}

list_3 = set(list_1 | list_2)
table_format = '{:<10} {:<10}'
print(table_format.format('list_1', 'list_2'))
print('-' * 20)
for elem in sorted(list_3):
    if elem in list_1:
        if elem in list_2:
            print(table_format.format(elem, elem))
        else:
            print(table_format.format(elem, 'Missing'))
    else:
        print(table_format.format('Missing', elem))

答案 1 :(得分:1)

使用OrderedDict似乎可以胜任:

from collections import OrderedDict

list_1 = ['a', 'b', 'c', 'd', 'e', 'f']
list_2 = ['b', 'c', 'f', 'z']

mapping = OrderedDict()
for x in list_1:
    mapping[x] = x if x in list_2 else 'Missing'

for x in list_2:
    mapping[x] = x if x in list_1 else 'Missing'

table_format = '{:<10} {:<10}'
print(table_format.format('list_1', 'list_2'))
print('-' * 20)

for k in mapping:
    if k in list_1:
        print(table_format.format(k, mapping[k]))
    else:
        print(table_format.format(mapping[k], k))

输出:

list_1     list_2    
--------------------
a          Missing   
b          b         
c          c         
d          Missing   
e          Missing   
f          f         
Missing    z      

答案 2 :(得分:0)

你可以使用列表理解来做同样的事情!

>>> print "\n".join(map(str,['list_1\tlist_2\n---------------']+[(each if each in list_1 else 'missing')+'\t'+(each if each in list_2 else 'missing') for each in sorted(set(list_1+list_2))]))
list_1  list_2
---------------
a       missing
b       b
c       c
d       missing
e       missing
f       f
missing z

我已经打破了理解,以便更好地理解!

>>> [each if each in list_1 else 'missing' for each in sorted(set(list_1+list_2))]
['a', 'b', 'c', 'd', 'e', 'f', 'missing']

>>> [each if each in list_2 else 'missing' for each in sorted(set(list_1+list_2))]
['missing', 'b', 'c', 'missing', 'missing', 'f', 'z']

>>> [(each if each in list_1 else 'missing',each if each in list_2 else 'missing') for each in sorted(set(list_1+list_2))]
[('a', 'missing'), ('b', 'b'), ('c', 'c'), ('d', 'missing'), ('e', 'missing'), ('f', 'f'), ('missing', 'z')]

>>> [['list_1','list_2']]+[(each if each in list_1 else 'missing',each if each in list_2 else 'missing') for each in sorted(set(list_1+list_2))]
[['list_1', 'list_2'], ('a', 'missing'), ('b', 'b'), ('c', 'c'), ('d', 'missing'), ('e', 'missing'), ('f', 'f'), ('missing', 'z')]

>>> print "\n".join(map(str,[['list_1','list_2']]+[(each if each in list_1 else 'missing',each if each in list_2 else 'missing') for each in sorted(set(list_1+list_2))]))
['list_1', 'list_2']
('a', 'missing')
('b', 'b')
('c', 'c')
('d', 'missing')
('e', 'missing')
('f', 'f')
('missing', 'z')

>>> print "\n".join(map(str,['list_1\tlist_2\n---------------']+[(each if each in list_1 else 'missing')+'\t'+(each if each in list_2 else 'missing') for each in sorted(set(list_1+list_2))]))
list_1  list_2
---------------
a       missing
b       b
c       c
d       missing
e       missing
f       f
missing z