比较两个元组列表并寻找不同的值
Comparing two lists of tuples and looking for different values
我有两个元组列表。例如:
我保留的第一个:
[
("base", "first_val", "value 1", "1"),
("base", "first_val", "value 2", "0"),
("base", "first_val", "value 3", "2"),
("base", "second_val", "value 1", "10"),
("base", "second_val", "value 2", "10"),
("base", "third_val", "value 1", "100"),
("base", "third_val", "value 2", "1"),
("base", "fourth_val", "value 1", "param 1", "22"),
("base", "fourth_val", "value 2", "param 1", "222"),
("base", "fourth_val", "value 3", "12")
]
10 个元组,4 个带子参数的参数。
我得到的第二个。此列表可能还有其他内容:
[
("base", "first_val", "value 1", "10000"), #changed
("base", "first_val", "value 2", "5555"), #changed
("base", "first_val", "value 3", "2"), #not changed
("base", "fourth_val", "value 1", "param 1", "22"), #not changed
("base", "fourth_val", "value 2", "param 1", "100000"), #changed
("base", "fourth_val", "value 3", "12") #not changed
]
6 个元组,2 个带子参数的参数。
事实上,这些表格有数百个条目。
用元组填充结果列表是不断变化的,但保留了构建元组的一般原则。如何以最快的方式只获取那些发生变化的元组?
您可以在 Python 中使用 set
来确定更改的值:
first_list = [("base", "first_val", "value 1", "1"),
("base", "first_val", "value 2", "0"),
("base", "first_val", "value 3", "2"),
("base", "second_val", "value 1", "10"),
("base", "second_val", "value 2", "10"),
("base", "third_val", "value 1", "100"),
("base", "third_val", "value 2", "1"),
("base", "fourth_val", "value 1", "param 1", "22"),
("base", "fourth_val", "value 2", "param 1", "222"),
("base", "fourth_val", "value 3", "12")]
second_list = [
("base", "first_val", "value 1", "10000"), #changed
("base", "first_val", "value 2", "5555"), #changed
("base", "first_val", "value 3", "2"), #not changed
("base", "fourth_val", "value 1", "param 1", "22"), #not changed
("base", "fourth_val", "value 2", "param 1", "100000"), #changed
("base", "fourth_val", "value 3", "12") #not changed
]
changed = list(set(second_list) - set(first_list))
print(changed)
这输出:
[('base', 'fourth_val', 'value 2', 'param 1', '100000'),
('base', 'first_val', 'value 1', '10000'),
('base', 'first_val', 'value 2', '5555')]
我有两个元组列表。例如:
我保留的第一个:
[
("base", "first_val", "value 1", "1"),
("base", "first_val", "value 2", "0"),
("base", "first_val", "value 3", "2"),
("base", "second_val", "value 1", "10"),
("base", "second_val", "value 2", "10"),
("base", "third_val", "value 1", "100"),
("base", "third_val", "value 2", "1"),
("base", "fourth_val", "value 1", "param 1", "22"),
("base", "fourth_val", "value 2", "param 1", "222"),
("base", "fourth_val", "value 3", "12")
]
10 个元组,4 个带子参数的参数。
我得到的第二个。此列表可能还有其他内容:
[
("base", "first_val", "value 1", "10000"), #changed
("base", "first_val", "value 2", "5555"), #changed
("base", "first_val", "value 3", "2"), #not changed
("base", "fourth_val", "value 1", "param 1", "22"), #not changed
("base", "fourth_val", "value 2", "param 1", "100000"), #changed
("base", "fourth_val", "value 3", "12") #not changed
]
6 个元组,2 个带子参数的参数。
事实上,这些表格有数百个条目。
用元组填充结果列表是不断变化的,但保留了构建元组的一般原则。如何以最快的方式只获取那些发生变化的元组?
您可以在 Python 中使用 set
来确定更改的值:
first_list = [("base", "first_val", "value 1", "1"),
("base", "first_val", "value 2", "0"),
("base", "first_val", "value 3", "2"),
("base", "second_val", "value 1", "10"),
("base", "second_val", "value 2", "10"),
("base", "third_val", "value 1", "100"),
("base", "third_val", "value 2", "1"),
("base", "fourth_val", "value 1", "param 1", "22"),
("base", "fourth_val", "value 2", "param 1", "222"),
("base", "fourth_val", "value 3", "12")]
second_list = [
("base", "first_val", "value 1", "10000"), #changed
("base", "first_val", "value 2", "5555"), #changed
("base", "first_val", "value 3", "2"), #not changed
("base", "fourth_val", "value 1", "param 1", "22"), #not changed
("base", "fourth_val", "value 2", "param 1", "100000"), #changed
("base", "fourth_val", "value 3", "12") #not changed
]
changed = list(set(second_list) - set(first_list))
print(changed)
这输出:
[('base', 'fourth_val', 'value 2', 'param 1', '100000'),
('base', 'first_val', 'value 1', '10000'),
('base', 'first_val', 'value 2', '5555')]