0

I have tried many things but still stuck. This is mostly because I'm failing to understand how python loops through an array or what options I have to loop through it.

I want to build an array: 4 columns, 18 rows.

I need the first column of each row to be a persons name in the person_list. Every 3 rows of my array I would then increment to the next person.

It would seem that my loop is not looping through the array the way I intended. It seems that it sets all values in the first column equal to the person_list value.

person_list = ["person1", "person2","person3","person4","person5","person6"]
my_array = [[0]*4]*len(person_list)*3

i=0
j=1
for row in my_array:
    while j <= 3:
        row[0] = person_list[i]
        j+=1
    j=1
    i+=1

So the goal would be the resulting array

[
["person1", 0, 0, 0],
["person1", 0, 0, 0],
["person1", 0, 0, 0],
["person2", 0, 0, 0],
["person2", 0, 0, 0],
["person2", 0, 0, 0],
["person3", 0, 0, 0],
["person3", 0, 0, 0],
...
]

4 Answers 4

1

The problem seems to stem from the way my_array was initialized. Each row is 'nested' with the other rows, by that I mean if you try to modify my_array[0], you will end up modify all the remaining rows. Here is an example to illustrate that:

my_array[0].insert(0, 'hello_world')
# this prints:
# [['hello_world', 0, 0, 0],
   ['hello_world', 0, 0, 0],
   ....
   ['hello_world', 0, 0, 0]]

To get around, this you can use numpy to initialize your array of zeros or do something like this:

my_array = [[0, 0, 0, 0] for i in range(18)]

To make the code slight more concise and print the expected output, you can then do the following:

for i in range(len(person_list)):
    # person1 : idx 0, 1, 2
    # person2 : idx 3, 4, 5
    # ...
    end_idx = (i + 1) * 3
    start_idx = end_idx - 3

    for sub_arr in arr[start_idx:end_idx]:
        sub_arr.insert(0, person_list[i])

Hope this helped!

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

Comments

0

You could use the modulo % to increment at certain intervals. For example, row%3==0 will return True at multiples of 3. FYI in your above code you were not resetting the row variable every interval of 3.

person_list = ["person1", "person2","person3","person4","person5","person6"]
my_array = [[0]*4]*len(person_list)*3

j=0
i=0
for row in my_array:
    if j%3==0:
        row[0] = person_list[i]
        i+=1
    j+=1 # increment the row here

Comments

0

I think everything is good in your code, you are just multiplying it by 3 :

person_list = ["person1", "person2","person3","person4","person5","person6"]
my_array = [[0]*4]*len(person_list)

i=0
j=1
for row in my_array:
    while j <= 3:
        row[0] = person_list[i]
       j+=1
    j=1
    i+=1

I think its this like which needs to change from my_array = [[0]*4]*len(person_list)*3 to my_array = [[0]*4]*len(person_list)

1 Comment

Sorry, I edited my answer to demonstrate my expected resulting array. I need 18 rows and multiplying by 3 gave me the correct dimensions in the resulting array. So the x3 is required.
0

So I think NeuralX was helpful in pointing out the way I create the array is not going to work for what I intend to do to it. He is absolutely correct when I run a change it will update every entry in the final array.

In more searching with working with nested lists, I have found how I should create the array so that I can update nested lists individually by looping through it. The final results are going to be below.

person_list = ["person1", "person2","person3","person4","person5","person6"]

# The way I create the array appears the be the main root cause here. I need to create it like this
my_array = [[0 for x in range(4)] for y in range(sum(person_assessments))]

i=0
j=0
for person in person_list:

    for assessment in range(1, 4):
        my_array[i][0] = person
        # print(person, ", ", assessment)
        # print(i,", ", j,", ", person)

        i+=1

    j += 1

print(my_array)

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.