1

I have two arrays: (arrayA contains ints and arrayB and arrayC contain strings)

arrayA = [5, 4]
arrayB = ["id_1", "id_2"] #arrayA and arrayB always have same lengths 
arrayC = ['3', '4', '15', '20', '23', '8', '11', '14', '21']

I want result:

res = {"id_1": ['3', '4', '15', '20', '23'], 
       "id_2": ['8', '11', '14', '21']}

so basically I want to tell the program that id_1 of arrayB contains first 5 strings and id_2 of arrayB contains next 4 strings.

similarly a result from a problem like following would be:

A = [2, 1, 3, 4]
B = ["a", "b", "c", "d"]
C = ['23', '8', '11', '3', '4', '15', '20', '23', '100', '45'] 

res = {"a": ['23', '8'], 
       "b": ['11'],
       "c": ['3', '4', '15'], 
       "d": ['20', '23', '100', '45']}

3 Answers 3

2

You can try:

A = [2, 1, 3, 4]
B = ["a", "b", "c", "d"]
C = ['23', '8', '11', '3', '4', '15', '20', '23', '100', '45'] 


res = {}
ix = 0
for k, l in zip(B, A):
    res[k] = C[ix:(ix+l)]
    ix += l

print(res)

# {'a': ['23', '8'], 'b': ['11'], 'c': ['3', '4', '15'], 'd': ['20', '23', '100', '45']}

The solution works as follows:

First, we create a dictionary called res to keep track of the result. Then, we make a variable called ix and set it to 0. This variable helps us keep track of where to index the array C to get the desired values for our result dictionary. Further, we zip arrays B and A. This zipping process is equivalent to creating a new list of tuples from B and A that looks like [("a", 2), ("b", 1), ("c", 3), ("d", 4)]. Now that you have this zipped container, we iterate through with a for-loop; and hence the for k, l in zip(B, A) part. Subsequently, at every iteration, we slice array C from ix to ix + l; where l is the corresponding integer value from array A. The values from this slicing operation are saved in our res dictionary with a key from the array B. Finally, we increment the value ix by the value of l, to make sure that the next slice we make moves forward through array C.

I hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for this. can you please explain the code too? what is happening in for loop and what is zip function?
this was so beautifully and thoroughly explained! thank you so much! I have never worked witn for loop containing more than one variables but now i have and it has made life so much more easy! Thank you!
1

You could use itertools.islice to peel off portions of C according to the lengths provided by A. For this to work, you'll also need to make C an iterator so the slices start where the last slice left off:

import itertools as IT
A = [2, 1, 3, 4]
B = ["a", "b", "c", "d"]
C = ['23', '8', '11', '3', '4', '15', '20', '23', '100', '45'] 
C = iter(C)
result = {bi: list(IT.islice(C, ai)) for ai, bi in zip(A, B)}
print(result)

yields

{'b': ['11'], 
 'c': ['3', '4', '15'], 
 'd': ['20', '23', '100', '45'], 
 'a': ['23', '8']}

3 Comments

thank you so much but i have never used itertools before. can you please explain each line of the code?
Your question is too broad since I don't know how much you already know. If everything about the line is a complete mystery, then the place to start is by reading the tutorial, in particular, list comprehensions, dictionaries, zip, and islice. If you have a more specific question, I'd be glad to explain.
thank you so much for providing the references! I will definitely look them up and ask you if I have further questions!
0
A = [2, 1, 3, 4]
B = ["a", "b", "c", "d"]
C = ['23', '8', '11', '3', '4', '15', '20', '23', '100', '45']
res = {}
ind = 0
for i in range(0, len(A)):
    res[B[i]] = C[ind:A[i] + ind]
    ind = ind + A[i]

print(res)

result:

{'a': ['23', '8'], 'b': ['11'], 'c': ['3', '4', '15'], 'd': ['20', '23', '100', '45']}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.