Selecting Randomly within an Array [duplicate] - python

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)

Related

Why is the index not being incremented by 2 positions in this for loop? [duplicate]

This question already has answers here:
How to change for-loop iterator variable in the loop in Python?
(6 answers)
Updating Index Value in For Loop
(2 answers)
Closed 1 year ago.
I have a similar code that needs incrementing and while loop cant be used there,
m = range(10)
for i in range(len(m)):
print(i)
i+=2
Do it like this:
m = range(10)
nb = 0
for i in range(len(m)):
print(nb)
nb+=2
The problem is that you wanted to use i for two different tasks.

How Pytorch Tensor get the index of elements? [duplicate]

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]

How to have a list with all the same items, but the length be equal to and equation [duplicate]

This question already has answers here:
Create list of single item repeated N times
(9 answers)
Closed 6 years ago.
I'm wondering how to have a list with all the same items, but the length be equal to and equation, for example:
myList = 'a' * 10
but this gives you
myList = 'aaaaaaaaaa'
when I want
myList = a, a, a, a, etc.
This looks like the shortest way:
['a']*10

Iterate through multiple lists at the same time [duplicate]

This question already has answers here:
How do I iterate through two lists in parallel?
(8 answers)
Closed 7 years ago.
Is it possible to iterate through multiple lists and return arguments from different lists within the same loop?
I.e., Instead of -
For x in trees:
Print(x)
For y in bushes:
Print(y)
Something like -
For x,y in trees,bushes:
Print(x +"\n"+ y)
You can simply use zip or itertools.izip:
for x, y in zip(trees, bushes):
print x, y
You can use zip():
a=['1','2','2']
b=['3','4','5']
for x,y in zip(a,b):
print(x,y)
output:
1 3
2 4
2 5

I want to deal with matrices without using numpy [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 8 years ago.
I want to make my own matrix solution code. I having problem when taking values. my code is below:
ask_r = int(raw_input("How many rows: "))
ask_c = int(raw_input("How many columns: "))
"""
[1,2,3]
[3,4,5]
[7,8,9]
"""
"""
matrix = [[1,2,3],[4,5,6],[7,8,9]]
"""
col_m = [0]*ask_c
row_m = [col_m]*ask_r
for i in range(ask_r):
for j in range(ask_c):
val = input("Put in: ")
row_m[i][j] = val
print row_m
What's wrong there as the input changes the value of all the three inner lists(considered as columns).
row_m = [col_m]*ask_r creates a list of ask_r times the same reference to col_m.
You are looking for something like
row_m = [[0]*ask_c for _ in range(ask_r)]

Categories