简体   繁体   English

Python:比较列表和元组列表

[英]Python: Compare a list and list of tuples

I have a list as shown below: 我有一个列表如下所示:

z = [('Anna Smith', 'IN1'), ('John', 'IN2'), ('Matt Andrew', 'IN3'), ('Smith', 'IN4')]

And another list: 另一个清单:

c = ['Anna Smith', 'John', 'Anna', 'Smith']

I want the below output: 我想要以下输出:

o = ['Anna Smith|IN1', 'John|IN2', 'Smith|IN4']

I have tried the below code: 我试过以下代码:

for s, s_inc in z:
     for word in c:
          if word.lower() in s.lower():
                o.append("%s|%s"%(word, s_inc))

But the above gives me the output: 但上面给出了输出:

o = ['Anna Smith|IN1', 'Anna|IN1', 'Smith|IN1', 'John|IN2', 'Smith|IN4']

How do I get what I want? 我怎样才能得到我想要的东西?

List comprehension is an elegant method for this type of filtering/list manipulation problems. 列表理解是这种类型的过滤/列表操作问题的优雅方法。

The comprehension consists of three parts: 理解包括三个部分:

-First the the result is constructed in a+'|'+b - 首先,结果是在+'|'+ b中构造的

-Secondly, a and b are assigned to the first and second element in each 2-tuple in list z - 其次,a和b被分配给列表z中的每个2元组中的第一和第二元素

-Thirdly we filter on the condition that a must be a member of list c - 第三,我们过滤了必须是列表c的成员的条件

print [a+'|'+b for a,b in z if a in c]

# Prints ['Anna Smith|IN1', 'John|IN2', 'Smith|IN4']

From your examples, it seems you are looking for an exact match , so just use == instead of in : 从你的例子,似乎你正在寻找一个精确匹配 ,因此就使用==而不是in

for s, s_inc in z:
     for word in c:
          if word == s:
                o.append("%s|%s"%(word, s_inc))

Or shorter, as a single list comprehension: 或者更短,作为单个列表理解:

o = ["%s|%s"%(s, s_inc) for s, s_inc in z if s in c]

After this, o is ['Anna Smith|IN1', 'John|IN2', 'Smith|IN4'] 在此之后, o['Anna Smith|IN1', 'John|IN2', 'Smith|IN4']

Try this: 尝试这个:

z = [('Anna Smith', 'IN1'), ('John', 'IN2'), ('Matt Andrew', 'IN3'), ('Smith', 'IN4')]
c = set(['Anna Smith', 'John', 'Anna', 'Smith'])

o = [
    '|'.join([name, code]) for name, code in z if name in c   
]

I'd make c a set , for fast constant-time testing: 我会做c 一套 ,用于快速固定时间的测试:

c_set = {w.lower() for w in c}

I lower-cased the words to make it easy to test for membership case-insensitively. 我对这些单词进行了较低的设置,以便在不区分大小写的情况下轻松测试成员资格。

Then just use: 然后使用:

for s, s_inc in z:
    if s.lower() in c_set:
        o.append('|'.join([s, s_inc]))

or even: 甚至:

o = ['|'.join([s, s_inc]) for s, s_inc in z if s.lower() in c_set]

to produce the whole list with a list comprehension. 用列表理解产生整个列表。

Demo: 演示:

>>> z = [('Anna Smith', 'IN1'), ('John', 'IN2'), ('Matt Andrew', 'IN3'), ('Smith', 'IN4')]
>>> c = ['Anna Smith', 'John', 'Anna', 'Smith']
>>> c_set = {w.lower() for w in c}
>>> ['|'.join([s, s_inc]) for s, s_inc in z if s.lower() in c_set]
['Anna Smith|IN1', 'John|IN2', 'Smith|IN4']
>>> z = [('Anna Smith', 'IN1'), ('John', 'IN2'), ('Matt Andrew', 'IN3'), ('Smith', 'IN4')]
>>> c = ['Anna Smith', 'John', 'Anna', 'Smith']
>>> ['|'.join([i,x[1]]) for x in z for i in c if x[0]==i]
['Anna Smith|IN1', 'John|IN2', 'Smith|IN4']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM