正在为 python 中的列表列表生成新顺序
Generating a new order for a list of lists in python
我有一个列表列表,我想重新排序:
qvalues = [[0.1, 0.3, 0.6],[0.7, 0.1, 0.2],[0.3, 0.4, 0.3],[0.1, 0.3, 0.6],[0.1, 0.3, 0.6],[0.1, 0.3, 0.6]]
如果我有一个按我想要的顺序排列的列表(示例 here),我知道如何重新排序此列表。棘手的部分是获取此订单。
我的是这样的:
locations = [(['Loc1','Loc1'], 3), (['Loc2'], 1), (['Loc3', 'Loc3', 'Loc3'], 2)]
这是一个元组列表,其中每个元组的第一个元素是一个带有位置名称的列表,为该位置的每个人重复,第二个元素是这些人在 qvalues
列表(qvalues[0]
是 'Loc2'
,qvalues[1:4]
是 'Loc3'
和 qvalues[4:6]
是 'Loc1'
。
我想要的是将 qvalues
中列表的顺序更改为它们在 locations
中显示的顺序:首先是 'Loc1'
,然后是 'Loc2'
,最后'Loc3'
.
这只是一个小例子,我的真实数据集有数百个人和 17 个位置。
在此先感谢您提供的任何帮助。
您需要构建一个偏移量和长度列表,而不是 locations
列表中提供的长度和位置。然后,您将能够根据链接到的答案重新排序:
qvalues = [[0.1, 0.3, 0.6],[0.7, 0.1, 0.2],[0.3, 0.4, 0.3],[0.1, 0.3, 0.6],[0.1, 0.3, 0.6],[0.1, 0.3, 0.6]]
locations = [(['Loc1','Loc1'], 3), (['Loc2'], 1), (['Loc3', 'Loc3', 'Loc3'], 2)]
locations_dict = {pos:(index,len(loc)) for index,(loc,pos) in enumerate(locations)}
# if python2: locations_dict = dict([(pos,(index,len(loc))) for index,(loc,pos) in enumerate(locations)])
offsets = [None]*len(locations)
def compute_offset(pos):
# compute new offset from offset and length of previous position. End of recursion at position 1: we’re at the beginning of the list
offset = sum(compute_offset(pos-1)) if pos > 1 else 0
# get index at where to store current offset + length of current location
index, length = locations_dict[pos]
offsets[index] = (offset, length)
return offsets[index]
compute_offset(len(locations))
qvalues = [qvalues[offset:offset+length] for offset,length in offsets]
你最终会得到 qvalues
列表列表的列表而不是 "simple" 列表列表。如果您想展平它以保持您的初始布局,请改用此列表推导式:
qvalues = [value for offset,length in offsets for value in qvalues[offset:offset+length]]
第一个版本的输出
[[[0.1, 0.3, 0.6], [0.1, 0.3, 0.6]], [[0.1, 0.3, 0.6]], [[0.7, 0.1, 0.2], [0.3, 0.4, 0.3], [0.1, 0.3, 0.6]]]
第二个版本的输出
[[0.1, 0.3, 0.6], [0.1, 0.3, 0.6], [0.1, 0.3, 0.6], [0.7, 0.1, 0.2], [0.3, 0.4, 0.3], [0.1, 0.3, 0.6]]
我有一个列表列表,我想重新排序:
qvalues = [[0.1, 0.3, 0.6],[0.7, 0.1, 0.2],[0.3, 0.4, 0.3],[0.1, 0.3, 0.6],[0.1, 0.3, 0.6],[0.1, 0.3, 0.6]]
如果我有一个按我想要的顺序排列的列表(示例 here),我知道如何重新排序此列表。棘手的部分是获取此订单。
我的是这样的:
locations = [(['Loc1','Loc1'], 3), (['Loc2'], 1), (['Loc3', 'Loc3', 'Loc3'], 2)]
这是一个元组列表,其中每个元组的第一个元素是一个带有位置名称的列表,为该位置的每个人重复,第二个元素是这些人在 qvalues
列表(qvalues[0]
是 'Loc2'
,qvalues[1:4]
是 'Loc3'
和 qvalues[4:6]
是 'Loc1'
。
我想要的是将 qvalues
中列表的顺序更改为它们在 locations
中显示的顺序:首先是 'Loc1'
,然后是 'Loc2'
,最后'Loc3'
.
这只是一个小例子,我的真实数据集有数百个人和 17 个位置。
在此先感谢您提供的任何帮助。
您需要构建一个偏移量和长度列表,而不是 locations
列表中提供的长度和位置。然后,您将能够根据链接到的答案重新排序:
qvalues = [[0.1, 0.3, 0.6],[0.7, 0.1, 0.2],[0.3, 0.4, 0.3],[0.1, 0.3, 0.6],[0.1, 0.3, 0.6],[0.1, 0.3, 0.6]]
locations = [(['Loc1','Loc1'], 3), (['Loc2'], 1), (['Loc3', 'Loc3', 'Loc3'], 2)]
locations_dict = {pos:(index,len(loc)) for index,(loc,pos) in enumerate(locations)}
# if python2: locations_dict = dict([(pos,(index,len(loc))) for index,(loc,pos) in enumerate(locations)])
offsets = [None]*len(locations)
def compute_offset(pos):
# compute new offset from offset and length of previous position. End of recursion at position 1: we’re at the beginning of the list
offset = sum(compute_offset(pos-1)) if pos > 1 else 0
# get index at where to store current offset + length of current location
index, length = locations_dict[pos]
offsets[index] = (offset, length)
return offsets[index]
compute_offset(len(locations))
qvalues = [qvalues[offset:offset+length] for offset,length in offsets]
你最终会得到 qvalues
列表列表的列表而不是 "simple" 列表列表。如果您想展平它以保持您的初始布局,请改用此列表推导式:
qvalues = [value for offset,length in offsets for value in qvalues[offset:offset+length]]
第一个版本的输出
[[[0.1, 0.3, 0.6], [0.1, 0.3, 0.6]], [[0.1, 0.3, 0.6]], [[0.7, 0.1, 0.2], [0.3, 0.4, 0.3], [0.1, 0.3, 0.6]]]
第二个版本的输出
[[0.1, 0.3, 0.6], [0.1, 0.3, 0.6], [0.1, 0.3, 0.6], [0.7, 0.1, 0.2], [0.3, 0.4, 0.3], [0.1, 0.3, 0.6]]