我有一个“字典-valueiterator”组。对于每个组列表,我随机抽样两个(或更多值)并存储在一个新列表中
GroupsList = list([])
for m in Groups:
GroupsList.append(point_random_selection(m,n))
其中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>]]
在某些情况下,我有两个或更多列表(例如:)
GroupsList[6]
[<liblas.point.Point object at 0x000000047DF396A0>,
<liblas.point.Point object at 0x000000047DF14F98>]
我希望在新列表中存储一个逐个列表,以便在我有两个或多个liblas.point.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>]]
列表理解是最好的方法吗?ex插入
GroupsList.append(point_random_selection(m,n))