#๐Ÿ”’ array list

352 messages ยท Page 1 of 1 (latest)

golden olive
#

this is my code, my array list holds 3 array, the problem is the output:

elder scarabBOT
#

@golden olive

Python help channel opened

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.

golden olive
#
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

glad sigil
#

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)
elder scarabBOT
#

@glad sigil :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | [[7]
002 |  [7]
003 |  [3]]
glad sigil
#

What type of array are you trying to make?

#

What should the shape be?

golden olive
#

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

glad sigil
#

sure

golden olive
#
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)
glad sigil
#

That is the code they gave you?

golden olive
#

Yes

glad sigil
#

yikes

#

What is your task, I can't read spanish (?)

golden olive
#

I am working on a list of array, (someting like "list = [[list1],[list2],[list3]]

glad sigil
#

They are not using arrays though, but lists

golden olive
#

the task is:

glad sigil
#

Their import array is pointless

golden olive
#

"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

glad sigil
#

Is that translated, or the literal assignment text?

golden olive
#

this is the result of 5-5 with 1 as min and 10 as max

golden olive
glad sigil
#

Okay, and that code you just send was their solution to this assignment?

#

And you want to try and do it yourself now

golden olive
#

they just send the code saing "this is the code that we made"

golden olive
glad sigil
#

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)

golden olive
#

tbh i dont understand that well how does list of list works

glad sigil
#

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)
elder scarabBOT
#

@glad sigil :white_check_mark: Your 3.12 eval job has completed with return code 0.

[1, 2, 'a', 'abcdef', 5.0, None]
glad sigil
#

So this is an example of a list holding a few different objects

#

Like numbers, strings, etc.

#

This makes sense?

golden olive
#

yeah

#

its like an array, but more easy

glad sigil
#

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?

golden olive
#

yeah

glad sigil
#

Okay, so a more complicated example now

#

!e

my_list = [0, [1, 2, 3], 4, 5, [6, 7], 8]

print(my_list)
elder scarabBOT
#

@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]
glad sigil
#

Now we have a list, and some of the objects it refers to are lists as well

golden olive
#

something like

for i in range(len(my_list)):
  print(my_list[i], end=" ")

should print it all right?

golden olive
#

how can we call them this time?

glad sigil
#

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?

golden olive
#

yeah

#

but what about the list inside? dosent them make number too?

glad sigil
#

So let's say we want to access the 7, how do we do that?

#

value = ...

golden olive
glad sigil
#

Maybe an easier question, how do we acess the [6, 7] list?

#

the inner list

golden olive
glad sigil
#

We want to acess the inner list

#

so inner_list = my_list[...]

#

The ... should be ?

golden olive
#

4?

#

or 1

glad sigil
#

Is the [6, 7] at position 1 or 4 of my_list ?

golden olive
#

4

glad sigil
#

So we acess the item at index 4

#

!e

my_list = [0, [1, 2, 3], 4, 5, [6, 7], 8]

print(my_list[4])
elder scarabBOT
#

@glad sigil :white_check_mark: Your 3.12 eval job has completed with return code 0.

[6, 7]
glad sigil
#

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?

golden olive
#

so in the inner list we have to access the index 0 or 1

#

for 7 the index is 1

glad sigil
#

Right

golden olive
#

something like inner_list[1]

glad sigil
#

!e

my_list = [0, [1, 2, 3], 4, 5, [6, 7], 8]
inner_list = my_list[4]
value = inner_list[1]
print(value)
elder scarabBOT
#

@glad sigil :white_check_mark: Your 3.12 eval job has completed with return code 0.

7
glad sigil
#

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?

golden olive
#

yeah

glad sigil
#

So just to make sure you get it

#

How to acess the 3?

#

value = ...

golden olive
#
my_list = [0, [1, 2, 3], 4, 5, [6, 7], 8]
value = my_list[1][2]
print(value)
glad sigil
#

Okay, think we got it so far

golden olive
#

why cant we just combine it in the print?

glad sigil
#

We can

#

I just want to show it in steps, so you understand why we can do my_list[a][b]

golden olive
#

okkk

#

so we made a list with some list

glad sigil
#

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

golden olive
#

something like a square

#

or naval battle

glad sigil
#

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

golden olive
#

can we make the first list (index 0) with a len of 4?

#

or everything need to be the same

glad sigil
#

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

golden olive
#

oh ok, i am understainding

glad sigil
#

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 = []

...
elder scarabBOT
#

@glad sigil :warning: Your 3.12 eval job has completed with return code 0.

[No output]
glad sigil
#

So now we want this outer list to hold a reference to how many lists?

golden olive
#

4

glad sigil
#

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?

golden olive
#

no i dont think i can

glad sigil
#

Do you know how to make an empty list?

golden olive
#

like this []

glad sigil
#

Right

golden olive
#

but then i dont respect the 3 of colomns

glad sigil
#

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)

golden olive
#

with .append or .insert right?

glad sigil
#

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?

golden olive
#

control c and control v?

glad sigil
#

ehh

#

sometimes haha

#

loop?

golden olive
#

well yeah

#

or a variable

glad sigil
#

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

golden olive
#

yeah, but if we want to make the list all the same dont we use avariable?

glad sigil
#

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?

golden olive
#

