简体   繁体   English

使用用户定义的规则对项目进行排序

[英]Sorting items using user-defined rule

For instance, if I have a list of usernames, lets call this list L1, each username has his/her own profile (not stored in the L1). 例如,如果我有一个用户名列表,让我们调用此列表L1,每个用户名都有他/她自己的配置文件(不存储在L1中)。 How should we write the function so that it will sort the usernames based on their actual name, if any usernames are tied for actual name, sort them by usernames. 我们应该如何编写函数,以便它根据实际名称对用户名进行排序,如果有任何用户名与实际名称相关联,则按用户名对它们进行排序。 Note, I can get the username of the user by writing username[1] , usernames are unique. 注意,我可以通过编写username[1]来获取用户的用户username[1] ,用户名是唯一的。

This is what I write: 这就是我写的:

def username(s1, s2):

    if s1 < s2:
        return -1
    elif s1 > s2:
        return 1
    else:
        # How can i sort them by username if they have the same actual name
        return 0

You can always store the user names in a list, and then use the builtin sorted() function to make a sorted list of user names. 您始终可以将用户名存储在列表中,然后使用builtin sorted()函数生成用户名的排序列表。 Example: 例:

users = ['Guido', 'Alex', 'Jack', 'Brian']
users_sorted = sorted(users)
print repr(users_sorted)

The result of this would be the list of users sorted alphabetically: 结果将是按字母顺序排序的用户列表:

['Alex', 'Brian', 'Guido', 'Jack']

sorted takes several arguments, so if you want to define a special way to compare items in the list, you can do sorted(user, cmp=function) , where function is your special comparison function. sorted需要多个参数,所以如果你想定义一种比较列表中项目的特殊方法,你可以做sorted(user, cmp=function) ,其中function是你的特殊比较函数。

See the docs on sorted() for more. 有关详细信息,请参阅sorted()上文档

sorted_username_list = sorted(usernames, key=user_sorter)

where: 哪里:

usernames is a list usernames是一个列表

user_sorter is a function that takes a username and returns user's actual name user_sorter是一个获取用户名并返回用户实际名称的函数

for same actual name, you can write the sorter function as this: 对于相同的实际名称,您可以将分拣机功能编写为:

def user_sorter(x):
    return actual_name_of_user(x) + x

You might want to use sorted(iterable, key=function) . 您可能希望使用sorted(iterable, key=function) For example: 例如:

users = list(something) # a list of all the usernames 
user_profiles = dict(something) # a dict of the profiles

users_sorted = sorted(users, key=lambda un: user_profiles[un])

You could also overload the comparison operators like >, = and < by override __gt__(), __eq__() and __lt__() of your user profile data class. 您还可以重载比较运算符,例如>,=和<覆盖用户配置文件数据类的__gt__(), __eq__() and __lt__() Please refer to Python 2.7 reference - Data model for further information. 有关详细信息,请参阅Python 2.7参考 - 数据模型

Pack the args into tuples and sort them using the built in sort. 将args打包成元组并使用内置排序对它们进行排序。 Then unpack. 然后打开包装。

usernames = ['bbob', 'srv', 'gman', 'salmer']
firstnames = ['Billy', 'Steve', 'Guy', 'Steve']
tuples = zip(firstnames, usernames)
tuples.sort()
users_sorted, first_sorted = zip(*tuples)


>>>tuples
[('Billy', 'bbob'), ('Guy', 'gman'), ('Steve', 'salmer'), ('Steve', 'srv')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM