如何在Python中对某些条件进行排序? [英] How can I sort with some conditions in Python?

查看:65
本文介绍了如何在Python中对某些条件进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,非常感谢伸出援助之手。

我想根据某些条件对x1,x2,..,x5进行排序。



my_list = [['x2','x5','1'],['x3','x1','0'],['x3','x2','0' ],[ '×4', 'X 1', '0'],[ 'X2', '×3', '1'],[ 'X1', 'X2', '1'],[ 'X1', x3','1'],['x2','x1','0'],['x2','x4','1'],['x4','x5','1']]



我知道如果my_list中的子列表中的第3个元素为1,则表示第一个elemt大于第二个元素(x2> x5)。 br />
并且,如果第3个元素为0,则表示第二个元素大于第一个元素(x1> x3)。

根据这些信息,它会不会是可以分类x1,.. x5?



我尝试过:



根据这些信息,是否可以对x1,.. x5进行排序?

I have a question and would really appreciate lending a helping hand.
I want to sort x1,x2,..,x5 based on some conditions.

my_list=[['x2','x5','1'],['x3','x1','0'],['x3','x2','0'],['x4','x1','0'],['x2','x3','1'],['x1','x2','1'],['x1','x3','1'], ['x2','x1','0'], ['x2','x4','1'],['x4','x5','1']]

I know that if 3rd element in sublists in my_list is "1", it means that the first elemt is greater than the second one (x2>x5).
And, if 3rd element is "0", it means that the second element is greater than the first one (x1>x3).
Based on such information, would it be doable to sort x1,..x5?

What I have tried:

Based on such information, would it be doable to sort x1,..x5?

推荐答案

对不起,没有好运(如果我是对的)。

一个简单的转换产生

Sorry, there is no luck (if I am right).
A simple transformation produces
l = [['x5', 'x2'], ['x3', 'x1'], ['x3', 'x2'], ['x4', 'x1'], ['x3', 'x2'], ['x2', 'x1'], ['x3', 'x1'], ['x2', 'x1'], ['x4', 'x2'], ['x5', 'x4']]

在每个子列表中,firt项目小于第二个。



现在,尽管你可以写

Where, in every sublist, the firt item is smaller than the second one.

Now, while you can write

'x5' < 'x4' < 'x2' < 'x1'



无法确定'x3'的正确位置,所有以下序列被允许:


There is no way to establish the correct position of 'x3', all of the following sequences being allowed:

'x5' < 'x4' < 'x3' < 'x2' < 'x1'
'x5' < 'x3' < 'x4' < 'x2' < 'x1'
'x3' < 'x5' < 'x4' < 'x2' < 'x1'


引用:

根据这些信息,它会不会可以排序x1,.. x5?

Based on such information, would it be doable to sort x1,..x5?



它可以,但不是必需的,它取决于你的清单。

拿一张纸和一支铅笔。

绘制图形

将名为x1的5个节点绘制为x5。

该列表为您提供节点对之间的更大关系。
$ b列表的每个元素都是$ b,在节点之间绘制一条边,然后放一个箭头来显示哪一个更大。

检查所有边是否一致并且不要自相矛盾。



只有对图表的分析才能告诉您列表og关系是否足以对节点进行排序,或者只对子集进行排序。

语言无关紧要,您需要找到一个算法来告诉节点是否已排序。


It can be, but not necessary, it depend on the list you have.
Take a sheet of paper and a pencil.
Draw a graph
Draw 5 nodes named x1 to x5.
The list gives you the relation 'Greater' between pairs of nodes.
for each element of list, draw an edge between the nodes and put an arrow to show which one is greater.
Check that all edges are consistent and do not contradict themselves.

Only an analyze of the graph can tell you if the list og relations is enough to sort the nodes, or only a subset.
The language does not matter, you need to find an algorithm that tell is the nodes are sorted or not.


这篇关于如何在Python中对某些条件进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