Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mapping digits to their appearances in a list of numbers

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[[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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading