Select random items from list of lists in Python -
i have list of lists of different lengths. each list of lists, need randomly select 10 items. have combine results in single list each list item separated comma. (pls see output below). not sure whether possible in python. 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 output in format suitable plotting them on map using folium.
[[-26.0490761 27.79991], [-25.9444218 27.9116535], [ 44.6563835 -63.679512 ], [ 44.66706 -63.621582 ]]
i tried code not sure went wrong:
cluster in clusters: in range(2): modifiedlist.append(cluster)
something easy using module random
:
import random def samplefromlists(lists,n): """draws n elements each list in lists returning result single list""" sample = [] 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]]
Comments
Post a Comment