对于列表或(元组)中的每个数字列表或(元组),从这个主要列表和 return 中获得差异或将所有这些列表存储到变量
For each numbers list or (tuple) from Lists or (Tuples) get a difference from this main one list and return or store all those Lists to variable
我的需求是从主列表中获取许多列表的列表差异或反转(NOT(反向)),并将这些反转列表保存到变量以打印它们以查看输出。
仅供参考,我知道如何区分两个列表,但不知道如何区分多个列表。
两个列表之间差异的示例代码:
a = [1,2,3,4,5,6,7,8,9,10]
b = [2,4,6,8,10]
c = [number for number in a if number not in b]
print(c) # OUTPUT: [1, 3, 5, 7, 9]
我从数据库中的 table 中获取所有列表,请参见下面的两行代码:
t_numbers = cur.execute("SELECT n1, n2, n3, n4, n5 FROM Numbers")
all_list_of_numbers = [num for num in t_numbers]
main_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
all_list_of_numbers = [(1, 2, 3, 4, 5), (1, 2, 3, 5, 9), (2, 4, 7, 8, 10), (3, 5, 6, 8, 10),
(4, 5, 6, 8, 9), (1, 3, 6, 7, 10), (2, 3, 6, 8, 10), (5, 7, 8, 9 ,10),
(1, 2, 4, 6, 9), (1, 2, 3, 8, 9), (1, 5, 6, 8, 9), (2, 3, 4, 5, 7),
(1, 3, 5, 7, 10), (2, 4, 5, 6, 8), (3, 5, 8, 9, 10), (2, 3, 4, 7, 9),
(3, 4, 7, 9, 10), (3, 4, 6, 7, 8), (5, 6, 7, 8, 10), (1, 4, 6, 7, 9)]
我正在编写一个函数,但它完全没有按照我想要的方式运行 return 我的列表。
也许我不需要函数,也许使用列表理解或其他更好的东西?
def Diff(main_list, all_list_numbers):
return list(set(main_list) - set(all_list_numbers)) + list(set(all_list_numbers) - set(main_list))
我还有一个函数可以检查并从 Numbers table:
中获取五个数字的总数
def get_total_numbers(allfiveNumbers):
count = 0
for numbers in allfiveNumbers:
count += 1
# print("All the number in the list: ", get_total_numbers(all_list_numbers))
# OUTPUT -- All five numbers lists is: 20
这是一种方法:
main_set = set(main_list)
res = [main_set.symmetric_difference(i) for i in all_list_of_numbers]
输出:
[{6, 7, 8, 9, 10},
{4, 6, 7, 8, 10},
{1, 3, 5, 6, 9},
{1, 2, 4, 7, 9},
{1, 2, 3, 7, 10},
{2, 4, 5, 8, 9},
{1, 4, 5, 7, 9},
{1, 2, 3, 4, 6},
{3, 5, 7, 8, 10},
{4, 5, 6, 7, 10},
{2, 3, 4, 7, 10},
{1, 6, 8, 9, 10},
{2, 4, 6, 8, 9},
{1, 3, 7, 9, 10},
{1, 2, 4, 6, 7},
{1, 5, 6, 8, 10},
{1, 2, 5, 6, 8},
{1, 2, 5, 9, 10},
{1, 2, 3, 4, 9},
{2, 3, 5, 8, 10}]
我的需求是从主列表中获取许多列表的列表差异或反转(NOT(反向)),并将这些反转列表保存到变量以打印它们以查看输出。
仅供参考,我知道如何区分两个列表,但不知道如何区分多个列表。 两个列表之间差异的示例代码:
a = [1,2,3,4,5,6,7,8,9,10]
b = [2,4,6,8,10]
c = [number for number in a if number not in b]
print(c) # OUTPUT: [1, 3, 5, 7, 9]
我从数据库中的 table 中获取所有列表,请参见下面的两行代码:
t_numbers = cur.execute("SELECT n1, n2, n3, n4, n5 FROM Numbers")
all_list_of_numbers = [num for num in t_numbers]
main_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
all_list_of_numbers = [(1, 2, 3, 4, 5), (1, 2, 3, 5, 9), (2, 4, 7, 8, 10), (3, 5, 6, 8, 10),
(4, 5, 6, 8, 9), (1, 3, 6, 7, 10), (2, 3, 6, 8, 10), (5, 7, 8, 9 ,10),
(1, 2, 4, 6, 9), (1, 2, 3, 8, 9), (1, 5, 6, 8, 9), (2, 3, 4, 5, 7),
(1, 3, 5, 7, 10), (2, 4, 5, 6, 8), (3, 5, 8, 9, 10), (2, 3, 4, 7, 9),
(3, 4, 7, 9, 10), (3, 4, 6, 7, 8), (5, 6, 7, 8, 10), (1, 4, 6, 7, 9)]
我正在编写一个函数,但它完全没有按照我想要的方式运行 return 我的列表。 也许我不需要函数,也许使用列表理解或其他更好的东西?
def Diff(main_list, all_list_numbers):
return list(set(main_list) - set(all_list_numbers)) + list(set(all_list_numbers) - set(main_list))
我还有一个函数可以检查并从 Numbers table:
中获取五个数字的总数def get_total_numbers(allfiveNumbers):
count = 0
for numbers in allfiveNumbers:
count += 1
# print("All the number in the list: ", get_total_numbers(all_list_numbers))
# OUTPUT -- All five numbers lists is: 20
这是一种方法:
main_set = set(main_list)
res = [main_set.symmetric_difference(i) for i in all_list_of_numbers]
输出:
[{6, 7, 8, 9, 10},
{4, 6, 7, 8, 10},
{1, 3, 5, 6, 9},
{1, 2, 4, 7, 9},
{1, 2, 3, 7, 10},
{2, 4, 5, 8, 9},
{1, 4, 5, 7, 9},
{1, 2, 3, 4, 6},
{3, 5, 7, 8, 10},
{4, 5, 6, 7, 10},
{2, 3, 4, 7, 10},
{1, 6, 8, 9, 10},
{2, 4, 6, 8, 9},
{1, 3, 7, 9, 10},
{1, 2, 4, 6, 7},
{1, 5, 6, 8, 10},
{1, 2, 5, 6, 8},
{1, 2, 5, 9, 10},
{1, 2, 3, 4, 9},
{2, 3, 5, 8, 10}]