Faiz Ahmed
Faiz Ahmed

Reputation: 97

Combining two arrays into one in Python, by dynamic allocation

I have two arrays, list_string & list_string2. Their values are assigned and filled statically(In the editor)

list_string = ["1", "12", "15", "21", "131"]

#DEFINING THE ROW AND COLUMN VALUE FOR RESULTANT ARRAY
rows,coln = (5,5)

#INITIALIZING THE RESULTANT ARRAY
arr = [[0]for i in range(rows) for j in range(coln)]

list_string2 = ["1","2","3","4","5"]

#ITERATING THROUGH EACH OF THE LISTS FOR FILLING
for i in range(len(list_string)):
     for j in range(len(list_string2)):
         arr[i][j] = [list_string[i]] [list_string2[j]]) 

I did some reading, seeing other threads on defining, Initializing 2D arrays, but all of them create new arrays and statically assign the values. I'm trying to dynamically assign the values but the error code, I'm getting

Traceback (most recent call last): File "C:......intro2.py", line 12, in arr.append([list_string[i]] [list_2[j]]) TypeError: list indices must be integers or slices, not str

The indices i & j being used are definitely integers, since they are looping through the range method. Can anyone explain why this is happening.

EDIT 2:

Expected Output:

[ ["1" "1" "12" "15" "21" "131"]

["2 " "1" "12" "15" "21" "131"]

["3" " 1" "12" "15" ........"131"]

["4" "1" ......................"131"]

["5" ........................."131"]

Code changes:

I figured out some out of bounds index errors, and modified the code as follows,(Error still persists)

    list_string = ["1", "12", "15", "21", "131"]

#DEFINING THE ROW AND COLUMN VALUE FOR RESULTANT ARRAY
rows,coln = (5,6)
arr = [[0 for i in range(coln)] for j in range(rows)]

print(arr)

list_string2 = ["1","2","3","4","5"]

#ITERATING THROUGH EACH OF THE LISTS FOR FILLING
for i in range(len(list_string)):
     for j in range(len(list_string2)):
         arr[i][j] = [list_string[i]][list_string2[j]]


print (arr)

Upvotes: 1

Views: 492

Answers (1)

doca
doca

Reputation: 1548

Answer:

If I understood correctly, you do not need rows, coln variables since the loops depend on the sizes of list_string and list_string2

arr = [list_string[:] for _ in range(len(list_string2))]
for i, l in enumerate(arr): 
   l.insert(0, list_string2[i]) 

This will produce your expected output

>>> print(arr)
[['1', '1', '12', '15', '21', '131'],
 ['2', '1', '12', '15', '21', '131'],
 ['3', '1', '12', '15', '21', '131'],
 ['4', '1', '12', '15', '21', '131'],
 ['5', '1', '12', '15', '21', '131']]

First of all, there is an extra bracket here

arr[i][j] = [list_string[i]] [list_string2[j]])
                                              ^

To explain the error, it comes from here

arr[i][j] = [list_string[i]] [list_string2[j]]

[list_string[i]] evaluates to a new list with an element from list_string. For i=0, this is ["1"]. For [list_string2[j]] it is ["1"] for j=0

When you put square brackets after a list, syntactically that is to access an element of the list. So the last line of you code is equivalent to the below

lstr = ["1"]            # [list_string[0]]
arr[i][j] = lstr["1"]   # [list_string[0]] [list_string2[0]]

You are trying to access element "1" of a list which does not make sense. This is why you are getting TypeError: list indices must be integers or slices, not str

Upvotes: 1

Related Questions