randomly selecting items from an array python [duplicate] - python

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I randomly select an item from a list using Python?
I have two arrays pool_list_X , pool_list_Y. Both have a numpy array as element in the list.
So basically
pool_list_x[0] = [1 2 3 4] # a multidimensional numpy array.
and every element of pool_list_x has corresponding element in pool_list_y
which is to say, that pool_list_x[i] corresponds to pool_list_y[i]
Now. if I have to randomly select 10 elements from list_x (and hence the corresponding elements to list_y). how do i do this.
I can think of a very naive way.. randomly generate numbers. and stuff.. but that is not very efficient.. what is the pythonic way to do this.
Thanks

Not sure if I understand you one hundred percent, but I think using zip and random.sample might work:
import random
random.sample(zip(list_a,list_b), 10)
Some short explanations:
zip will create a list of pairs, i.e. it ensures that you pick corresponding elements - if you pick one, you automatically get the other (Zip([1,2,3],[4,5,6]) = [(1,4),(2,5),(3,6)])
random.sample(l,n) randomly selects n elements from a list l

There is a function allowing you to get the random element of the given sequence:
import random
my_choice = random.choice(my_sequence)
For details see the documentation.

Related

Create a unique list of tuple in a series sorted by order and taken randomly in a range

I'm looking for a solution to find unique lists of tuple with 6 values in a serie sorted by order and taken randomly in a range. I need to test each possible combination possible knowing that the values in each tuples must be sorted: I want to remove all tuples which have a value bigger than the next one as (10,1,2,3,4,5) and here to keep only (1,2,3,4,5,10).
i.e: output search [(0,1,2,3,4,5),(0,1,2,3,4,6),....(95,96,97,98,99,100)]
And I got a memory issue: MemoryError.
if someone as a solution, please.
Thanks in advance.
Regards
Here is my code:
import random
import itertools
from itertools import combinations, permutations
from random import choice, sample
score_found = list(range(math.ceil(0), math.floor(101)) # the range between score should be found
score number_number = 6 # number of risk classes
list_of_random_score=[]
for i in combinations(score_found, score_number):
list_of_random_score.append(i)
list_of_random_score.sort() #sort each tuple in the list
list_of_random_score2 = list(set(list_of_random_score)) #edit the list in order to have only [(0,1,2,3,4,5),(0,1,2,3,4,6),....(95,96,97,98,99,100)].

Selecting mutiple random sequences of N elements from M different-length lists in Python

I have X lists of elements, each list containing a different number of elements (without repetitions inside a single list). I want to generate (if possible, 500) sequences of 3 elements, where each element belongs to a different list, and sequences do not repeat. So something like:
X (in this case 4) lists of elements: [A1,A2], [B1,B2,B3,B4], [C1], [D1,D2,D3,D4,D5]
possible results: [A1,B2,D2], [B3,C1,D2], [A1,B2,C1]... (here 500 sequences are impossible, so would be less)
I think I know how to do it with a nasty loop: join all the lists, random.sample(len(l),3) from the joint list, if 2 indices belong to the same list repeat, if not, check if the sequence was not found before. But that would be very slow. I am looking for a more pythonic or more mathematically clever way.
Perhaps a better way would be to use random.sample([A,B,C,D], 3, p=[len(A), len(B), len(C), len(D)]), then for each sequence from it randomly select an element from each group in the sequence, then check if a new sequence generated in this way hasn't been generated before. But again, a lot of looping.
Any better ideas?
Check itertools module (combination and permutation in particular).
You can get a random.choice() from the permutations of 3 elements from the X lists (thus selecting 3 lists), and for each of them get a random.choice() (random module).

Trying to loop through a list that contains multiple items inside it [duplicate]

This question already has answers here:
Can I add a sequence of markers on a Folium map?
(2 answers)
Closed 2 years ago.
Problem
I'm mapping coordinates to a map using folium, my coordinates are stored in a list such as this e.g:
[[51.52765, -0.1322611111111111], [53.54326944444444, -2.633125]].
Now when I tell folium to map a coordinate from the list, I have to specify it like this:
folium.Marker(all_coords[1]).add_to(m) #[1] = the first set of coordinates from my list.
My list contains a lot of coordinates and in order to print them all out I have to do this:
folium.Marker(all_coords[1]).add_to(m) #[1] = the first set of coordinates from my list.
folium.Marker(all_coords[2]).add_to(m) #[2] = the second set of coordinates from my list.
folium.Marker(all_coords[3]).add_to(m) #[3] = the third set of coordinates from my list.
folium.Marker(all_coords[4]).add_to(m) #[4] = the fourth set of coordinates from my list.
folium.Marker(all_coords[5]).add_to(m) #[5] = the fifth set of coordinates from my list.
How would I go about telling folium to read all items from my list one by one?
Any help is appreciated thank you :)
Yes , #roganjosh is absolutely right. What you are trying to achieve can be done using the python for loop or while loop.
Of those the for loop is the best since looping variable is automatically defined.
Therefore instead of indexes, you can iterate through the list by:
for i in all_coords:
folium.Marker(i).add_to(m)
This is also clearly shown here.

Choosing N changing points in a sorted list

Imagine we have a sorted list with size P. How can we choose N indices for which the values reflect the range of the list more smoothly. For example if our list is:
List=[0,0,0,0,0,0,0,0,0,0.1,0.1,0.9,0.91,0.91,0.92,0.99,0.99,0.99]
Then how we choose let's say 5 indices that somehow shows the full range of the list?
In this example it would be something like :
indices=[0,9,11,14,15]
The final indices list doesn't have to be exactly like the one I wrote here though
This will give you a starting point:
[List.index(x) for x in set(List)]
Now this may have too many elements but "somehow" is totally subjective and not a clear enough definition for what you need to do. As a default you can keep the first and last element, then randomly pick as many as you require from the "middle".

List item changed across multiple items in a list [duplicate]

This question already has answers here:
How to deep copy a list?
(10 answers)
Closed 5 years ago.
I understand where my problem is stemming from, I simply do not know how to fix it.
for x in listoflists:
if x[1] == [1,0]:
q=[0, 0]
for z in range(5):
q[z] = x[:]
q[z][1] = [0,1]
q[z][0][-1] = q[z][0][-1] * 1.25
print(id(q[z][0][-1]))
list.append(q)
What I am trying to do is:
go into some data that I have gathered and make copies where I have switched a data label from [1,0] to [0,1]
then go into the zero index and multiply the price column by some factor to scale it up.
What I currently have is changing all prior values to be what I want the last value to be.
I understand this is something to do with python handling lists as references.
I had to change my first line to be a slice of x to alleviate this, but I do not know how to get around the parts where I take specifically indexed list items because it is showing the id as two different memory spaces.
All of the solutions I have seen show list comprehensions but I do not know how that applies to my case, because it seems like my z indexing is working correctly and they all have separate memory ids.
You need to do deep copy. Slicing does shallow copy.
from copy import deepcopy
lst1 = ['a','b',['ab','ba']]
lst2 = deepcopy(lst1)
id(lst1[2])
# 4451912
id(lst2[2])
# 51003144

Categories

Resources