We have answer of your question!

100% solved queries, no empty question

Question: Iterating a list of tasks to get the translators that can perform the translation in python


0

l1 = [('x', [1]), ('y', [1]) ('z', [2]]
l2 = ['1', '2', '3']

Desired output, assuming that '1' can be done by 'x' and 'y', and 'z' can do '2':

l3 = [[['1'['x', 'y']], ['2'['z']], ['3'[]]

So then i can do an untie between 'x' and 'y' using aditional paramenters that for the sake of redability i wont be putting here unless asked.

This is what i've come to so far:

x = []
for i in l2:
    for j in l1:
        if i in j[1][0]:
              x.append(j[0]) 

This gives me the possible l1, but i'm having no luck in adding the l2 part in a way i can put 'x' and 'y' toghether. Since this a school project, i can't use tools like zip.

Question author Stasisoo | Source

Answer


1


Build a dict with the keys being the elements of l2, initializing their values as a list, then iterate over l1 elements appending element[0] to the list of the key that matches element[1].

l3 = {}
for elem in l2:
    l3[elem] = []

# Look for matches in l1
for letter, tasks in l2:
    for task in tasks:
        l3[str(task)].append(letter)

In this algorithm, l3 will be a dict instead of list which makes it easier to access the values in each task. If you really need to be a list, just convert it to one.

Answer author Lucasnadalutti

Tickanswer.com is providing the only single recommended solution of the question Iterating a list of tasks to get the translators that can perform the translation in python under the categories i.e python , list , . Our team of experts filter the best solution for you.

Related Search Queries:


You may also add your answer

Thanks for contributing an answer to Tick Answer!