Python 从关键字列表中选择主键并按字母顺序作为辅助键的词典排序列表
我有一个字典列表,我希望它们按照关键字列表作为主键进行排序,或者按照字母顺序排列相等的条目 目前,我先按字母顺序排序,然后根据提供的关键字进行排序,由于使用了稳定的排序算法,因此产生了所需的结果。然而,我认为这可以一步完成,但我不知道为什么。有人能帮忙吗 其次,我希望能够使用关键字,而不是关键字排序部分的精确匹配。我该怎么做 以下是我目前的代码:Python 从关键字列表中选择主键并按字母顺序作为辅助键的词典排序列表,python,sorting,dictionary,Python,Sorting,Dictionary,我有一个字典列表,我希望它们按照关键字列表作为主键进行排序,或者按照字母顺序排列相等的条目 目前,我先按字母顺序排序,然后根据提供的关键字进行排序,由于使用了稳定的排序算法,因此产生了所需的结果。然而,我认为这可以一步完成,但我不知道为什么。有人能帮忙吗 其次,我希望能够使用关键字,而不是关键字排序部分的精确匹配。我该怎么做 以下是我目前的代码: # Define the keywords I want to see first preferred_projects = ['project on
# Define the keywords I want to see first
preferred_projects = ['project one', 'project two', 'project three']
# example data
AllMyProjectsFromaDatasource = [{ 'name': 'project two', 'id': 5, 'otherkey': 'othervalue'},
{ 'name': 'project three', 'id': 1, 'otherkey': 'othervalue'},
{ 'name': 'project one', 'id': 3, 'otherkey': 'othervalue'},
{ 'name': 'abc project', 'id': 6, 'otherkey': 'othervalue'},
{ 'name': 'one project', 'id': 9, 'otherkey': 'othervalue'}
]
def sort_by_preferred(key):
"""Sort lists out by prefered name."""
sortkey = key['name']
return preferred.index(sortkey) if sortkey in preferred else len(preferred)
# First sort alphabetical
AllProjects = sorted(AllMyProjectsFromaDatasource,
key=lambda k: k['name'])
# Then sort by keyword
preferred = preferred_projects
AllProjects.sort(key=sort_by_preferred)
所以实际上我想定义我的“排序过滤器”,就像这样:
preferred_projects = ['one', 'two', 'three']
[{ 'name': 'one project', 'id': 9, 'otherkey': 'othervalue'}
{ 'name': 'project one', 'id': 3, 'otherkey': 'othervalue'},
{ 'name': 'project two', 'id': 5, 'otherkey': 'othervalue'},
{ 'name': 'project three', 'id': 1, 'otherkey': 'othervalue'},
{ 'name': 'abc project', 'id': 6, 'otherkey': 'othervalue'},]
并将列表按如下方式排序:
preferred_projects = ['one', 'two', 'three']
[{ 'name': 'one project', 'id': 9, 'otherkey': 'othervalue'}
{ 'name': 'project one', 'id': 3, 'otherkey': 'othervalue'},
{ 'name': 'project two', 'id': 5, 'otherkey': 'othervalue'},
{ 'name': 'project three', 'id': 1, 'otherkey': 'othervalue'},
{ 'name': 'abc project', 'id': 6, 'otherkey': 'othervalue'},]
您可以创建一个合适的元组作为排序键。第一部分是首选项目的索引,默认值为最大索引。第二部分是允许按字母顺序排序的名称:
preferred_projects = ['project one', 'project two', 'project three']
def sort_by(entry):
name = entry['name']
try:
index = preferred_projects.index(name)
except ValueError:
index = len(preferred_projects)
return (index, name)
AllMyProjectsFromaDatasource = [
{ 'name': 'project two', 'id': 5, 'otherkey': 'othervalue'},
{ 'name': 'project three', 'id': 1, 'otherkey': 'othervalue'},
{ 'name': 'project one', 'id': 3, 'otherkey': 'othervalue'},
{ 'name': 'abc project', 'id': 6, 'otherkey': 'othervalue'},
{ 'name': 'one project', 'id': 9, 'otherkey': 'othervalue'}]
AllProjects = sorted(AllMyProjectsFromaDatasource, key=sort_by)
for p in AllProjects:
print p
为您提供以下输出:
{'otherkey': 'othervalue', 'name': 'project one', 'id': 3}
{'otherkey': 'othervalue', 'name': 'project two', 'id': 5}
{'otherkey': 'othervalue', 'name': 'project three', 'id': 1}
{'otherkey': 'othervalue', 'name': 'abc project', 'id': 6}
{'otherkey': 'othervalue', 'name': 'one project', 'id': 9}
可以使用查找子字符串是否包含在另一个字符串中)
对于Unicode和字符串类型,当且仅当x
是y
的子字符串时,x in y
为真。等效测试是y.find(x)!=-1
。[…]空字符串始终被视为任何其他字符串的子字符串,因此“abc”中的”将返回True
您可以使用它来实现关键字排序键
您可以使用另一个答案中给出的方法(将元组作为键传递),将字母排序作为辅助键来实现
下面是一个例子:
import pprint
# Define the keywords I want to see first
preferred_projects = ['one', 'two', 'three']
# example data
AllMyProjectsFromaDatasource = [{ 'name': 'project two', 'id': 5, 'otherkey': 'othervalue'},
{ 'name': 'project three', 'id': 1, 'otherkey': 'othervalue'},
{ 'name': 'project one', 'id': 3, 'otherkey': 'othervalue'},
{ 'name': 'abc project', 'id': 6, 'otherkey': 'othervalue'},
{ 'name': 'one project', 'id': 9, 'otherkey': 'othervalue'}
]
def keyfunc(x):
# keyword primary key
# (add index to list comprehension when keyword is in name)
preferred_key = [float(idx)
for idx, i in enumerate(preferred_projects)
if i in x['name']]
# found at least one match in preferred keywords, use first if any, else infinity
keyword_sortkey = preferred_key[0] if preferred_key else float('inf')
# return tuple to sort according to primary and secondary key
return keyword_sortkey, x['name']
AllMyProjectsFromaDatasource.sort(key=keyfunc)
pprint.pprint(AllMyProjectsFromaDatasource)
输出为:
[{'id': 9, 'name': 'one project', 'otherkey': 'othervalue'},
{'id': 3, 'name': 'project one', 'otherkey': 'othervalue'},
{'id': 5, 'name': 'project two', 'otherkey': 'othervalue'},
{'id': 1, 'name': 'project three', 'otherkey': 'othervalue'},
{'id': 6, 'name': 'abc project', 'otherkey': 'othervalue'}]
你能给我举一个你的字典的例子吗?从其他的解决方案来看,我仍然错过了我的第二个问题:我只想选择“一”,所以无论它的名字中包含什么项目“一”,都会排在第一位。