Arrays being re-assigned (Python) [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm recreating Conway's game of life in pygame, and I've been having an issue implementing the preset feature. When I'm assigning the preset array to the default array, and then assigning it back to the default array again, the preset is changed. I'm only editing the array "Initial_frame" but it is somehow changing the preset array.
Code:
https://paste.pythondiscord.com/uvetekikot.py

You are probably doing something like this:
a = [1 2 3 4 5]
b = a
a and b are really arrays, rather they are references to arrays. b=a means they both reference the same array.
You will need to copy the array instead of just copying the reference, like this:
b = a[:]

Related

List append in classes [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I am making a Go game and I've stumbled across a problem with my list.append() method.
Every time a player makes a move I want to append the board (2d list) to my ex_boards list. With every iteration of my for loop, instead of appending the current board to my list, it appends it and changes every past board to the current one in the list, something like ex_boards[every index possible in the list] = current_board. So instead of having every state of the game in my list, I have n (len of the list) times the last updated board.
What don't I understand about classes? Also, my list is right below Class Go.
Try using
ex_boards.append(copy.deepcopy(board))

Combining two array in python with np [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have two array one with shape (320,1) and one with (850,1) how should I combine the two array. I tried np.append however it seems to be an error.
Assuming the two arrays are called arr1 and arr2, you could try the following code:
arr3 = np.append(arr1,arr2).reshape((-1,1))
The reshape is needed to make sure the final shape is (1170,1) instead of (1170,)

Element wise multiplication using Numpy [Python] [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm trying to get the element-wise mulitplication using normal *, and i tried np.multiply(), both give a weird answers.
now (1-y) is (100,) and np.log(1-sigmoid(np.dot(X,theta)))) is (100,1), so when i muliply them by element-wise, it should give (100,1); but it gives me (100,100) matrix(ALL are higlighted BLUE)
Here is my original function if it can help.
Can anyone help me with getting the source of erre here?
I'm not 100% sure why python does this, but the way to solve it would be to apply np.reshape((1-y),(100,1)) and then apply np.multiply(). In general it's always better to reshape your arrays and to give them a second dimension.
EDIT: This explains how numpy does the broadcasting when using an array of dimensions (n,) and (n,1).

Difference beetween C arrays and Python lists [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I ask this because i'm a beginner in CS and i have this doubt. So it would be nice if peeps try to explain instead of just make humor or downvote. I tought this site was for learning from others.
Is just a different name or there something different underlying it?
Can anyone briefly explain the difference beetween C arrays and Python lists?
A Python list is essentially an array of object references that keeps track of how many elements it contains.
(Conceptually, a Python reference is somewhat similar to a C pointer.)
Read more here: Internals of Python list, access and resizing runtimes

In Python, how do you make a loop? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have started learning Python and was wondering how to make a loop, I cant find a website that has it so if you knew it would be great!
I assume you mean a for loop. You do it like so:
for i in range(10):
print i
This is the simplest way. You can use xrange rather than range for very long loops to help with memory management.
Also there are ways to iterate through iterable objects (like lists, arrays, strings etc.):
a=[1,2,3]
for something in a:
print something
You could also use comprehension to make the code shorter, but that's a bit more advanced and you probably don't need it just yet. Should you so wish to learn what it's about this looks OK for a start.
There are many ways.
while True:
That loops until you put break on a line.
The two Aleksander Lidtke mentioned.
for x in range(10): #Loops through the following code 10 times
And:
x = [1,2,3,4]
for items in x: #the variable items is assigned to
#each variable as it iterates through
To learn more about python, read this book.

Categories

Resources