I need to take in a sequence of numbers. The last number in this sequence gives a sequence of digits. For each digit, I need to create a list of all the numbers that contain that digit. If no numbers contain that digit, the list should contain 0 rather than an empty list.
Input:
96 23 43 113 6
315
I need output like this:
[[23,43,113],[113],0]
need to store:
1st element in k is 3 -> [23,43,113]
2nd element in k is 1 -> [113]
3rd element in k is 5 -> 0 (No 5 in any element in LIST(l))
I tried the following:
l=list(map(str,input().split()))
k=input()
ans=[[l[j]] for i in range(len(k)) for j in range(0,len(l)) if k[i] in l[j]]
print(ans)
but received the following output:
[['23'], ['43'], ['113'], ['113'], ['113']]
>Solution :
Why don’t you just do it not in one line, This way it would be more transparent:
goal = []
for char in list(k):
temp = [element for element in l if char in element]
temp = temp if temp else 0
goal.append(temp)
But if you still want your code to be short in one line:
goal = [[element for element in l if char in element] or 0 for char in k]