- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的。这可能很困难,但我一直在努力奋斗,但没有太大的改进,所以我想知道你们的想法。
假设我有以下对象列表:
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/
好的。这可能很困难,但我一直在努力奋斗,但没有太大的改进,所以我想知道你们的想法。 假设我有以下对象列表: objects = [ {'id': '1', 'w': 0.20},
我想知道在使用 C/C++ 为不同的体系结构和操作系统(例如 Android 和 Iphone)编码时非常耗时的一件事。 我看到每个基于 gcc 的编译器都必须编译: 目标 build 主持人 我想知
嗯, 这个问题有点简单。 我有一个对象定义为: public class FullListObject : System.Collections.ArrayList, IPagedCollection
我观看了 Oracle OTN 虚拟 Activity :Java SE 和 JavaFX 2.0(2012 年 2 月 28 日),在谈论新的菱形运算符(即 Map> myMap = new Has
我有一个自定义单元格需要接收滑动事件,以便它可以在右侧显示删除按钮。 这是一个 iPad 应用程序。 问题是接收事件非常困难。就在快速滑动时调用委托(delegate)。在我项目的其他单元格中,这没有
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: What are the differences between struct and class in C
我正在尝试编写一个 tcp 流“隧道”(类似于默认情况下 SSH 处理的隧道),但有一个异常(exception),我必须在它流经时重写某些信息。 我确定那里有类似的东西,但我没能找到它。我有三个主要
我们有一些 Power BI 报告,希望与组织外部共享,但仅限指定用户。我们很幸运拥有高级容量,因此我们可以使用Secure Embed 功能可以以访客身份向我们的组织验证这些用户自己的电子邮件地址(
这是我当前用于选择要在弹出窗口中显示的监视器选定值的 Javascript: var theMonitor = document.getElementById("ctl00_Conte
我正在尝试了解开放式寻址方法。我引用了 T. H. Cormen 关于这个主题的书,其中指出在公开寻址中删除是困难的。我完全停留在这一段: Deletion from an open-address
相对于页面,CSS 中的水平对齐很容易 - margin:0 auto 大部分时间都能满足您的需求,而 text-align:center其他一些情况。 我对大师们的问题不是如何垂直对齐,而是为什么如
我试图为数据框中的每个变量创建一个双变量散点图,我发现 sns.pairplot() 正是我所需要的。 但是,无论我做什么(并且我已经尝试了此 question 中找到的所有建议),情节总是出现太分散
我引用最近的Google JavaScript Style Guide : Do not use JavaScript getter and setter properties. They are p
我最近放弃了在 Eclipse 中使用 Scala 的尝试(像完成这样的基本功能不起作用)。所以现在我正在尝试 IntelliJ。我还没有走得太远。 我已经能够编辑程序(在语法突出显示和完成内....
我是一名优秀的程序员,十分优秀!