osaama.s
osaama.s

Reputation: 59

array and string manipulation

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']}

Upvotes: 1

Views: 179

Answers (3)

Vashdev Heerani
Vashdev Heerani

Reputation: 679

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']}

Upvotes: 0

unutbu
unutbu

Reputation: 879701

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']}

Upvotes: 1

Abdou
Abdou

Reputation: 13274

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.

Upvotes: 2

Related Questions