variable = [1,2,3]
my_list = [variable, variable, variable]

#

isnt this what we want to make?

glad sigil
#

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?

golden olive
#

yeah, every index of the list refer to the same

glad sigil
#

Right

#

So adding variable[0] == 99 would give

golden olive
#

so a for i in range(3) loop is the best

glad sigil
#

Okay

#

But we are given rows = ... and columns = ...

#
rows = 4
columns = 3

matrix = []
golden olive
#

so a for in a for loop

#

one for each row and the other for each columns

glad sigil
#

We could do that if you want

#

So can you write that?

#

Actually, no

golden olive
#

yes, let me try

golden olive
glad sigil
#

Can you simply just write a loop that adds an empty list the correct number of times

golden olive
#

oh empty

glad sigil
#

Yes, no nested loop necessary, just a single for loop

golden olive
#

okkk

#

but how?

#

.append([])?

glad sigil
#

Yes

#

And you need to append rows times

golden olive
#

for i in range(row):
matrix.append([])

glad sigil
#

!e

rows = 4
columns = 3

matrix = []

for i in range(rows):
       matrix.append([])

print(matrix)
elder scarabBOT
#

@glad sigil :white_check_mark: Your 3.12 eval job has completed with return code 0.

[[], [], [], []]
glad sigil
#

rows not row but yes correct

golden olive
#

sorry

glad sigil
#

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 = ...

golden olive
#

matrix[0]

glad sigil
#

Okay, and we want to add columns items

#

So what do we need for repetition?

golden olive
#

a for loop

glad sigil
#

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?

golden olive
#

for i in range(columns)

#

and add the value with an r.randint

glad sigil
#

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

golden olive
#

min, max

glad sigil
#

again?

glad sigil
#

What values do we use

golden olive
glad sigil
#

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?

golden olive
#

an .append with random value in it

#

.append(random_value)

#

huh

glad sigil
#

Give the entire line of code we should add

golden olive
#

okk

#

row.append(random_value)

#

because we dont want to add it at the whole matrix

glad sigil
#

!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)
elder scarabBOT
#

@glad sigil :white_check_mark: Your 3.12 eval job has completed with return code 0.

[[1, 7, 1], [], [], []]
glad sigil
#

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

golden olive
#

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)
glad sigil
#

!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)
golden olive
#

we fill the first with index 0, the the for continues with an i = 1

elder scarabBOT
#

@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]]
golden olive
#

sorry i forgot the s

glad sigil
#

Jup

#

But that is entirely correct yeah!

golden olive
#

lez go!

glad sigil
#

So the only thing to change is that you should ask the user for the rows, columns and min_value and max_value

golden olive
#

so now my matrix its a list of list were everything is created randomly

glad sigil
#

Jup

golden olive
#

this is even better than the code that my class made

#

only ptoblem is to make a good print as they made

glad sigil
#

So it should be printed for each row with tabs then?

golden olive
#

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?

glad sigil
#

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?

golden olive
#

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
elder scarabBOT
#

@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	
glad sigil
#

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

golden olive
#

okk

#

also wait

#

this si the full code i think

glad sigil
#

Well not really, they want to print the minimum value for each row of the matrix

golden olive
#

oh not the min value

#

sorry i understand it wrongly

glad sigil
#

Which is random each time (because the matrix is randomly filled)

golden olive
#

cant we just use the min() function?

glad sigil
#

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

golden olive
#
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
golden olive
glad sigil
#

I would do it separately

#

Not in the other loop (they would print interleaved that way)

golden olive
#

okk

#

the teacher like to make the code more hard than it should be

#

let me see if i can make withoiut the function

glad sigil
#

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

golden olive
#

!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)
elder scarabBOT
#

@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
glad sigil
#

Do you see that min and max is highlighted?

golden olive
#

we started school in september, started python in that moment

golden olive
glad sigil
#

Right

#

So we should name our variables something else

#

As to not overwrite this

golden olive
#

idk why in vs code i dont get the color

glad sigil
#

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

golden olive
#

i just used min1

glad sigil
#

Would try to name your variables as useful as possible

#

Like min_row_value

golden olive
#

yeaht thats a good idea

#

and put a lot of comment

#

the code without them is not readable

glad sigil
#

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

golden olive
#

also true

#

also

#

isnt it better to do "from random import randint"?

glad sigil
#

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

golden olive
#

oh ok, well yeah its less intuitive

glad sigil
#

Also, multiple modules can have a function randint

golden olive
#

and what does "from random import *" mean

golden olive
glad sigil
#

it basically means from random import randint, randrange, random, ....

#

So importing literally everything from the random module

golden olive
#

oh so the less intuitive thing to do

glad sigil
#

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

golden olive
#

so the code of my class was litherally the wrost thing

glad sigil
#

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])

golden olive
#

its the minimun value

glad sigil
#

In their code they want the maximum value iirc

golden olive
#

idk they did a lot of strange stuff

glad sigil
#

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

golden olive
#

tomorrow i will have python lesson, thx to you i will understand it

glad sigil
#

Yeah hope it helped, took a bit of time, but it should make it a bit more sense ;D

golden olive
golden olive
#

bye, thx again

#

!solved

elder scarabBOT
#
Python help channel closed

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.