Learning Python

This is for discussions about news, politics, sports, other games, culture, philosophy etc.
User avatar
Great Britain I_HaRRiiSoN_I
Retired Contributor
Posts: 1626
Joined: Jan 15, 2016
Location: United Kingdom

Learning Python

  • Quote

Post by I_HaRRiiSoN_I »

Hey Squad!

so while ive been doing shitty work i have had to think about next steps in real life about work/career etc. I did a masters in Physics and at the moment and then had a data analysis job at an airline but felt much more 'safety & compliance' and based administration work really. i think i want to head down the data science with statistics route and so Ive decided to start off with learning python. I was just really wanting to know from people:
- Anybody here good with python?
- How did you get into python and why?
- Has it been worth it?
- any pointers for how i should go about learning this?

I will also want to get back into R studio at some point in the future and revisit statistics and do some further statistics learning but unsure whether i should try and learn this on a job, education institute, online courses, from a chunky textbook etc. The only kicker is that i still couldnt answer the question "what do you want to do in life" 100% confidently at the moment and I dont think i will ever. do we have an official coding thread yet for this kinda thing?

I have to admit i do want to spend a little less time with AoE as a result and am a little gutted by the amount of time wasted into AoE over lockdown casting/playing/casual playing and although it was fun i think I have missed a massive trick there while the whole country was busy doing F all.

cheers for any advice pointers
H
User avatar
Netherlands Goodspeed
Retired Contributor
Posts: 13006
Joined: Feb 27, 2015

Re: Learning Python

  • Quote

Post by Goodspeed »

