#๐ array list
352 messages ยท Page 1 of 1 (latest)
@golden olive
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
import array as a
import random as r
array1 = a.array("i", [])
array2 = a.array("i", [])
array3 = a.array("i", [])
for i in range(10):
array1.append(r.randint(1,10))
array2.append(r.randint(1,10))
array3.append(r.randint(1,10))
matrice = [[array1], [array2], [array3]]
print(matrice)
the output is "[[array('i', [3, 9, 5, 8, 8, 6, 1, 3, 2, 7])], [array('i', [10, 7, 1, 3, 5, 6, 9, 2, 1, 8])], [array('i', [4, 1, 5, 5, 4, 3, 9, 4, 10, 4])]]"
what i want is to remove every time the "[array('i',[])]" part
what can i do? i also can print the whole array list with a for loop, but i was searching for a better solution
what i tryed: strip, it dosent work
It's a weird way to create a 2d (?) matrix to begin with, but that is simply how an array is printed
You can make a function for printing it, but a better option is to use numpy
Are you allowed to use numpy? @golden olive
!e
import numpy as np
matrice = np.random.randint(1, 10, (3, 1))
print(matrice)
@glad sigil :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | [[7]
002 | [7]
003 | [3]]
No :(
Also I wasn't at school when they started talking about array list, I can give you the code that they made but I don't understand it at all
There are no comments
sure
from random import *
import array
r = int(input("inserisca il numero delle righe che vuole inserire "))
while r < 1 :
r = int(input("inserisca il numero delle righe che vuole inserire "))
c = int(input("inserisca il numero di colonne che vuole inserire "))
while c < 1 :
c = int(input("inserisca il numero delle colonna che vuole inserire "))
min = int(input("inserisca il valore minimo "))
max = int(input("inserisca il valore massimo "))
while max <= min :
max = int(input("inserisca il valore massimo "))
m = []
for y in range(r) :
ar1 = []
for i in range (c):
x = randint(min, max)
ar1.append(x)
m.append(ar1)
for j in range(r) :
for t in range (c) :
print(m[j][t], end="\t")
print()
for j in range (r):
max = min-1
for i in range(c) :
val1 = m[j][i]
if val1 > max :
max = val1
print (max)
That is the code they gave you?
Yes
I am working on a list of array, (someting like "list = [[list1],[list2],[list3]]
They are not using arrays though, but lists
i saw, but they used "import array"
the task is:
Their import array is pointless
"create an array made of list, ask the user for the number of column and row and print the array"
its ramndom generated so also ask for min and max for the randint range
Is that translated, or the literal assignment text?
this is the result of 5-5 with 1 as min and 10 as max
thats what the code should do, the assignment was not given
Okay, and that code you just send was their solution to this assignment?
And you want to try and do it yourself now
they just send the code saing "this is the code that we made"
yes
also yes
The code they sent is not great to say it nicely... many beginner mistakes like shadowing existing function names, using 1 letter variable names, importing an entire module with *, importing a module and not using etc.
Take it with a grain of salt
And I think we can just use a list of lists
So not necessary to use array (if they do not use it either)
ok
tbh i dont understand that well how does list of list works
A list is just something that has a reference to one or multiple objects
So it can hold all types of different stuff
!e
my_list = [1, 2, "a", "abcdef", 5.0, None]
print(my_list)
@glad sigil :white_check_mark: Your 3.12 eval job has completed with return code 0.
[1, 2, 'a', 'abcdef', 5.0, None]
So this is an example of a list holding a few different objects
Like numbers, strings, etc.
This makes sense?
So when we create the list, we have the following:
A variable called "my_list" which refers to all kinds of objects
And we access each separate object by supplying an index, like my_list[0] refers to the object 1
And my_list[3] refers to "abcdef"
This makes sense?
yeah
Okay, so a more complicated example now
!e
my_list = [0, [1, 2, 3], 4, 5, [6, 7], 8]
print(my_list)
@glad sigil :white_check_mark: Your 3.12 eval job has completed with return code 0.
[0, [1, 2, 3], 4, 5, [6, 7], 8]
something like
for i in range(len(my_list)):
print(my_list[i], end=" ")
should print it all right?
ok so in a big list we added another list
how can we call them this time?
Yeah, so my_list refers to the "outer list"
And in this outer list we have some numbers, and some inner lists
So let's say we want to access the 8, we can value = my_list[5]
Does that make sense?
good question
something like mylist[4][0]?
Is the [6, 7] at position 1 or 4 of my_list ?
4
So we acess the item at index 4
!e
my_list = [0, [1, 2, 3], 4, 5, [6, 7], 8]
print(my_list[4])
@glad sigil :white_check_mark: Your 3.12 eval job has completed with return code 0.
[6, 7]
Okay, so now we have a reference to this inner list, we want acess to the 7
my_list = [0, [1, 2, 3], 4, 5, [6, 7], 8]
inner_list = my_list[4]
How do we get this value?
Right
something like inner_list[1]
!e
my_list = [0, [1, 2, 3], 4, 5, [6, 7], 8]
inner_list = my_list[4]
value = inner_list[1]
print(value)
@glad sigil :white_check_mark: Your 3.12 eval job has completed with return code 0.
7
Exactly
And since inner_list is the same as my_list[4], we can shorten the two lines to value = my_list[4][1]
Is the same as inner_list[1]
Does this make sense?
yeah
my_list = [0, [1, 2, 3], 4, 5, [6, 7], 8]
value = my_list[1][2]
print(value)
Okay, think we got it so far
why cant we just combine it in the print?
We can
I just want to show it in steps, so you understand why we can do my_list[a][b]
It's not a special syntax to do [a][b] on a list, it's just indexing an inner list, and then indexing from this inner list
So now we want to make a "2D matrix" with lists
So the idea would be that the outer list would hold a number of rows, and each row would hold a number of values/numbers
More like a rectangle
This would be the structure
An outer list with references to some other lists
Each list representing a row of the matrix
can we make the first list (index 0) with a len of 4?
or everything need to be the same
We can actually do this, yes
But if we want to think of this outer list as a matrix, this would not make sense
Remember that the outer list can hold a reference to different types of objects
So the item at position 0 can even be a string or single number
Does not even need to be a list
oh ok, i am understainding
So we could start with make the general structure
Which is a list holding a reference to a number of lists
Let's say we know the number of rows and columns
!e
rows = 4
columns = 3
matrix = []
...
@glad sigil :warning: Your 3.12 eval job has completed with return code 0.
[No output]
So now we want this outer list to hold a reference to how many lists?
4
RIght, because each inner list holds item for a single row of the matrix
Are you able to add 4 empty lists to the matrix?
no i dont think i can
Do you know how to make an empty list?
like this []
Right
but then i dont respect the 3 of colomns
We don't care about how many columns
We are just adding an empty list 4 times
We can fill these rows later on (3 values per row)
okk
with .append or .insert right?
Jup
and the same way we can add empty lists to the matrix
What do we use if we want to do the same thing multiple times?
control c and control v?
We use a loop to repeat some code multiple times
We want to repeat this code 4 times (we add a row 4 times), so how do we write this with a loop
yeah, but if we want to make the list all the same dont we use avariable?
A variable is just a reference to some value
Like a pointer to some value, or simply just a name you give some value
Okay, so repeating code 4 times, how do you do that?
variable = [1,2,3]
my_list = [variable, variable, variable]
isnt this what we want to make?
There are some issues with that approach
First of all, you "hardcode" such that it adds 3 rows
We can have any number of rows
Second of all:
Do you see the issue here?
yeah, every index of the list refer to the same
so a for i in range(3) loop is the best
Okay
But we are given rows = ... and columns = ...
rows = 4
columns = 3
matrix = []
yes, let me try
ok sorry i cant
Can you simply just write a loop that adds an empty list the correct number of times
oh empty
Yes, no nested loop necessary, just a single for loop
for i in range(row):
matrix.append([])
!e
rows = 4
columns = 3
matrix = []
for i in range(rows):
matrix.append([])
print(matrix)
@glad sigil :white_check_mark: Your 3.12 eval job has completed with return code 0.
[[], [], [], []]
rows not row but yes correct
sorry
So now we have the general structure
Now let's say we want to add columns items to the first row of the matrix
How do we access the first row? row = ...
matrix[0]
a for loop
And let's say we want to add a random integer that number of times
And we have a minimum and maximum value for this random integer
rows = 4
columns = 3
min_value = 1
max_value = 10
matrix = []
for i in range(rows):
matrix.append([])
row = matrix[0]
for ...:
...
Can you fill in the dots to add the correct number of random values to the first row?
Yes, so let's import random indeed
And use random.randint(...)
import random
rows = 4
columns = 3
min_value = 1
max_value = 10
matrix = []
for i in range(rows):
matrix.append([])
row = matrix[0]
for i in range(columns):
random_value = random.randint(min_value, max_value)
Fill in the dots
min, max
again?
We defined the minimum and maximum in this code
What values do we use
sorry, min_value and max_value
Okmay so filled in the dots
And then we want to add it to the first row, so what do we add to the for loop?
Give the entire line of code we should add
!e
import random
rows = 4
columns = 3
min_value = 1
max_value = 10
matrix = []
for i in range(rows):
matrix.append([])
row = matrix[0]
for i in range(columns):
random_value = random.randint(min_value, max_value)
row.append(random_value)
print(matrix)
@glad sigil :white_check_mark: Your 3.12 eval job has completed with return code 0.
[[1, 7, 1], [], [], []]
Right, and because we have 4 separate lists (and not 4 reference to a single list) we get this output
So we have succesfully filled in one of the rows
But obviously we want to do this for every row
i have an idea
let me write it one sec
for i in range(row):
row = matrix[i]
for j in range(columns):
random_value = random.randint(min_value, max_value)
row.append(random_value)
!e
import random
rows = 4
columns = 3
min_value = 1
max_value = 10
matrix = []
for i in range(rows):
matrix.append([])
for i in range(rows):
row = matrix[i]
for j in range(columns):
random_value = random.randint(min_value, max_value)
row.append(random_value)
print(matrix)
we fill the first with index 0, the the for continues with an i = 1
@glad sigil :white_check_mark: Your 3.12 eval job has completed with return code 0.
[[7, 10, 5], [9, 4, 3], [4, 10, 2], [2, 1, 2]]
sorry i forgot the s
lez go!
So the only thing to change is that you should ask the user for the rows, columns and min_value and max_value
so now my matrix its a list of list were everything is created randomly
Jup
this is even better than the code that my class made
only ptoblem is to make a good print as they made
So it should be printed for each row with tabs then?
Yeah
But that doesn't seem to hard
We take 1 rows at the time and print it index by index
And use an ,end="/t" for the tab space
Right?
They also seem to print the maximum* value for each row
for j in range (r):
max = min-1
for i in range(c) :
val1 = m[j][i]
if val1 > max :
max = val1
print (max)
This part
Prints the maximum value of each row
And before that they print each value with a tab after it
import random
rows = 4
columns = 3
min_value = 1
max_value = 10
matrix = []
# Create an empty matrix
for i in range(rows):
matrix.append([])
# Fill the matrix
for i in range(rows):
row = matrix[i]
for j in range(columns):
row.append(random.randint(min_value, max_value))
print(matrix)
So this is our code so far
Simplified the filling in to one line
Can you create some code for printing?
Let me try
!e
import random
rows = 4
columns = 3
min_value = 1
max_value = 10
matrix = []
# Create an empty matrix
for i in range(rows):
matrix.append([])
# Fill the matrix
for i in range(rows):
row = matrix[i]
for j in range(columns):
row.append(random.randint(min_value, max_value))
for i in range(rows):
row = matrix[i]
for j in row:
print(j, end="\t")
print() # New line for the new row
print(f"\nmin: {min_value}\nmax: {max_value}") # For max and min, \n just to space
@golden olive :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 7 1 5
002 | 6 10 9
003 | 9 10 4
004 | 4 10 6
Jup, that is what they had as well
You can see it still screws up the allignment a bit
There are ways to fix this, but they are probably a bit too advanced for now
Well not really, they want to print the minimum value for each row of the matrix
Which is random each time (because the matrix is randomly filled)
cant we just use the min() function?
Yeah haha, but they shadow over it by doing min = ...
So now min holds a value instead of the function that gives you a minimum value
We could still do that because we don't replace the value of min since we named our variable min_value
So in that case we can do
for row in matrix:
print(min(row))
But they might want you to do it without the min function
import random
rows = 4
columns = 3
min_value = 1
max_value = 10
matrix = []
# Create an empty matrix
for i in range(rows):
matrix.append([])
# Fill the matrix
for i in range(rows):
row = matrix[i]
for j in range(columns):
row.append(random.randint(min_value, max_value))
for i in range(rows):
row = matrix[i]
for j in row:
print(j, end="\t")
print("\tmin:",min(row))
print() # New line for the new row
ok let me try
I would do it separately
Not in the other loop (they would print interleaved that way)
okk
the teacher like to make the code more hard than it should be
let me see if i can make withoiut the function
Yeah, I would really try to find your resources elsewhere, this teacher is not proficient with Python at all
This is code that someone with a few weeks (maybe a month or two) of experience would make
!e
import random
rows = 4
columns = 3
min_value = 1
max_value = 10
matrix = []
# Create an empty matrix
for i in range(rows):
matrix.append([])
# Fill the matrix
for i in range(rows):
row = matrix[i]
for j in range(columns):
row.append(random.randint(min_value, max_value))
for i in range(rows):
row = matrix[i]
for j in row:
print(j, end="\t")
print() # New line for the new row
for i in range(rows):
min = max_value # Biggest value possible in the code, reset the variable
row = matrix[i]
for j in row:
if j < min:
min = j
print("min value for row",i,"is:",min)
@golden olive :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 6 9 1
002 | 3 1 3
003 | 8 6 3
004 | 2 5 10
005 | min value for row 0 is: 1
006 | min value for row 1 is: 1
007 | min value for row 2 is: 3
008 | min value for row 3 is: 2
emh
Do you see that min and max is highlighted?
we started school in september, started python in that moment
yesh because they are function
idk why in vs code i dont get the color
Otherwise we could not use the min and max function after that
You'll remember most of the names after some practice
vscode syntax highlighter is just not that smart I guess
sad
i just used min1
yeaht thats a good idea
and put a lot of comment
the code without them is not readable
Same goes for the for i in range(rows) and for j in range(columns), might as well do for row_nr in range(rows) f.e.
If your variable names make sense, then comments are not as necessary
It's kind of subjective. Doing from random import randint makes it so you only have to write randint(0, 10) in your code instead of random.randint(0, 10) f.e
This is shorter, but it is unclear where this function randint comes from in your code
So when someone reads your code and sees randint they will need to search up what kind of function this is
But if you do random.randint(0, 10) it is already clear where the function comes from
oh ok, well yeah its less intuitive
Also, multiple modules can have a function randint
and what does "from random import *" mean
code ๐ฅ
it basically means from random import randint, randrange, random, ....
So importing literally everything from the random module
oh so the less intuitive thing to do
Without the need to write random. in the code
Yes, and you are filling the "namespace" with all the crap inside random
So it's a bad practice
so the code of my class was litherally the wrost thing
Yeah it sucks pretty bad for a teacher
This would be my final code
import random
rows = 4
columns = 3
min_value = 1
max_value = 10
matrix = []
# Create an empty matrix
for i in range(rows):
matrix.append([])
# Fill the matrix
for row_nr in range(rows):
for j in range(columns):
matrix[row_nr].append(random.randint(min_value, max_value))
# Print the matrix
for row_nr in range(rows):
for value in matrix[row_nr]:
print(value, end="\t")
print() # New line for the new row
# Print the maximum value for each row
for row_nr in range(rows):
max_row_value = max_value # Biggest value possible in the code, reset the variable
for value in matrix[row_nr]:
if value > max_row_value:
max_row_value = value
print("max value for row", row_nr, "is:", max_row_value)
Shortened some things (like not doing row = ... but directly using matrix[row_nr])
wait no
its the minimun value
In their code they want the maximum value iirc
idk they did a lot of strange stuff
yeah like I said, many weird beginner mistakes, or at the very least mistakes by not knowing python very well
And the short variable names and stuff make it seem like they are just bad at making readable code in general
tomorrow i will have python lesson, thx to you i will understand it
Yeah hope it helped, took a bit of time, but it should make it a bit more sense ;D
idk, i wasnt i class. maybe they did it for a reson
thx you so much for the help and for theaching me. now i can say that i know how to use matrix
bye, thx again
!solved
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.