在Python中比较同一字典中每个键的字典值
更新:
你好。我的问题是,如何比较字典的值是否相等。有关我的词典的更多信息:
- 键是会话编号
每个键的值是嵌套列表 ->铁
[[1,0],[2,0],[3,1]]
每个键的值的长度不相同,因此会话号 1 的值可能比会话号 2< /p>
- 这里是一个示例字典:
订单会话= {1:[[100,0],[22,1],[23,2]],10:[100,0],[232,0],[10,2],[11,2]], 22:[[5,2],[23,2],....], ... }
我的目标:
第 1 步:将会话号 1 的值与字典中所有其他会话号的值进行比较,以确定是否相等
第 2 步:采用下一个会话号并将这些值与其他会话号的其他值进行比较,依此类推 - 最后我们比较每个会话数值
第3步:将结果保存到列表中 输出 = [[100,0],[23,2], ... ] 或 输出 = [(100,0),(23,2), ... ]
- 如果您可以看到值对 [100,会话1和会话10的0]是相同的。会话 1 和 22 的值对 [23,2] 也相同。
谢谢你帮我。
更新2
感谢您提供的所有帮助和提示,将列表的嵌套列表更改为元组列表,这样可以更好地处理它。
我更喜欢 Boaz Yaniv 解决方案;) 我也喜欢使用 collections.Counter() ...不幸的是我使用 2.6.4 (Counter 在 2.7 上工作)也许我有时会更改为 2.7 。
Update:
Hello again. My question is, how can I compare values of an dictionary for equality. More Informationen about my Dictionary:
- keys are session numbers
values of each key are nested lists -> f.e.
[[1,0],[2,0],[3,1]]
the length of values for each key arent the same, so it could be that session number 1 have more values then session number 2
- here an example dictionary:
order_session =
{1:[[100,0],[22,1],[23,2]],10:[100,0],[232,0],[10,2],[11,2]],22:[[5,2],[23,2],....],
... }
My Goal:
Step 1: to compare the values of session number 1 with the values of the whole other session numbers in the dictionary for equality
Step 2: take the next session number and compare the values with the other values of the other session numbers, and so on
- finally we have each session number value compared
Step 3: save the result into a list f.e.
output = [[100,0],[23,2], ... ] or output = [(100,0),(23,2), ... ]
- if you can see a value-pair [100,0] of session 1 and 10 are the same. also the value-pair [23,2] of session 1 and 22 are the same.
Thanks for helping me out.
Update 2
Thank you for all your help and tips to change the nested list of lists into list of tuples, which are quite better to handle it.
I prefer Boaz Yaniv solution ;)
I also like the use of collections.Counter() ... unlucky that I use 2.6.4 (Counter works at 2.7) maybe I change to 2.7 sometimes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你的字典很长,你会想要使用集合,以获得更好的性能(在列表中查找已经遇到的值会非常慢):
我还建议(再次,出于性能原因)将元组嵌套在列表中而不是列表(如果您不打算即时更改它们)。我在这里发布的代码可以以任何方式工作,但如果值已经是元组,那么速度会更快。
If your dictionary is long, you'd want to use sets, for better performance (looking up already-encountered values in lists is going to be quite slow):
I also suggest (again, for performance reasons) to nest tuples inside your lists instead of lists, if you're not going to change them on-the-fly. The code I posted here will work either way, but it would be faster if the values are already tuples.
可能有一种更好、更优化的方法来做到这一点,但我会从这里开始工作:
基本上,它的作用是查看所有值,如果该值之前已经见过,但之前没有输出,那么它被附加到输出中。
请注意,这适用于值对的实际值 - 如果您有各种导致指针的对象,我的算法可能会失败(我没有测试过它,所以我不确定)。 Python 对“低”整数重复使用相同的对象引用;也就是说,如果您依次运行语句
a = 5
和b = 5
,则a
和b
将指向同一个整数对象。但是,如果将它们设置为 10^5,则它们不会。但我不知道限制在哪里,所以我不确定这是否适用于您的代码。There's probably a nicer and more optimal way to do this, but I'd work my way from here:
Basically, what this does is to look through all the values, and if the value has been seen before, but not output before, it is appended to the output.
Note that this works with the actual values of the value pairs - if you have objects of various kinds that result in pointers, my algorithm might fail (I haven't tested it, so I'm not sure). Python re-uses the same object reference for "low" integers; that is, if you run the statements
a = 5
andb = 5
after each other,a
andb
will point to the same integer object. However, if you set them to, say, 10^5, they will not. But I don't know where the limit is, so I'm not sure if this applies to your code.如果您真的需要一个列表列表
If you really really need a list of lists