如何以一种方式对python列表进行排序,如果1应该在10之前,而10应该在20之前

时间:2018-09-02 11:03:35

标签: python

['2', '8', '2', '3', '6', '4', '1', '1', '10', '6', '3', '3', '6', '1', '3', '8', '4', '6', '1', '10', '8', '4', '10', '4', '1', '3', '2', '3', '2', '6', '1', '5', '2', '9', '8', '5', '10', '8', '7', '9', '6', '4', '2', '6', '3', '8', '8', '9', '8', '2', '9', '10', '3', '10', '7', '5', '7', '1', '7', '5', '1', '4', '7', '6', '1', '10', '5', '4', '8', '4', '2', '7', '8', '1', '1', '7', '4', '1', '1', '9', '8', '6', '5', '9', '9', '3', '7', '6', '3', '10', '8', '10', '7', '2', '5', '1', '1', '9', '9', '5']

按照以下方式使用lambda函数后:

    a.sort(key=lambda a: int(a.split()[0]))
    a = a[::-1]

我知道了

['10', '10', '10', '10', '10', '10', '10', '10', '10', '9', '9', '9', '9', '9', '9', '9', '9', '9', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '7', '7', '7', '7', '7', '7', '7', '7', '7', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '5', '5', '5', '5', '5', '5', '5', '5', '4', '4', '4', '4', '4', '4', '4', '4', '4', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '2', '2', '2', '2', '2', '2', '2', '2', '2', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1']

但是我想要    10在1后面排在最后,同样,如果将20和2放在列表中,则2应该排在20和20在10等之前

5 个答案:

答案 0 :(得分:1)

操作:

a.sort()

没有其他选项,会将a设置为:

['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '10', '10', '10', '10', '10', '10', '10', '10', '10', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '4', '4', '4', '4', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '5', '5', '5', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '7', '7', '7', '7', '7', '7', '7', '7', '7', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '9', '9', '9', '9', '9', '9', '9', '9', '9']

答案 1 :(得分:0)

您可以使用dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.2' implementation 'com.android.support:support-v4:27.1.1' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.google.firebase:firebase-firestore:17.0.4' implementation 'com.google.firebase:firebase-core:16.0.1' implementation 'com.google.firebase:firebase-auth:16.0.2' implementation 'com.google.android.gms:play-services-auth:15.0.1' implementation 'com.facebook.android:facebook-android-sdk:4.34.0' implementation 'com.android.support:design:27.1.1' implementation 'de.hdodenhof:circleimageview:2.2.0' implementation 'com.android.support:multidex:1.0.3' implementation 'com.google.android.gms:play-services-location:15.0.1' implementation 'com.github.drawers:SpinnerDatePicker:1.0.6' implementation 'com.github.yalantis:ucrop:2.2.2' }

使用以下示例
key=str

答案 2 :(得分:0)

您无需在此处使用lambda方法。可以使用'.sort()'方法对列表中的项目进行排序,而不是使用lambda。像这样一个:

li=['2', '8', '2', '3', '6', '4', '1', '1', '10', '6', '3', '3', '6', '1', '3', '8', '4', '6', '1', '10', '8', '4', '10', '4', '1', '3', '2', '3', '2', '6', '1', '5', '2', '9', '8', '5', '10', '8', '7', '9', '6', '4', '2', '6', '3', '8', '8', '9', '8', '2', '9', '10', '3', '10', '7', '5', '7', '1', '7', '5', '1', '4', '7', '6', '1', '10', '5', '4', '8', '4', '2', '7', '8', '1', '1', '7', '4', '1', '1', '9', '8', '6', '5', '9', '9', '3', '7', '6', '3', '10', '8', '10', '7', '2', '5', '1', '1', '9', '9', '5','20']
li.sort()
print(li)

输出:

['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '10', '10', '10', '10', '10', '10', '10', '10', '10', '2', '2', '2', '2', '2', '2', '2', '2', '2', '20', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '4', '4', '4', '4', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '5', '5', '5', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '7', '7', '7', '7', '7', '7', '7', '7', '7', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '9', '9', '9', '9', '9', '9', '9', '9', '9']

希望您能收到答复。

答案 3 :(得分:0)

这可能会帮助您:

a.sort(key=str)

new list = sorted(a, key=str) # if you dont want to change a

这将根据需要对列表进行排序,即使项目为整数

答案 4 :(得分:0)

您需要一个有序的字符串列表。这些字符串是数字的表示。现在,您希望进行分组排序。首先,一切都以'9'开头,然后是'8',最后到'1'。在每个组中,值都应按数字顺序排序。

示例列表:

a = ['11', '105', '2', '8', '2', '3', '6', '4', '1', '1', '10', '81', '3', '3', '5', '10', '8', '7', '9', '6', '4', '2']

现在让我们用a.sort(key=lambda v: v[0])进行分组排序:

['11', '105', '1', '1', '10', '10', '2', '2', '2', '3', '3', '3', '4', '4', '5', '6', '6', '7', '8', '81', '8', '9']

我们看到,这些值现在已经分组了,但是我们希望首先以'9'开头的值。我们将通过使用a.sort(key=lambda v: v[0], reversed=True)

反转结果来解决此问题
['9', '8', '81', '8', '7', '6', '6', '5', '4', '4', '3', '3', '3', '2', '2', '2', '11', '105', '1', '1', '10', '10']

组是正确的,现在我们必须对组中的值进行排序。因此,在根据第一个字符进行排序之后,我们必须按数字对值进行排序。这很容易,我们只需要为密钥tuple

创建一个a.sort(key=lambda v: (v[0], int(v)), reverse=True)
['9', '81', '8', '8', '7', '6', '6', '5', '4', '4', '3', '3', '3', '2', '2', '2', '105', '11', '10', '10', '1', '1']

好的,现在对值进行排序,但是我们必须在组中将它们取反。最简单的方法是采用负数:a.sort(key=lambda v: (v[0], -int(v)), reverse=True)

['9', '8', '8', '81', '7', '6', '6', '5', '4', '4', '3', '3', '3', '2', '2', '2', '1', '1', '10', '10', '11', '105']