简体   繁体   English

两个列表中的公共元素python

[英]common elements in two lists python

I have two python lists generated from two different database tables 我有两个从两个不同的数据库表生成的python列表

list1= ["'HZ1398043','HZ1395693','HZ1395532','HZ1395240','HZ1395194','HZ1395113','HZ1395036','HZ1395236','HZ1396139','HZ1398028','HZ1395098','HZ1395998','HZ1395018','HZ1395829','HZ1398031','HZ1395708','HZ1398029','HZ1398030','HZ1398054''"]

list2= ['', '', '', '', '', 'HZ1395018', 'HZ1395036', 'HZ1395098', 'HZ1395113', 'HZ1395194', 'HZ1395236', 'HZ1395240', 'HZ1395532', 'HZ1395693', 'HZ1395708', 'HZ1395829', 'HZ1395998', 'HZ1396139', 'HZ1398028', 'HZ1398029', 'HZ1398031', 'HZ1398043', 'HZ1397932', 'HZ1397949', 'HZ1398004', 'HZ1398021', 'HZ1398030', 'HZ1397940', 'HZ1397941', 'HZ1398010',', '', '']

I need to find common elements between the two 我需要找到两者之间的共同点

set(list1) & set(list2) 

doesn't display anything 什么都不显示

even [i for i in list1 if i in list2] doesn't display anything. 即使[i for i in list1 if i in list2]也不会显示[i for i in list1 if i in list2] I can clearly see HZ1398043 is common. 我可以清楚地看到HZ1398043是常见的。

Look closely, your first list is actually a list with one item, a big string. 仔细观察,您的第一个列表实际上是一个包含一个大字符串的项目的列表。

>>> list1= ["'HZ1398043','HZ1395693','HZ1395532','HZ1395240','HZ1395194','HZ1395113','HZ1395036','HZ1395236','HZ1396139','HZ1398028','HZ1395098','HZ1395998','HZ1395018','HZ1395829','HZ1398031','HZ1395708','HZ1398029','HZ1398030','HZ1398054''"]
>>> len(list1)
1

Ideally, fix wherever you are getting the data to give you the right thing, if that's not possible, then you will need to parse the data. 理想情况下,修复获取数据的位置以提供正确的东西,如果不可能的话,则需要解析数据。

You will want to do something like list1 = [item.strip("'") for item in list1[0].split(",")] to get the actual list (a simple list comprehension ), then use one of your methods (the set method is the most efficient, although if you wish the keep duplicates and order, you will need to do the second method, although you can improve it by making a set of list2 beforehand to check for membership in). 您将要对list1 = [item.strip("'") for item in list1[0].split(",")]执行类似于list1 = [item.strip("'") for item in list1[0].split(",")]以获取实际列表(一个简单的列表推导 ),然后使用其中一个方法(set方法是最有效的,尽管如果您希望保留重复项和顺序,则您将需要执行第二种方法,尽管您可以通过预先创建一组list2来检查其成员资格来改进它)。

def compare(list1,list2):
ln= []
for i in list1:
    if i  in list2:
       ln.append(i)
return ln

print(compare(list1,list2)) 打印(比较(列表1,列表2))

not optimise but easy to understand. 没有优化,但易于理解。

First you need to make a proper list out of list1, this can be done with something like: 首先,您需要从list1中创建一个正确的列表,这可以通过以下方法完成:

list1 = [item.strip("'") for item in list1[0].split(",")]

Then your code should work just fine. 然后您的代码应该可以正常工作。 An alternative (more compact but slower) way to find common elements would be: 查找常见元素的另一种方法(更紧凑但更慢)是:

common = filter(lambda x:x in list1,list2)

This is what they should be: 应该是:

list1= ['HZ1398043','HZ1395693','HZ1395532','HZ1395240','HZ1395194','HZ1395113','HZ1395036','HZ1395236','HZ1396139','HZ1398028','HZ1395098','HZ1395998','HZ1395018','HZ1395829','HZ1398031','HZ1395708','HZ1398029','HZ1398030','HZ1398054']
list2= ['', '', '', '', '', 'HZ1395018', 'HZ1395036', 'HZ1395098', 'HZ1395113', 'HZ1395194', 'HZ1395236', 'HZ1395240', 'HZ1395532', 'HZ1395693', 'HZ1395708', 'HZ1395829', 'HZ1395998', 'HZ1396139', 'HZ1398028', 'HZ1398029', 'HZ1398031', 'HZ1398043', 'HZ1397932', 'HZ1397949', 'HZ1398004', 'HZ1398021', 'HZ1398030', 'HZ1397940', 'HZ1397941', 'HZ1398010','', '', '']

Your code works after you fix them. 修复它们后,代码即可工作。

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

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