- How did you get into python and why?
I first used it because someone wrote a library for it that did something very specific which I needed and the language I favor (C#) didn't have it as far as I could see.

Then from there I used it for misc scripting tasks and smaller hobby projects like a helper app for the game dominion (for balance purposes). I always found it fun to work with, and weak/dynamic typing can be convenient. But its main advantage I've found is the large collection of libraries written by the community that are often simply not there for other languages.

As for how I got into it, I just googled how Python implemented concepts I already knew. Learning programming is mostly about learning concepts for efficiently expressing the language of logic to a computer. Once you know the concepts it's fairly straightforward to switch between languages.
- Has it been worth it?
Yeah, I still use it frequently both at work and for hobby projects, and like working with it. But if I want to build something big and maintainable long-term, I think strongly typed languages are superior so I tend to avoid Python.

For your purposes though it sounds like the way to go because it has a lot of great libraries for data analysis.
- any pointers for how i should go about learning this?
To learn programming it doesn't really matter which language you start with, and Python seems as good as any. I would recommend doing a basic tutorial and giving yourself small projects you are interested in. Then simply google how to do things as you go along. That's always been the only thing that worked for me, because it's fun. Doing assignments out of a book is less fun. And ultimately you learn programming by doing, by lots of trial and error, not so much by reading a book, although it helps to read about certain concepts before attempting to use them.

Maybe you already have some ideas for projects? If so, mention them here and maybe I can give more specific advice about whether it's feasible for a beginner and/or what you'll need to learn.

Some resources: https://wiki.python.org/moin/BeginnersG ... rogrammers
CheckIO seems interesting, maybe that interactive way of learning would help keep you engaged. Haven't tried it myself.

Oh and be prepared to spend many hours on this. It's not something you learn in a day. This is why I would favor methods of learning that focus on keeping the fun in it. Try to think of projects to work on that you are actually interested in completing.
User avatar
India TNT333
Lancer
Posts: 530
Joined: Sep 13, 2018
ESO: TNT333

Re: Learning Python

Post by TNT333 »

I know HTML
"We are kings or pawns" Napoleon Bonaparte
User avatar
Latvia harcha
Gendarme
Posts: 5141
Joined: Jul 2, 2015
ESO: hatamoto_samurai

Re: Learning Python

Post by harcha »

- Anybody here good with python?
Not me
- How did you get into python and why?
Some young professor taught it to us in my 2nd year of masters. The subject was completely unrelated, but he saw that it would be useless for us to teach that junk so he just decided to teach something useful - Python. He showed us the basics and how to apply those for gathering data from the web and doing simple operations for the purpose of data analysis.
- Has it been worth it?
Yeah, I actually used it to extract the data from long text files (logs) for my masters thesis practical part. Was pretty rad. Made that part much faster.
- any pointers for how i should go about learning this?
no idea mate
POC wrote:Also I most likely know a whole lot more than you.
POC wrote:Also as an objective third party, and near 100% accuracy of giving correct information, I would say my opinions are more reliable than yours.
India Ashvin
Retired Contributor
Posts: 2432
Joined: Jul 6, 2016
ESO: Octanium

Re: Learning Python

Post by Ashvin »

- Anybody here good with python?
Nope, no interest either.
- How did you get into python and why?
I wanted to check out different languages during my bachelor's degree and also wanted to work in different paradigms. I created some small neat programs with it, as well as my final year thesis project based on convolutional neural networks.
- Has it been worth it?
Well I don't write python professionally so no.
- any pointers for how i should go about learning this?
It depends on the learner, some people learn better with videos I am a documentation man myself. I read official docs and take it from there, pretty old school but works for me. You should try both and pick what suits you.
Image
User avatar
Great Britain Black_Duck
Dragoon
Posts: 347
Joined: Mar 4, 2018
ESO: Tom
Location: London
Clan: DUKC

Re: Learning Python

Post by Black_Duck »

Are you starting programming from scratch or do you know other languages already?
Challenged_Macro wrote: Respect Mussolini guys,if hes triggered there is always a reason.
Mussolini wrote: I hope corona gets you, your family and all your fuckin relatives as you fuckin deserve it for being cocky
User avatar
Great Britain I_HaRRiiSoN_I
Retired Contributor
Posts: 1626
Joined: Jan 15, 2016
Location: United Kingdom

Re: Learning Python

Post by I_HaRRiiSoN_I »

well i did a module in c language, bit of oracle/matlab but thats about it. The idea being python and R being the main languages
User avatar
Great Britain Riotcoke
Retired Contributor
ECL Reigning ChampsDonator 01
Posts: 4088
Joined: May 7, 2019
ESO: Riotcoke
Location: Dorsetshire
Clan: UwU

Re: Learning Python

Post by Riotcoke »

@I_HaRRiiSoN_I Can always do a conversion masters in CS, can be done quite cheaply if you go to the same uni you did your undergrad in and is basically a fast track way.
Image

twitch.tv/stangoesdeepTV
User avatar
Great Britain Black_Duck
Dragoon
Posts: 347
Joined: Mar 4, 2018
ESO: Tom
Location: London
Clan: DUKC

Re: Learning Python

Post by Black_Duck »

I_HaRRiiSoN_I wrote:well i did a module in c language, bit of oracle/matlab but thats about it. The idea being python and R being the main languages
Python shouldnt be too hard to pick up in that case. You just need a project to work and youll just pick it up in no time.
Challenged_Macro wrote: Respect Mussolini guys,if hes triggered there is always a reason.
Mussolini wrote: I hope corona gets you, your family and all your fuckin relatives as you fuckin deserve it for being cocky
France iNcog
Ninja
Posts: 13236
Joined: Mar 7, 2015

Re: Learning Python

Post by iNcog »

Bump

hey just dicking around with Python trying to learn some of it. and does anyone know what the hell is going on here.

I am trying to multiply two matrices together and in order to do so, I am setting up a blank, empty matrix for the result. Doing so manually (the A matrix) gives me the correct answer. Trying to set up a matrix that is sized appropriately with a little code (the Z matrix) seems to bug out the code. Even though A and Z matrices are literally the same and even python says so.
Attachments
qsdf.PNG
YouTube: https://www.youtube.com/incog_aoe
Garja wrote:
20 Mar 2020, 21:46
I just hope DE is not going to implement all of the EP changes. Right now it is a big clusterfuck.
User avatar
Great Britain I_HaRRiiSoN_I
Retired Contributor
Posts: 1626
Joined: Jan 15, 2016
Location: United Kingdom

Re: Learning Python

Post by I_HaRRiiSoN_I »

Probably want to use numpy package, either the numpy array or numpy matrix. In base python the problem you have is {2,3,4}*3 ={2,3,4,2,3,4,2,3,4}
User avatar
Latvia harcha
Gendarme
Posts: 5141
Joined: Jul 2, 2015
ESO: hatamoto_samurai

Re: Learning Python

Post by harcha »

i'm but a noob, but does the i j k of the second matrix multiplication "for" statement begin restart counting or have they remained at some higher value since the first multiplication?
POC wrote:Also I most likely know a whole lot more than you.
POC wrote:Also as an objective third party, and near 100% accuracy of giving correct information, I would say my opinions are more reliable than yours.
France iNcog
Ninja
Posts: 13236
Joined: Mar 7, 2015

Re: Learning Python

Post by iNcog »

harcha wrote:i'm but a noob, but does the i j k of the second matrix multiplication "for" statement begin restart counting or have they remained at some higher value since the first multiplication?
ohhh that's a really good thought.

I didn't think of that

however yeah it should reset because I tried to delete the lines of code for the A matrix and keep only Z and the result is pretty much the same

@I_HaRRiiSoN_I yeah I heard about the numpy pack. I'm going to import it once I need to crunch real numbers as opposed to little classroom examples, but just to get an understand for python coding, I wanted to do this without importing anything. thanks for the response though, how is your python coming along?
YouTube: https://www.youtube.com/incog_aoe
Garja wrote:
20 Mar 2020, 21:46
I just hope DE is not going to implement all of the EP changes. Right now it is a big clusterfuck.
User avatar
Great Britain chris1089
Retired Contributor
Posts: 2651
Joined: Feb 11, 2017
ESO: chris1089

Re: Learning Python

Post by chris1089 »

harcha wrote:i'm but a noob, but does the i j k of the second matrix multiplication "for" statement begin restart counting or have they remained at some higher value since the first multiplication?
I don't think that the I j and k used in the for loop are global values. I think they are local to the for loop and automatically get reset.

@iNcog idk why that's going wrong. I've only got one semester of python though.
User avatar
Netherlands Goodspeed
Retired Contributor
Posts: 13006
Joined: Feb 27, 2015

Re: Learning Python

Post by Goodspeed »

If the variables weren't reset you would get "index out of bounds" exceptions.

It looks like the way you're creating the Z array is causing this. It might be creating references to rather than copies of the first created array when you multiply it like that. Would be weird, but is the only possible cause I can see. The fact that the Z results in the 3 columns happen to be sums of the A results supports this.

EDIT:
>>> x = [0,0]
>>> y = 2 * [x]
>>> print(y)
[[0, 0], [0, 0]]
>>> y[0][0] += 1
>>> print(y)
[[1, 0], [1, 0]]

Both are changed because they both refer to the same value in memory.
No Flag RefluxSemantic
Gendarme
Posts: 5996
Joined: Jun 4, 2019

Re: Learning Python

Post by RefluxSemantic »

Code: Select all

i,j,k =  0 0 0
Z = [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
i,j,k = 0 0 1
Z = [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
i,j,k = 0 0 2
Z = [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
i,j,k = 0 1 0
Z = [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
i,j,k = 0 1 1
Z = [[1, 2, 0], [1, 2, 0], [1, 2, 0]]
i,j,k = 0 1 2
Z = [[1, 2, 0], [1, 2, 0], [1, 2, 0]]
i,j,k = 0 2 0
Z = [[1, 2, 0], [1, 2, 0], [1, 2, 0]]
i,j,k = 0 2 1
Z = [[1, 2, 0], [1, 2, 0], [1, 2, 0]]
i,j,k = 0 2 2
Z = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
i,j,k = 1 0 0
Z = [[3, 2, 3], [3, 2, 3], [3, 2, 3]]
i,j,k = 1 0 1
Z = [[3, 2, 3], [3, 2, 3], [3, 2, 3]]
i,j,k = 1 0 2
Z = [[3, 2, 3], [3, 2, 3], [3, 2, 3]]
i,j,k = 1 1 0
Z = [[3, 2, 3], [3, 2, 3], [3, 2, 3]]
i,j,k = 1 1 1
Z = [[3, 6, 3], [3, 6, 3], [3, 6, 3]]
i,j,k = 1 1 2
Z = [[3, 6, 3], [3, 6, 3], [3, 6, 3]]
i,j,k = 1 2 0
Z = [[3, 6, 3], [3, 6, 3], [3, 6, 3]]
i,j,k = 1 2 1
Z = [[3, 6, 3], [3, 6, 3], [3, 6, 3]]
i,j,k = 1 2 2
Z = [[3, 6, 8], [3, 6, 8], [3, 6, 8]]
i,j,k = 2 0 0
Z = [[5, 6, 8], [5, 6, 8], [5, 6, 8]]
i,j,k = 2 0 1
Z = [[5, 6, 8], [5, 6, 8], [5, 6, 8]]
i,j,k = 2 0 2
Z = [[5, 6, 8], [5, 6, 8], [5, 6, 8]]
i,j,k = 2 1 0
Z = [[5, 6, 8], [5, 6, 8], [5, 6, 8]]
i,j,k = 2 1 1
Z = [[5, 12, 8], [5, 12, 8], [5, 12, 8]]
i,j,k = 2 1 2
Z = [[5, 12, 8], [5, 12, 8], [5, 12, 8]]
i,j,k = 2 2 0
Z = [[5, 12, 8], [5, 12, 8], [5, 12, 8]]
i,j,k = 2 2 1
Z = [[5, 12, 8], [5, 12, 8], [5, 12, 8]]
i,j,k = 2 2 2
Z = [[5, 12, 15], [5, 12, 15], [5, 12, 15]]
It seems like due to the way you have assigned Z, Z[1] and Z[2] are a copy of Z[0]. So changing any of them changes all of them. This has something do to with assigning and python somethings getting annoying with it. It doesn't feel very transparent, but that's the price you pay for otherwise convenient languages like python.

EDIT: Actually, why the fuck am I getting a different result? nvm I made a small error
User avatar
Netherlands Goodspeed
Retired Contributor
Posts: 13006
Joined: Feb 27, 2015

Re: Learning Python

Post by Goodspeed »

All object-oriented languages have their quirks when it comes to assigning/copying by value vs by reference. There are plenty of use cases for both approaches, but most languages seem to default to doing it by reference these days.

For example in C#:

Code: Select all

public class Car {
public String Color { get; set; }
}

Car A = new Car { Color = "Red" };
Car B = A;
B.Color = "Blue";
Console.WriteLine(A.Color); // Prints "Blue"
The reason you would want to copy and assign things by reference becomes obvious when you start working with objects. For example say you have a game where you have a car object, and it has wheels. Say you have some bit of logic in your code that takes the front right wheel and tells it to deflate. You then want the car object that contains the wheel to also have a deflated front right wheel. You don't want the wheel to be stored separately, because then, if you deflate it, you have a deflated wheel in a vacuum but the car still has 4 good wheels. The wheel you're deflating should be a reference to the wheel in the car object, so that any changes you make to the wheel affect the car as well.

It's hard to explain that concept to someone who isn't used to working with objects in programming, but suffice to say this is not some weird bug in Python but intended behavior. It is a bit unintuitive in this case though.
No Flag RefluxSemantic
Gendarme
Posts: 5996
Joined: Jun 4, 2019

Re: Learning Python

Post by RefluxSemantic »

Of course I never said it was a weird bug. It's just annoying in python because these things happen more implicitly than for example in C. So sometimes unexpected and unintuitive things happen. Using the same code in C, it'd have been far easier to spot this mistake as the error needs to be explicitly coded like that.
France iNcog
Ninja
Posts: 13236
Joined: Mar 7, 2015

Re: Learning Python

Post by iNcog »

Goodspeed wrote:If the variables weren't reset you would get "index out of bounds" exceptions.

It looks like the way you're creating the Z array is causing this. It might be creating references to rather than copies of the first created array when you multiply it like that. Would be weird, but is the only possible cause I can see. The fact that the Z results in the 3 columns happen to be sums of the A results supports this.

EDIT:
>>> x = [0,0]
>>> y = 2 * [x]
>>> print(y)
[[0, 0], [0, 0]]
>>> y[0][0] += 1
>>> print(y)
[[1, 0], [1, 0]]

Both are changed because they both refer to the same value in memory.
ohhhhh. so yeah it's just the same array three times?

tricky, but I think I get it. dimly. but then how to make it so that the three arrays are independent from one another? perhaps creating three small sub-arrays that are different from one another

my understanding of programming is obviously limited but I want to try to solve this

numpy, btw, makes everything super easy.
YouTube: https://www.youtube.com/incog_aoe
Garja wrote:
20 Mar 2020, 21:46
I just hope DE is not going to implement all of the EP changes. Right now it is a big clusterfuck.
No Flag RefluxSemantic
Gendarme
Posts: 5996
Joined: Jun 4, 2019

Re: Learning Python

Post by RefluxSemantic »

iNcog wrote:
Goodspeed wrote:If the variables weren't reset you would get "index out of bounds" exceptions.

It looks like the way you're creating the Z array is causing this. It might be creating references to rather than copies of the first created array when you multiply it like that. Would be weird, but is the only possible cause I can see. The fact that the Z results in the 3 columns happen to be sums of the A results supports this.

EDIT:
>>> x = [0,0]
>>> y = 2 * [x]
>>> print(y)
[[0, 0], [0, 0]]
>>> y[0][0] += 1
>>> print(y)
[[1, 0], [1, 0]]

Both are changed because they both refer to the same value in memory.
ohhhhh. so yeah it's just the same array three times?

tricky, but I think I get it. dimly. but then how to make it so that the three arrays are independent from one another? perhaps creating three small sub-arrays that are different from one another

my understanding of programming is obviously limited but I want to try to solve this

numpy, btw, makes everything super easy.
You could do something with append I suppose. First create an empty list and then append whatever you want it to eventually contain. That's what I tend to do with this stuff.
(although I only code hot steaming crap in python that gives me a result asap even if it's achieved in a very shitty way, so there might be even better ways.
No Flag RefluxSemantic
Gendarme
Posts: 5996
Joined: Jun 4, 2019

Re: Learning Python

Post by RefluxSemantic »

RefluxSemantic wrote:
iNcog wrote:
Goodspeed wrote:If the variables weren't reset you would get "index out of bounds" exceptions.

It looks like the way you're creating the Z array is causing this. It might be creating references to rather than copies of the first created array when you multiply it like that. Would be weird, but is the only possible cause I can see. The fact that the Z results in the 3 columns happen to be sums of the A results supports this.

EDIT:
>>> x = [0,0]
>>> y = 2 * [x]
>>> print(y)
[[0, 0], [0, 0]]
>>> y[0][0] += 1
>>> print(y)
[[1, 0], [1, 0]]

Both are changed because they both refer to the same value in memory.
ohhhhh. so yeah it's just the same array three times?

tricky, but I think I get it. dimly. but then how to make it so that the three arrays are independent from one another? perhaps creating three small sub-arrays that are different from one another

my understanding of programming is obviously limited but I want to try to solve this

numpy, btw, makes everything super easy.
You could do something with append I suppose. First create an empty list and then append whatever you want it to eventually contain. That's what I tend to do with this stuff.
(although I only code hot steaming crap in python that gives me a result asap even if it's achieved in a very shitty way, so there might be even better ways.

Code: Select all

X = [[1,2,3],[2,4,5],[2,6,7]]

Y = [[1,0,0],[0,1,0],[0,0,1]]

n = len(X)
m = len(Y[0])

zero = [0]
Z = [zero*m]*n

functioning_Z = []

for i in range(len(X)):
z = []
for i in range(len(Y[0])):
z.append(0)
functioning_Z.append(z)

print(functioning_Z)

A = [[0,0,0],[0,0,0],[0,0,0]]

print (A==Z)

for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
A[i][j] += X[i][k] * Y[k][j]

for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
Z[i][j] += X[i][k] * Y[k][j]

for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
functioning_Z[i][j] += X[i][k] * Y[k][j]

print("A = ", A)
print("Z = ", Z)
print("functioning_Z = ", functioning_Z)
France iNcog
Ninja
Posts: 13236
Joined: Mar 7, 2015

Re: Learning Python

Post by iNcog »

getting closer

Code: Select all


X = [[1,2,2],[2,4,5],[2,6,7]] 

Y = [[1,0,0],[0,1,0],[0,0,1]]

n = len(X)
m = len(Y[0])
print("result matrix rows, n is: ", n)
print("result matrix columns, m is: ", m)

Z = []
z1 = []
z2 = []
z3 = []

for i in range(m):
z1.append(0)
z2.append(0)
z3.append(0)

Z.append(z1)
Z.append(z2)
Z.append(z3)
print(Z)

A = [[0,0,0],[0,0,0],[0,0,0]]
print ("A before operation is: "), print(A)

print("A and Z are the same matrix: ",A==Z)

for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
A[i][j] += X[i][k] * Y[k][j]

print("the A result matrix is: ")
for a in A:
print(a)

for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
Z[i][j] += X[i][k] * Y[k][j]

print("the Z result matrix is: ")
for z in Z:
print(z)
YouTube: https://www.youtube.com/incog_aoe
Garja wrote:
20 Mar 2020, 21:46
I just hope DE is not going to implement all of the EP changes. Right now it is a big clusterfuck.
France iNcog
Ninja
Posts: 13236
Joined: Mar 7, 2015

Re: Learning Python

Post by iNcog »

RefluxSemantic wrote:
iNcog wrote:
Goodspeed wrote:If the variables weren't reset you would get "index out of bounds" exceptions.

It looks like the way you're creating the Z array is causing this. It might be creating references to rather than copies of the first created array when you multiply it like that. Would be weird, but is the only possible cause I can see. The fact that the Z results in the 3 columns happen to be sums of the A results supports this.

EDIT:
>>> x = [0,0]
>>> y = 2 * [x]
>>> print(y)
[[0, 0], [0, 0]]
>>> y[0][0] += 1
>>> print(y)
[[1, 0], [1, 0]]

Both are changed because they both refer to the same value in memory.
ohhhhh. so yeah it's just the same array three times?

tricky, but I think I get it. dimly. but then how to make it so that the three arrays are independent from one another? perhaps creating three small sub-arrays that are different from one another

my understanding of programming is obviously limited but I want to try to solve this

numpy, btw, makes everything super easy.
You could do something with append I suppose. First create an empty list and then append whatever you want it to eventually contain. That's what I tend to do with this stuff.
(although I only code hot steaming crap in python that gives me a result asap even if it's achieved in a very shitty way, so there might be even better ways.
Yeah I had that idea, though wasn't sure how to create an empty list that wasn't a reference without needing to manually do it. Gonna look at your code here

edit: nice one, it worked! that's pretty neat. I just need to understand what it is your code is doing. I indeed went with append but don't understand enough about that function to understand what it does
YouTube: https://www.youtube.com/incog_aoe
Garja wrote:
20 Mar 2020, 21:46
I just hope DE is not going to implement all of the EP changes. Right now it is a big clusterfuck.
User avatar
Korea South Vinyanyérë
Retired Contributor
Donator 06
Posts: 1839
Joined: Aug 22, 2016
ESO: duolckrad, Kuvira
Location: Outer Heaven
Clan: 팀 하우스

Re: Learning Python

  • Quote

Post by Vinyanyérë »

iNcog wrote:
Goodspeed wrote:If the variables weren't reset you would get "index out of bounds" exceptions.

It looks like the way you're creating the Z array is causing this. It might be creating references to rather than copies of the first created array when you multiply it like that. Would be weird, but is the only possible cause I can see. The fact that the Z results in the 3 columns happen to be sums of the A results supports this.

EDIT:
>>> x = [0,0]
>>> y = 2 * [x]
>>> print(y)
[[0, 0], [0, 0]]
>>> y[0][0] += 1
>>> print(y)
[[1, 0], [1, 0]]

Both are changed because they both refer to the same value in memory.
ohhhhh. so yeah it's just the same array three times?

tricky, but I think I get it. dimly. but then how to make it so that the three arrays are independent from one another? perhaps creating three small sub-arrays that are different from one another

my understanding of programming is obviously limited but I want to try to solve this

numpy, btw, makes everything super easy.
List comprehensions are one way; be warned that a side effect of using list comprehensions is that you turn into a giga chad

Code: Select all


>>> x = [0 for _ in range(4)]
>>> x
[0, 0, 0, 0]
>>> x[2] = 7
>>> x
[0, 0, 7, 0]

>>> y = [[0 for m in range(3)] for n in range(2)]
>>> y
[[0, 0, 0], [0, 0, 0]]
duck
:mds:
imo
No Flag RefluxSemantic
Gendarme
Posts: 5996
Joined: Jun 4, 2019

Re: Learning Python

Post by RefluxSemantic »

Vinyanyérë wrote:
iNcog wrote:
Goodspeed wrote:If the variables weren't reset you would get "index out of bounds" exceptions.

It looks like the way you're creating the Z array is causing this. It might be creating references to rather than copies of the first created array when you multiply it like that. Would be weird, but is the only possible cause I can see. The fact that the Z results in the 3 columns happen to be sums of the A results supports this.

EDIT:
>>> x = [0,0]
>>> y = 2 * [x]
>>> print(y)
[[0, 0], [0, 0]]
>>> y[0][0] += 1
>>> print(y)
[[1, 0], [1, 0]]

Both are changed because they both refer to the same value in memory.
ohhhhh. so yeah it's just the same array three times?

tricky, but I think I get it. dimly. but then how to make it so that the three arrays are independent from one another? perhaps creating three small sub-arrays that are different from one another

my understanding of programming is obviously limited but I want to try to solve this

numpy, btw, makes everything super easy.
List comprehensions are one way; be warned that a side effect of using list comprehensions is that you turn into a giga chad

Code: Select all


>>> x = [0 for _ in range(4)]
>>> x
[0, 0, 0, 0]
>>> x[2] = 7
>>> x
[0, 0, 7, 0]

>>> y = [[0 for m in range(3)] for n in range(2)]
>>> y
[[0, 0, 0], [0, 0, 0]]
This method is very chad indeed.

Who is online

Users browsing this forum: No registered users and 18 guests

Which top 10 players do you wish to see listed?

All-time

Active last two weeks

Active last month

Supremacy

Treaty

Official

ESOC Patch

Treaty Patch

1v1 Elo

2v2 Elo

3v3 Elo

Power Rating

Which streams do you wish to see listed?

Twitch

Age of Empires III

Age of Empires IV