在比较python中的两个元素时,如何指示一个元组中的顺序变化?
我面临一个比较以下元素的问题:
chains = ['A','B','C','D']
proper_list = ['ABBA','BDDA','CDDA','ABBA']
corrupted_list = ['ABBA','CDDA','BDDA','ABBA']
proper_set = tuple(zip(chains, proper_list))
corrupted_set = tuple(zip(chains, corrupted_list))
两者的输出如下:
(('A', 'ABBA'), ('B', 'BDDA'), ('C', 'CDDA'), ('D', 'ABBA')),
(('A', 'ABBA'), ('B', 'CDDA'), ('C', 'BDDA'), ('D', 'ABBA'))
我想以某种方式打印出更新的元组,在该元组中,算法可以找到并指示分配给值的不当元素,如下所示:
(('A', 'ABBA'), ('C', 'CDDA'), ('B', 'BDDA'), ('D', 'ABBA'))
或至少产生该元素。列表具有适当的顺序:
['A','C','B','D']
由于适当/损坏的列表中的元素可能会重复,因此我无法(或无法)使用dict。
假设:
- 列表和元素的长度将始终是
- Corrputed_list中任何元素的顺序
相等,您可能建议解决此问题的建议?
I am facing a problem with comparing following tuples:
chains = ['A','B','C','D']
proper_list = ['ABBA','BDDA','CDDA','ABBA']
corrupted_list = ['ABBA','CDDA','BDDA','ABBA']
proper_set = tuple(zip(chains, proper_list))
corrupted_set = tuple(zip(chains, corrupted_list))
The output of both is as follows:
(('A', 'ABBA'), ('B', 'BDDA'), ('C', 'CDDA'), ('D', 'ABBA')),
(('A', 'ABBA'), ('B', 'CDDA'), ('C', 'BDDA'), ('D', 'ABBA'))
I would like to somehow print out the updated tuple, where an algorithm could find and indicate improper element assigned to value, as follows:
(('A', 'ABBA'), ('C', 'CDDA'), ('B', 'BDDA'), ('D', 'ABBA'))
or at least yield the list with proper order:
['A','C','B','D']
As the elements in proper/corrupted lists may be duplicated I cannot (or am not able to) use dict.
Assumptions:
- lengths of both lists and elements will always be equal
- the order of any element in corrputed_list may differ
What could you suggest to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定要理解您的问题,但这似乎可以得到您想要的结果:
extisouly它没有优化或使用任何内置功能,我只是做了一个快速的POC
Not sure to understand your question but this seems to get the result you want:
Obvisouly it is not optimised nor using any builtin functions, I just did a quick POC
我认为您需要修复链条,以防它使用该序列有问题,所以类似的东西
I think you need to fix the chain in case it has an issue using the sequence so something like this