Related
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 7 years ago.
s is a list of lists of integers with all values initialized to zero. I would like to increment only the first element of the first row by one, but the following command increments the first element of every row by one. How may I achieve this?
In [6]: s = [[0]*4]*4
In [7]: s[0][0] += 1
In [8]: s
Out[8]:
[[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0]]
Okay! Thanks for the advice, the problem was in my construction of s.
If s is truly a list of lists (and not a list containing multiple references to the same list), what you did works, your issue must be elsewhere
>>> s = [[0, 0, 0, 0, 0],
... [0, 0, 0, 0, 0],
... [0, 0, 0, 0, 0],
... [0, 0, 0, 0, 0],
... [0, 0, 0, 0, 0]]
>>> s[0][0]
0
>>> s[0][0] = 1
>>> s
[[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
You made your lists "incorrectly" in the first place; each element of your list simply points to the same single list. When you update that list they all update.
Make the list of lists using code something like this instead:
s = [[0 for _ in range(5)] for _ in range(5)]
This is classical Python oversight since lists assignments are done by references not by deep copy.
For example if you constructed using this way that's where it would have gone wrong.
>>> zeros = [0,0,0,0]
>>> s = [zeros,zeros,zeros,zeros]
>>> s[0][0]+=1
>>> s
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
So while copying lists use as below
>>> s = [list(zeros), list(zeros), list(zeros), list(zeros)]
>>> s[0][0]+=1
>>> s
[[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
I have a list of ground truth labels:
yTrue = ['class2','classC','class3','class3','classA','classB','class2']
and a list of the possible classes (distinct, in custom order):
orderedClasses = ['classA','class2','classB','class3','classC']
I want to code the list in One-Vs-The-Rest for all possible classes.
Desired output:
[[0,1,0,0,0],[0,0,0,0,1],[0,0,0,1,0],[0,0,0,1,0],[1,0,0,0,0],[0,0,1,0,0],[0,1,0,0,0]]
I tried to use sklearn.preprocessing.label_binarize (doc) but the problem is it doesn't maintain my custom order for classes:
[[0,0,1,0,0],[0,0,0,0,1],[1,0,0,0,0],[1,0,0,0,0],[0,0,0,1,0],[0,1,0,0,0],[0,0,1,0,0]]
Looking for an Pythonic and efficient way to get the desired output
Simply pass orderedClasses as classes parameter
In [15]: label_binarize(yTrue, orderedClasses)
Out[15]:
array([[0, 1, 0, 0, 0],
[0, 0, 0, 0, 1],
[0, 0, 0, 1, 0],
[0, 0, 0, 1, 0],
[1, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 1, 0, 0, 0]])
I'm trying to print a list as a table
The following code creates specified amount of rows and columns (in other words, it creates a list of a specified size).
finalgrid1 =[[0 for j in range(keyword_size)] #print rows (size of keyword)
for i in range(1 + new_grid_size)] #print column (size of ciphers)
print(finalgrid1)
The result printed is (when keyword_size = 3):
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Could someone show me how to print it in the following format.
[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
I'm working on creating a Cipher encoder and decoder program as part of an assignment.
I've implemented the majority of it already - just stuck with printing it in the right format (as a table).
Btw, I am not asking you to apply any changes whatsoever, I only want to know how to print the list in the format of a table, that's all.
I appreciate your help
Try this, essentially prints each list out until there are none left to print
i = 0
while i < len(finalgrid1):
print finalgrid1[i]
i += 1
Stringify each list and join them together:
>>> grid = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>>
>>> print('\n'.join(str(x) for x in grid))
[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]
Hi there i'm having troubles while copying arrays. I know there is really good posts about this but they dont resolve my problems.
First of all Im trying to copy nested arrays so the copy using slice do not work:
new_list = old_list[:]
This does not work when used with nested arrays, and I understand why it doesn't.
For my purposes i need to work with nested arrays so i've been using:
new_list = list(old_list)
This does copy correctly nested arrays but has a strange behavior when used inside a method. Here is an example code:
Edited Code:
def copy_and_reset(data):
import copy
events_data=list(data)
reset_to_0(data)
return events_data
def reset_to_0(the_array):
for i, e in enter code hereenumerate(the_array):
if isinstance(e, list):
reset_to_0(e)
else:
the_array[i] = 0
a=[[1,1,1,1,1],[2,2,2,2,2,2],[3,3,3,3,3,3],[4,4,4,4]]
b=copy_and_reset(a)
print a
print b
b=list(a)
a.append([22,22])
print a
print b
And here is the output:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0], [22, 22]]
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0]]
Some idea what happens there? The only way to correctly copy an array inside a method is:
new_list = copy.deepcopy(old_list)
When you are copying the list you are just creating a reference to the lists contained in A. Here is a visualaztion of whats happening:
If you want to step though and view whats happening you should check out PythonTutor
I have to create a matrix (or an array) from a string file like this:
>>>print Qval
[1*10**4,0,0,0],[0,1,0,0],[0,0,1*10**3,0], [0,0,0,1]
>>>print type(Qval)
<type 'str'>
I have to get the matrix this way, because in my Tkinter GUI I have to input the value like this (part of the interface: field label= default value):
Q-Matrix= [1*10**4,0,0,0],[0,1,0,0],[0,0,1*10**3,0],[0,0,0,1]
I get the Qval string from my interface like this:
Qval=vars[13].get()
Is there a proper way to do this?
Thanks.
>>> ast.literal_eval('[1e4, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1e3, 0], [0, 0, 0, 1]')
([10000.0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1000.0, 0], [0, 0, 0, 1])
you can use the built-in function eval like that:
a ="[1*10**4,0,0,0],[0,1,0,0],[0,0,1*10**3,0], [0,0,0,1]"
mat = eval(a)