Python - changing element value of a list in list [duplicate] - python

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 7 years ago.
I have a list in list. All the values are False. I have to change value to True of a very specific one.
s=[[False,False,False,False]
[False,False,False,False]
[False,False,False,False]
[False,False,False,False]]
I want to change it into this:
s=[[False,False,False,False]
[False,False,False,False]
[False,True,False,False]
[False,False,False,False]]
P.s.:
I tried doing this:
s[2][1]=True
But i got this:
[[False,True,False,False]
[False,True,False,False]
[False,True,False,False]
[False,True,False,False]]

Add commas in order to build your matrix.
s=[[False,False,False,False],
[False,False,False,False],
[False,False,False,False],
[False,False,False,False]]
s[2][1]=True
for i in s:
print i
Outputs:
[False, False, False, False]
[False, False, False, False]
[False, True, False, False]
[False, False, False, False]

Related

python lists with for loop and if-else statement [duplicate]

This question already has answers here:
if/else in a list comprehension
(12 answers)
Closed 1 year ago.
I want to use the one lined syntax for the "for_loop/if/elif/else/lists" for the code under:
for i in range(20):
if(i<15):
print("True")
else:
print("False")
this is a part of it
[True for i in range(20) if i<15]
How to add the "else" for it?
Try it.
s = [True if i<15 else False for i in range(20)]
print(s)
output
[True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, False, False, False, False, False]

Why do '*' vs list comprehension when making list of lists work differently? [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 2 years ago.
My purpose was to make 2D list by *
list2 = [[False]*2]*3 #[[False,False],[False,False],[False,False]]
list3 = [[False]*2 for _ in range(3)] #same as list2
list2[0][0] = True # [[True, False], [True, False], [True, False]]
list3[0][0] = True # [[True, False], [False, False], [False, False]]
list3 works well but list2 doesn't. list2 is affected by 'x' of list2[z][x].
What happened?
* operator concats the list and makes same replica or reference of the list so change made at one place will get replicated at all places whereas for _ range(3) will evaluate one by one hence making just one change at position list[0][0]

Combining boolean masks, why is in Python: [False, True] and [True, False] == [True, False] [duplicate]

This question already has answers here:
What is Truthy and Falsy? How is it different from True and False?
(8 answers)
Closed 2 years ago.
I was busy with combining masks and it strikes me that:
>>> [False, True] and [True, False]
[True, False]
and
>>> [True, False] and [False, True]
[False, True]
Why is this the case? Why shouldn't I expect it to be [False, False] in both cases?
I also think this is why np.logical_and() exists in Numpy:
>> np.logical_and([True, False], [False, False])
array([False, False])
This is the same reason why:
>>> (1,2) and (3,4)
(3, 4)
You need to understand that this is not doing element-wise comparison as opposed to np.logical_and.
The way and works is, if you have a and b, it checks whether a is False, if yes, return a else return b, does not matter what the value of b is.
In your case [False, True] is not False:
>>> bool([False, True])
True
Because it is a non-empty list, even [False, False] is True.
So in the case of [False, True] and [True, False], it checks to see whether [False, True] is False, which it is not, so it returns the second value. Same for the other case.
A python implementation of the and or logic would be:
def AND(first, second):
if bool(first) == True:
return second
else:
return first
def OR(first, second):
if bool(first) == True:
return first
else:
return second

Nested List declaration [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 3 years ago.
mytest=[[False]*3]*2
In [46]: mytest
Out[46]: [[False, False, False], [False, False, False]]
In [47]: mytest[0][1]=True
In [48]: mytest
Out[48]: [[False, True, False], [False, True, False]]
On the other hand
mytest=[ [False]*3 for i in range(2)]
In [53]: mytest[0][1]=True
In [54]: mytest
Out[54]: [[False, True, False], [False, False, False]]
On the first when in set [0][1], it sets at two places , but in second it sets correctly .. what is wrong with first assignment.
This is how Python handles objects. In your first example, the list mytest contains two [False, False, False] lists that are stored at the same memory location (i.e., both items in the list are pointing to the same memory location). When you change the one, the other is changed as well because simply they are both pointing to the same list in memory.
In the second example, and when you are using list comprehension the two lists [False, False, False] are two different objects pointing to different memory locations.
Proof
>>> mytest=[[False]*3]*2
>>> id(mytest[0])
4340367304
>>> id(mytest[1])
4340367304
>>> mytest=[ [False]*3 for i in range(2)]
>>> id(mytest[0])
4340436936
>>> id(mytest[1])
4340498512
The difference with the first and second statement is that your first statement will first evaluate [False] * 3 first which gives [False, False, False] and then *2 will create two references of that object ([False, False, False]). In the second example, you are creating a [False, False, False] each time.

Numpy indexing in python [duplicate]

This question already has answers here:
Mask out specific values from an array
(3 answers)
Closed 5 years ago.
Here is the deal,
idx_arr = [0,3,5,7];
tgt_arr = [
[0,3,3,5,5,6,6],
[1,1,3,1,1,3,3],
[2,4,6,8,1,2,9]]
I want to make new array with bool type that would look like. I tried also with sets but numpy.ndarrays are unhashable types. New matrix would look like
final_arr = [
[t,t,t,t,t,f,f],
[f,f,t,f,f,t,t],
[f,f,f,f,f,f,f]]
Thanks in advance.
Using base Python:
[[True if val in idx_arr else False for val in row] for row in tgt_arr]
Result:
[[True, True, True, True, True, False, False],
[False, False, True, False, False, True, True],
[False, False, False, False, False, False, False]]

Categories

Resources