Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
连接python列表中的公共项_Python - Fatal编程技术网

连接python列表中的公共项

连接python列表中的公共项,python,Python,我有两份清单: list1 = ['a','d'] list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')] 我想追加列表1或创建一个新列表,以产生: list1 = ['a','d'] list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')] [('a', '1'), ('d', '4')] 我尝试过使用索引,但运气不好 这是一个解决方案 list1 = ['a','d'] l

我有两份清单:

list1 = ['a','d']
list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]
我想追加列表1或创建一个新列表,以产生:

list1 = ['a','d']
list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]
[('a', '1'), ('d', '4')]
我尝试过使用索引,但运气不好

这是一个解决方案

list1 = ['a','d']
list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]
list1 = ['a','d'] 
list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]
my_set = set(list1)
new_list = [(x, y) for x, y in list2 if x in my_set]

如果您将列表
list2
转换为dict,您就可以更轻松地执行所需操作

list1 = ['a','d']
list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]
>>> list1 = ['a','d']; list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')] 
>>> dict2 = dict(list2)
>>> list3 = [(e, dict2[e]) for e in list1]
>>> 
>>> list3
[('a', '1'), ('d', '4')]

从键、值列表创建字典
list2

list1 = ['a','d']
list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]
>>> list1 = ['a', 'd']
>>> list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]
>>> d2 = dict(list2)
格言如下:

list1 = ['a','d']
list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]
>>> d2
{'a': '1', 'c': '3', 'd': '4', 'b': '2'}
>>> d2['c']
'3'
获取列表1中键的值

list1 = ['a','d']
list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]
>>> [(k, d2[k]) for k in list1]
[('a', '1'), ('d', '4')]
此外,您可能更喜欢数字数据,而不是编码为字符串的数字。在本例中,使用创建字典

list1 = ['a','d']
list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]
>>> d2 = dict((k, int(v)) for k,v in list2)
>>> d2
{'a': 1, 'c': 3, 'd': 4, 'b': 2}
您可以使用筛选感兴趣的元素:

list1 = ['a','d']
list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]
list1 = ['a','d']
list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]

# operating on a set makes the lookups O(1) which is faster then O(n) for lists.
# List with few elements (4-5) are still better then a set() due to the overhead
# a set introduces
whatIwant = set(list1)

# python 2.x filter returns a list directly
list3 = filter(lambda x: x[0] in whatIwant,list2) 


print(list3)
输出:

list1 = ['a','d']
list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]
[('a', '1'), ('d', '4')]
filter(function,iterable)
对任何
iterable
进行操作,并对每个元素应用该函数,以确定iterable的元素是否应在输出中(
function(item)==True
)或不在输出中(
function(item)==False

list1 = ['a','d']
list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]


对于python3:
filter(…)
将返回一个迭代器,因此您需要将结果填充到
列表(…)
中以获得返回的列表。

您只需使用列表理解即可实现这一点:

list1 = ['a','d']
list2 = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')]
list3 = [t for t in list2 if t[0] in list1]

这个答案和拉胡戈斯瓦米的答案一样,那又怎样?几秒钟后我点击了“post”,解释了发生的事情。是什么让这个答案对你“没用”呢?对不起,但当我发表评论时,答案和原来的完全一样。我猜你在进一步的编辑中添加了解释。我刚刚投了更高的票。干杯!:)@RahulGoswami没问题,很高兴我们能提供帮助:)它确实有几个限制,即
list2
中没有重复的键,
list1
中的所有内容都存在于
list2
中,但目前尚不清楚这是否对OP.FYI重要:您获得了否决票/否决票,因为您没有展示您尝试过的内容以及它如何不起作用。。。这是个骗局,我找不到。类似于“在其他列表中逐个元素从列表中选择元素”——通常是一个列表和一个需要的索引列表,在这种情况下,虽然相似,但略有不同。这个“问题”可以通过在列表2中为el添加一个简单的
并将其附加到结果列表中来解决。注意,您创建
集(列表1)
的次数与调用lambda函数的次数相同。我建议在过滤之前计算一次。如果您使用python3,您需要包装
list()
,因为
filter()
会返回一个迭代器。另外,最好将
set(list1)
放在循环之外,这样您就不会在每次迭代中创建新的集合。@RoadRunner观察得很好!我将编辑我的答案。谢谢。@AChampion true,答案的版本更好。非常感谢!工作正常。