1

I have a list of lists of different lengths. From each list of lists, I need to randomly select 10 items. Then I have to combine the results in a single list with each list item separated by comma. (Pls see the output below). Not sure whether this is possible in Python. Any help would be appreciated.

[[-26.0490761 27.79991 ] [-25.9444218 27.9116535] [-26.1055737 27.7756424] ..., [-26.036684 28.0508919] [-26.1367035 28.2753029] [-26.0668163 28.1137161]] [[ 45.35693 -63.1701241] [ 44.6566162 -63.5969276] [ 44.7197456 -63.48137 ] ..., [ 44.624588 -63.6244736] [ 44.6563835 -63.679512 ] [ 44.66706 -63.621582 ]] 

I would like to get the output in this format that is suitable for plotting them on a map using Folium.

 [[-26.0490761 27.79991], [-25.9444218 27.9116535], [ 44.6563835 -63.679512 ], [ 44.66706 -63.621582 ]] 

I tried this code but not sure what went wrong:

 for cluster in clusters: for i in range(2): modifiedlist.append(cluster) 
0

2 Answers 2

2

Something like this is easy using the module random:

import random def sampleFromLists(lists,n): """draws n elements from each list in lists returning the result as a single list""" sample = [] for subList in lists: sample.extend(random.sample(subList,n)) return sample 

Sample data (a list of lists of 2-element lists):

data = [ [[-26.0490761, 27.79991], [-25.9444218, 27.9116535], [-26.1055737, 27.7756424], [-26.036684, 28.0508919], [-26.1367035, 28.2753029], [-26.0668163,28.1137161]], [[ 45.35693, -63.1701241], [44.6566162 -63.5969276], [44.7197456, -63.48137], [44.624588, -63.6244736], [44.6563835,-63.679512], [44.66706, -63.621582]] ] 

And then:

>>> sampleFromLists(data,2) [[-26.036684, 28.0508919], [-26.0490761, 27.79991], [44.7197456, -63.48137], [44.6563835, -63.679512]] 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your response. But When we pass in [[1,2,3,4],[5,6,7,8],[9,10,11,12]] and select 2 numbers randomly from each list, the output has to be [[2,3][5,6],[9,12]] (random 2 elements from each list).
Just replace extend by append in the above code if you need. Note that your data seems to be of a list of list of 2-element lists, so I think that if you run my code with your data (assuming that it is all just one big list of lists) then the output will be a list of 2-element lists. Thus, unless I misunderstood your set up, I think that my code should work as is.
@user3447653 the edit shows what I mean -- I made the sample reflect the data in your question.
[['p1', 'p2', 'p3', 'p4', 'p5'], ['p6', 'p7', 'p8', 'p9', 'p10'], ['p11', 'p12', 'p13', 'p14', 'p15'], ['p16', 'p17', 'p18', 'p19', 'p20']] I have list like this... how do i modify the above code to get me output like : [p2,p9,p12,p19]? randomly choosing one element from a sublist and printing out the list?
0
import random x = [[1,2],[3,4],[5,6],[7,8],[9],[7675,6456,4],[5,6]] z = [] for i in range(10): y = random.choice(x) z.append([random.choice(y), random.choice(y)]) print(z) 

random.choice() picks a random item from the given input (in our case a list).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.