This question already has answers here:
How Pytorch Tensor get the index of specific value
(10 answers)
Closed 3 years ago.
I have 2 Tensors named x and list and their definitions are below:
x = torch.tensor(3)
list = torch.tensor([1,2,3,4,5])
Now I want to get the index of element x from list. The expected output is an Integer:
2
How can I do in an easy way?
import torch
x = torch.tensor(3)
list = torch.tensor([1,2,3,4,5])
idx = (list == x).nonzero().flatten()
print (idx.tolist()) # [2]
list = torch.tensor([1,2,3,3,5])
idx = (list == x).nonzero().flatten()
print (idx.tolist()) # [2, 3]
Related
This question already has answers here:
python: replace elements in list with conditional
(3 answers)
Replace values in list using Python [duplicate]
(7 answers)
Finding and replacing elements in a list
(10 answers)
Closed 1 year ago.
I have this list
x = [1,2,3,4,5,6]
I would like to convert elements greater than 3 as 0 so the list would become [1,2,3,0,0,0]
Here is my code
for i in range(len(x)):
if x[i] > 3:
x = 0
I got this error message " 'int' object is not subscriptable". could somebody tell me what I did wrong
Problem with your current code is, you are assigning 0 directly to x, not to the item at a particular index, the same way you are comparing the value.
for i in range(len(x)):
if x[i] > 3:
# x = 0
x[i] = 0 #<---- assign value at the index i
Or, you can just use a list-comprehension, and take 0 if the value is greater than 3, else take the value
>>> [0 if i>3 else i for i in x]
[1, 2, 3, 0, 0, 0]
This question already has answers here:
Fastest way to check if a value exists in a list
(11 answers)
Closed 1 year ago.
I have a long list of values and want a list comprehension to evaluate to True (and print "True" only once if any value in the list is the integer 1).
I can print "True" for each instance a 1 is found but cannot see how to just have it return a single True.
Code
a = [0,0,1,1,0,1]
b = [print("True") for i in a if i == 1]
print('\n')
#c = [print("True") if any i in a is True] # doesn't work, syntax error
d = [print("TRUE") if any(i == 1)]
Did you mean to convert the resulting list to bool()?
a = [0,0,1,1,0,1]
b = bool([i for i in a if i == 1])
print(b)
if your list only contains zeros and ones you could just print(any(a))
otherwise you could do this
a = [0,0,1,0,2,0]
b =[x==1 for x in a]
print(any(b))
returns True
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 2 years ago.
Why is my function 'increment' return different values for matrix that i create by other function and different for manual matrix?
n = 2
m = 3
indices = [[0,1],[1,1]]
def matrixpopulation(n,m):
row=[]
matrix=[]
row+=(0 for _ in range(0,m))
matrix+=(row for _ in range(0,n))
return matrix
def increment(indices,matrixa):
for v,k in indices:
for i in range(3):
matrixa[v][i]+=1
for i in range(2):
matrixa[i][k]+=1
return matrixa
matrixa=matrixpopulation(n,m)
filled_matrix=increment(indices,matrixa)
print(matrixpopulation(n,m))
print(filled_matrix)
manualmatrix=[[0,0,0],[0,0,0]]
print(manualmatrix)
print(increment(indices,manualmatrix))
matrix+=(row for _ in range(0,n))
When you make matrix here you actually add the reference to the same row n-times. When you modify some element in one 'row', all other rows are modified as well. For example:
a = [1, 2]
b = [a, a]
a[0] = 3
Check b now.
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 3 years ago.
I have a list of dataframe variables:
B = [df1,df2,df3,df4]
I am running a for loop like below:
for element in B:
element + '_norm' = (element - element.mean())/ (element.max() -
element.min())
How do I write the variable name so that I may reference the normalized dataframe? That is to say I would like to be able to define the normalized dataframe 3 using the variable "df3_norm". How can I write the variable into the for loop so that the "df3_norm" format can be referenced?
Use dict:
df_dict = {}
for i, element in enumerate(B):
df_dict['df%i_norm' % i] = (element - element.mean())/(element.max() - element.min())
BTW, normalization involves subtracting min from elements not mean.
If you want normalization:
df_dict['df%i_norm' % i] = (element - element.min())/(element.max() - element.min())
# ^^^^^
Else if you want standardization:
df_dict['df%i_norm' % i] = (element - element.mean())/(element.std())
This question already has answers here:
Randomly extract x items from a list using python
(3 answers)
Closed 8 years ago.
Given an array x = [2,3,5,4,1,7,4,2,8] I am looking to create a second array y which is a length p and consists of a random election of the elements within x. Is there an easier way other than doing the following
x = [2,3,5,4,1,7,4,2,8]
random.shuffle(x)
p = 5
y = x[0:p]
print y
Use random.sample:
x = [2,3,5,4,1,7,4,2,8]
y = random.sample(x, p)