i have a ‘dictionary-valueiterator’ Groups. For each list of Groups i random sample two (or more values) and i store in a new list
GroupsList = list([])
for m in Groups:
GroupsList.append(point_random_selection(m,n))
where point_random_selection
def point_random_selection(list,n):
if n == 0:
raise TypeError("The pulse density 0 is not iterable")
try:
sample_point = random.sample(list,n)
except ValueError:
sample_point = list
return sample_point
GroupsList = [[<liblas.point.Point object at 0x00000001363A0E10>],
[<liblas.point.Point object at 0x00000002CE52CB38>],
[<liblas.point.Point object at 0x00000000CF516908>],
[<liblas.point.Point object at 0x00000004293F9400>],
[<liblas.point.Point object at 0x0000000249F67C50>],
[<liblas.point.Point object at 0x0000000312D85A90>],
[<liblas.point.Point object at 0x000000047DF396A0>,
<liblas.point.Point object at 0x000000047DF14F98>]]
where in some case i have two or more list (ex:)
GroupsList[6]
[<liblas.point.Point object at 0x000000047DF396A0>,
<liblas.point.Point object at 0x000000047DF14F98>]
I wish to store one list-by-one list in the new list in order to append one-by-one also when i have two or more liblas.point.Point object as:
GroupsList = [[<liblas.point.Point object at 0x00000001363A0E10>],
[<liblas.point.Point object at 0x00000002CE52CB38>],
[<liblas.point.Point object at 0x00000000CF516908>],
[<liblas.point.Point object at 0x00000004293F9400>],
[<liblas.point.Point object at 0x0000000249F67C50>],
[<liblas.point.Point object at 0x0000000312D85A90>],
[<liblas.point.Point object at 0x000000047DF396A0>],
[<liblas.point.Point object at 0x000000047DF14F98>]]
Is it a List Comprehensions the best approach? ex to insert in
GroupsList.append(point_random_selection(m,n))
If I understand this correctly, you want to change a nested list like
[[a, b], [c, d]]into[a, b, c, d]using a list comprehension. If that is the correct interpretation for what you are trying to do, the following will work:However it would be better to just create the flat structure from the beginning by changing your loop to the following (replacing
append()withextend()):a.extend(b)(whereaandbare lists) results in the same behavior as the following loop (but does it more efficiently):