pow*_*xie 4 python nested-lists
Consider the lists:
assigned = [4,8]
matching = [['B', [4, 5, 6]], ['C', [7, 8, 9]]]
Run Code Online (Sandbox Code Playgroud)
I am trying to remove given integers with the following code
for ii in range(len(assigned)):
while any(assigned[ii] in x for x in matching):
matching.remove(assigned[ii])
Run Code Online (Sandbox Code Playgroud)
I have two problems here. First one is to get into the inner lists. Right now the code does nothing because there is no matching.
Second problem, I tried this:
t = ['B', [4, 5, 6]]
if any(4 in x for x in l2):
Run Code Online (Sandbox Code Playgroud)
And the result was an error:
if any(4 in x for x in l2):
TypeError: 'in <string>' requires string as left operand, not int
Run Code Online (Sandbox Code Playgroud)
Is there any way to achieve both in no more than two lines of code: found matching in nested lists and remove those matchings?
这是使用嵌套列表理解的一种方法:
matching = [[i[0], [j for j in i[1] if j not in assigned]] for i in matching]
print(matching)
Run Code Online (Sandbox Code Playgroud)
输出:
[['B', [5, 6]], ['C', [7, 9]]]
Run Code Online (Sandbox Code Playgroud)