我想追加所有列表(通过用户多次点击获得),最后得到他们的交集。让我举一个例子:
我正在创建一个空列表,并附加我点击按钮后会得到的所有列表。
First button click- got a list from database table [1, 6, 7, 8]
As the first button is clicked so after appending it in empty list it should give you the same list [[1, 6, 7, 8]] because there is a only one list to add.
and i am getting [[1, 6, 7, 8]]
second button click- got a list from database [1, 2, 5, 6, 7, 8]
Now second button is clicked so i should get [[1, 6, 7, 8],[1, 2, 5, 6, 7, 8]]
but what i am getting is [[1, 2, 5, 6, 7, 8]]
Third button click- got a list [2, 4, 7, 8]
after clicking third button [[1, 6, 7, 8],[1, 2, 5, 6, 7, 8],[2, 4, 7, 8]]
but i am getting [[2, 4, 7, 8]]
User can select n number of buttons.
Output after applying intersection function [7,8]
def OnClick(self, event):
name = event.GetEventObject().GetLabelText()
cursor= self.conn.execute("SELECT * FROM ELEMENT where SYMBOL==?", (name,))
elements = cursor.fetchall()
print elements
cursor= self.conn.execute("SELECT ATOMIC_NUMBER FROM ELEMENT where SYMBOL = ?", (name,))
numbers = cursor.fetchone()[0]
print numbers
atomicnumber = numbers
cursor= self.conn.execute("SELECT MOL_NUMBER FROM LINK where ELEMENT_NUMBER = ?", (atomicnumber,))
mnumbers = cursor.fetchall()
print mnumbers
mnum_list = []
for i in mnumbers:
mnum_list.append(i[0])
print mnum_list
my_global_list = list()
global inter_list
inter_list.append(mnum_list)
print inter_list
print list(set.intersection(*map(set,inter_list)))
可能问题在于,我在onclick事件中创建了mnum_list。因此,只要单击一个按钮,它就会创建一个新的空列表,并仅将该特定列表附加到其中。
答案 0 :(得分:1)
附加它们然后使用bitwise AND operator
" &
"跨越多个列表以获得交集。
>>> a = [1, 6, 7, 8]
>>> b = [1, 2, 5, 6, 7, 8]
>>> c = [2, 4, 7, 8]
>>> d= []
>>> d.append(a)
>>> d.append(b)
>>> d.append(c)
>>> intersection = set(a) & set(b) & set(c)
交叉点不会按原样排序,但我会将该部分留作练习。