gpt4 book ai didi

python - "Difficult"某些条件下排序

转载 作者:行者123 更新时间:2023-11-28 23:01:41 26 4
gpt4 key购买 nike

好的。这可能很困难,但我一直在努力奋斗,但没有太大的改进,所以我想知道你们的想法。

假设我有以下对象列表:

objects = [
{'id': '1', 'w': 0.20},
{'id': '1.1', 'w': 0.80},
{'id': '1.2', 'w': 0.20},
{'id': '1.3', 'w': 0.30},
{'id': '1.1.1', 'w': 0.60},
{'id': '1.1.2', 'w': 0.70},
{'id': '1.1.3', 'w': 0.40},
{'id': '1.2.1', 'w': 0.30},
]

我想按“id”(例如 “1”、“1.1”、“1.1.1”、“1.1.2”、“1.1.3”、“1.2”、“1.2”)对列表进行排序.1', '1.3') 但随后所有具有相同父元素的元素都需要按 'w' 排序(反向)。 “同父异母”是什么意思?那么,“1”是“1.1”、“1.2”和“1.3”的父级。同样,“1.1”是“1.1.1”、“1.1.2”、“1.1.3”的父级,“1.2”是“1.2.1”的父级。为了更好地说明这一点,假设这是一个带有嵌套评论的线程的表示(“1”是原始帖子,“1.1”是它的答案,依此类推)。

目前,我已经能够达到以下形式:

[ [ {'w': 0.2, 'id': '1'} ], [ {'w': 0.8, 'id': '1.1'}, {'w': 0.3, 'id': '1.3'}, 
{'w': 0.2, 'id': '1.2'} ], [ {'w': 0.7, 'id': '1.1.2'}, {'w': 0.6, 'id': '1.1.1'},
{'w': 0.4, 'id': '1.1.3'} ], [ {'w': 0.3, 'id': '1.2.1'} ] ]

如您所见,每个嵌套列表都包含作为其他元素的子元素的元素。比如第二个嵌套列表 [ {'w': 0.8, 'id': '1.1'}, {'w': 0.3, 'id': '1.3'}, {'w': 0.2, 'id': '1.2'} ] 包含元素 [ {'w': 0.2, 'id': '1'} ] 的所有子元素。此外,每个嵌套列表都按“w”排序。

最终结果应该是这样的(假设链接所有内部列表 - list(itertools.chain(*b))):

{'id': '1', 'w': 0.20}, {'id': '1.1', 'w': 0.80}, {'id': '1.1.2', 'w': 0.70}, 
{'id': '1.1.1', 'w': 0.60}, {'id': '1.1.3', 'w': 0.40}, {'id': '1.3', 'w': 0.30},
{'id': '1.2', 'w': 0.20}, {'id': '1.2.1', 'w': 0.30}

基本上,首先是父元素,然后是它的子元素(按 'w' 排序),这同样适用于每个元素(当然,如果它有子元素 - 这里 {'id': '1.3', 'w': 0.30} 没有 child ,所以我们不需要对它做任何事情。

我尝试了一些东西(太复杂了,不值得解释)。我最终得到了很多条件语句和丑陋的代码。

我怎样才能完成这种排序?

提前致谢。

最佳答案

简单的排序不会解决您的问题,因为不可能比较任何两个元素并立即知道一个元素何时出现在另一个元素之前(父元素的权重可能会改变顺序)。

需要将列表处理成树状结构,然后按顺序抽取:

tree = {}

for d in objects:
ids = d['id'].split('.')
w = d['w']
# walk into the tree, creating nodes as necessary
subtree = [0,tree]
for n in ids:
if n not in subtree[1]:
subtree[1][n] = [0,{}] # w, list of nodes
subtree = subtree[1][n] # recurse
# subtree is now the relevant node, set w
subtree[0] = w

## now we have a tree:
## >>> pprint.pprint(tree, width=10)
## {'1': [0.2,
## {'1': [0.8,
## {'1': [0.6,
## {}],
## '2': [0.7,
## {}],
## '3': [0.4,
## {}]}],
## '2': [0.2,
## {'1': [0.3,
## {}]}],
## '3': [0.3,
## {}]}]}

# now walk the tree and extract the nodes:
result = []
def walk_subtree(subtree, path=[]):
keyweights = [(subtree[key][0], key) for key in subtree]
# walk through nodes at this level, outputting.
for weight, key in sorted(keyweights, reverse=True):
result.append(('.'.join(path + [key]), weight))
walk_subtree(subtree[key][1], path=path+[key])

walk_subtree(tree)

##>>> pprint.pprint(result)
##[('1', 0.2),
## ('1.1', 0.8),
## ('1.1.2', 0.7),
## ('1.1.1', 0.6),
## ('1.1.3', 0.4),
## ('1.3', 0.3),
## ('1.2', 0.2),
## ('1.2.1', 0.3)]

关于python - "Difficult"某些条件下排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10793737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com