Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I was doing a project in python and I would like to create a global variable which must be a two-dimensional array, but the dimension must be assigned when the user enters it.
how can i declare it initially? and then resize it when I want?
thanks for your attention
You can declare two-dimensional array doing simply this:
a = [[]]
If you print it out, you're gonna get something like this: [[]] which is two-dimensional array. Then you can append to this array and get elements using a[y][x]
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I have some Python code that takes user input for mass and diameter of an object. Those data parameters I later use in a calculation formula. Instead of having the user input those two values, I now have a dictionary that stores the values. How can I now have those values extracted from the dictionary and placed into the calculation formula? I'm very new to Python and trying to figure out if this done via a while loop or defining an function within a while loop, etc.
Thanks.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am struggling to multiply lists as matrices in Python.
I have two lists (weights and returns) and I need to multiply them as: weights*TRANSPOSE(returns).
How are Weights and Return defined in your code?
You might be able to do the following:
#This sums the entries
matrixProduct = 0
for i in range(len(Weights)):
matrixProduct+= Weights[i]*Return[i]
#In case you meant to keep products of individual pairs of matrix entries (not sure from your notation):
matrixProduct = []
for i in range(len(Weights)):
matrixProduct.append(Weights[i]*Return[i])
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am working on a machine learning task and trying to convert all strings in a set of data to floats using hash() to do this I need to iterate over all the elements of a numpy array whilst not knowing if it is a 2D 3D or 4D array and then change each element. Is there any way to do this without using nested loops?
You can try numpu.vectorize, already mentioned here
Note:
The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop.here
arr = np.array([['aba', 'baa', 'bbb'],
['xxy', 'xyy', 'yyy']])
v_hash = np.vectorize(hash)
v_hash(arr)
array([[-1538054455328520296, -1528482088733019667, -7962229468338304433],
[ 5621962119614158870, 1918700875003591346, -3216770211373729154]])
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have list, its elements are also a list.
eg:
outlier=[[20,2,67], [90,6,12], [23,16,7]].
how I write a single line code to loop outlier list by taking elements from same index.I want to store the looping result as a list.
The question is not very clear to me, but if you want to make a new list that contains the first element of each list you have, then this is an example to take first element(x[0]) from each list and store them in a new list called "new_list":
new_list = [x[0] for x in outlier]
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 3 years ago.
Improve this question
This is just a question and I don't have any example code. But let's say that you were to make a python program were you have a "star" variable the star variable holds an integer and that integer is the age of the star, each time the program loops it adds a value of 1 to the star variable and if the star variable is equal to 100 it will create a new star which has its variable and the same process will occur with this variable as well.
How would you create a whole new variable or something along these lines without assigning one while writing the program?
You could store the object in a list. As a new star is created, just append it.