I have an input list that look like this
input_list = ["a", "b2","d"]
and another list that look like this
ref_list = [['a'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3', 'c4'], ['d']]
What I want to do is to get the list of value from input_list that not existed in ref_list So, the result based on these values should be
[['c1', 'c2', 'c3', 'c4']]
At first, my case only have something like
input_list = ["a","b","d"]
ref_list = ["a","b","c","d"]
which I can use
missing_value = list(set(ref_list) - set(input_list))
which will extract a result like this
["c"]
but for the case that a ref_list that each index is a list with single or multiple value in it. Is there a simple way to achieve the missing value?
CodePudding user response:
You can use all
for this with a nested list comprehension
input_list = ["a", "b2","d"]
ref_list = [['a'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3', 'c4'], ['d']]
res = [i for i in ref_list if all(j not in i for j in input_list)]
print(res)
Output
[['c1', 'c2', 'c3', 'c4']]
CodePudding user response:
It quit simple and I can not think of any efficient way to solve your problem.
input_list = ['a', 'b2','d']
ref_list = [['a'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3', 'c4'], ['d']]
required_list = []
for data in ref_list:
if len(set(data) & set(input_list)) == 0:
required_list.append(data)
print(required_list)
Output:
[['c1', 'c2', 'c3', 'c4']]
Also you can refer here for more clarity. Thanks!
CodePudding user response:
Looks like a job for set.isdisjoint
:
input_list = ["a", "b2","d"]
ref_list = [['a'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3', 'c4'], ['d']]
res = list(filter(set(input_list).isdisjoint, ref_list))
print(res)
Output (Try it online!):
[['c1', 'c2', 'c3', 'c4']]
Benchmark with your small data:
2.6 μs 2.6 μs 2.7 μs python_user
2.2 μs 2.2 μs 2.2 μs Uttam_Velani
0.9 μs 0.9 μs 0.9 μs dont_talk_just_code
Benchmark with a 1000 times longer ref_list
:
2150 μs 2184 μs 2187 μs python_user
1964 μs 1981 μs 1990 μs Uttam_Velani
292 μs 304 μs 306 μs dont_talk_just_code
Benchmark code (Try it online!):
from timeit import repeat
def python_user(input_list, ref_list):
return [i for i in ref_list if all(j not in i for j in input_list)]
def Uttam_Velani(input_list, ref_list):
required_list = []
for data in ref_list:
if len(set(data) & set(input_list)) == 0:
required_list.append(data)
return required_list
def dont_talk_just_code(input_list, ref_list):
return list(filter(set(input_list).isdisjoint, ref_list))
funcs = python_user, Uttam_Velani, dont_talk_just_code
def test(input_list, ref_list, number, format_time):
expect = funcs[0](input_list, ref_list)
for func in funcs:
result = func(input_list, ref_list)
print(result == expect, func.__name__)
print()
for _ in range(3):
for func in funcs:
times = sorted(repeat(lambda: func(input_list, ref_list), number=number))[:3]
print(*(format_time(t / number) for t in times), func.__name__)
print()
test(["a", "b2","d"],
[['a'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3', 'c4'], ['d']],
10000,
lambda time: '%4.1f μs ' % (time * 1e6))
test(["a", "b2","d"],
[['a'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3', 'c4'], ['d']] * 1000,
20,
lambda time: 'M μs ' % (time * 1e6))