Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help! Lists
#1
Hi everyone! Here I am trying to fill array a with the numbers in range(100)

s = list(range(100))
a = stdarray.create2D(10, 10, 0)
stdarray.write2D(a)
b = []
b += [a]
print(b)

for i in range(10):
    for j in range(10):
        for k in range(len(s)):
            b[i][j] += [s[k]]

print(b)
Unfortunately I kept getting this error. I'll be appreciated if you can help me out.

10 10
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
[[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]]
Traceback (most recent call last):
line 34, in <module>
b[i][j] += [s[k]]
IndexError: list index out of range

Desired Output:
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]]
Reply
#2
There are a couple of problems. Your first problem is you built b as a 3D list.
b=[]
b+=[a]  # makes b a list that contains a
a is a 2D list, so when you put it inside b, you made a 3D list. You get an index error because len(b) = 1. "b" only contains one 2D list. I don't know why you don't just use "a", but if you need a copy, use the list copy function. b = a.copy().

Solving that would cause you to see this message:
Output:
b[i][j] += [s[k]] TypeError: unsupported operand type(s) for +=: 'int' and 'list'
You get this error because python does not know how to add a list and a number. b[i][j] is a number and [s[k]] is a list. You could use b[i][j] = s[k] and that would be valid python, but the results are not what you want. "b" would be a 2D list filled with 99. The third for loop would set b[i][k] to 0, then 1, then 2 and so on up to 99. This is repeated for each element in each list until every element is 99.

To fill a 2D array use 2 for loops, not 3.
for i in range(10):
    for j in range(10):
        b[i][j] = i*10+j
There are so many ways to solve this problem.
# Relies on package that provides little benefit and limits use
b = stdarray.create2D(10, 10, 0)
for x in range(100):
    b[x//10][x%10] = x

# Same as above but without package dependency
b = [[0]*10]*10
for x in range(100):
    b[x//10][x%10] = x

# Build and fill at the same time
b = []
for x in range(0, 100, 10):
    b.append(list(range(x, (x+10))))

or

# Compact comprehension version of above
b = [list(range(x,x+10)) for x in range(0, 100, 10)]
Reply
#3
Try a list

numList = []
for i in range(0, 100):
    numList.append(str(i + 1)) #This will add the numbers 1 to 100 in string form to each list index
You need to +1 after i so you don't get 0 and actually get 100 as the last entry

I've tried this. It works! This is a very simple method! Also, this uses no third-party libs.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,361 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,254 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020