Adi*_*ikh 2 python tuples list
我有一个元组列表,如下所示。
x = [('b', 'c'),
('c',),
('a', 'c', 'b'),
('b', 'c', 'a', 'd'),
('b', 'c', 'a'),
('a', 'b'),
('a', 'b', 'c', 'd'),
('a', 'c', 'b', 'd'),
('b',),
('c', 'a'),
('a', 'b', 'c'),
('a',)]
Run Code Online (Sandbox Code Playgroud)
我想给出像 ('a') 这样的输入,那么它应该给出像这样的输出,
[('a', 'c', 'b'), ('a', 'b'),('a', 'b', 'c', 'd'),('a', 'c', 'b', 'd'),('a', 'b', 'c')]
#everything starts with a. But not "a".
Run Code Online (Sandbox Code Playgroud)
或者对于 ('a','b') 的输入,它应该给出一个输出
[('a', 'b', 'c', 'd'),('a', 'b', 'c')]
#everything start with ('a','b') but not ('a','b') itself.
Run Code Online (Sandbox Code Playgroud)
我尝试使用但没有成功。
print(filter(lambda x: ("a","b") in x, x))
>>> <filter object at 0x00000214B3A545F8>
Run Code Online (Sandbox Code Playgroud)
def f(lst, target):
return [t for t in lst if len(t) > len(target) and all(a == b for a, b in zip(t, target))]
Run Code Online (Sandbox Code Playgroud)
以便:
f(x, ('a', 'b'))
Run Code Online (Sandbox Code Playgroud)
返回:
[('a', 'b', 'c', 'd'), ('a', 'b', 'c')]
Run Code Online (Sandbox Code Playgroud)