python:组合sort-key-functions itemgetter和str.lower [英] python: combine sort-key-functions itemgetter and str.lower

查看:252
本文介绍了python:组合sort-key-functions itemgetter和str.lower的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 <$ 

我想按字典键对字典列表进行排序,我不想区分大写字母和小写字符。 c $ c> dict1 = {'name':'peter','phone':'12355'}
dict2 = {'name':'Paul','phone':'545435'}
dict3 = {'name':'klaus','phone':'55345'}
dict4 = {'name':'Krishna','phone':'12345'}
dict5 = {'名字':'Ali','phone':'53453'}
dict6 = {'name':'Hans','phone':'765756'}
list_of_dicts = [dict1,dict2,dict3 ,dict4,dict5,dict6]

key_field ='name'
list_of_dicts.sort(key = itemgetter(key_field))
#如何将key = itemgetter(key_field)和关键= str.lower?
for list_field in list_of_dicts:
print list_field [key_field]

应该提供

  Ali,Hans,klaus,Krishna,Paul,peter 

而不是

 克劳斯,彼得,阿里,汉斯,克里希纳,保罗


解决方案

在一般情况下,您需要编写用于分类目的的密钥提取功能;只有在特殊情况下(尽管很重要),您可以重新使用现有的调用方法来为您提取密钥,或者只需结合现有的一些调用(使用 lambda的快速和肮脏方式,因为没有内置的函数组合方法)。



如果您经常需要执行这两种键提取操作(获取一个项目并调用该项目的方法),我建议:

  def combiner(itemkey,methodname,* a, ** k):
def keyextractor(容器):
item = container [itemkey]
method = getattr(item,methodname)
返回方法(* a,** k )
return keyextractor

so listofdicts.sort(key = combiner ('name','lower'))将适用于您的情况。

请注意,过度泛化会导致成本,雅致和中等泛化(将项目键,方法名和方法参数(如果有的话)作为runtime-deter在这种情况下开采)通常具有优点 - 一个通用功能,不比十几个特定和专门的功能(使用提取器,方法调用,或两者,在他们的代码中硬连线)更复杂,将更容易维护(和,当然,重用起来要容易得多! - )。


I want to sort a list of dictionaries by dictionary key, where I don't want to distinguish between upper and lower case characters.

dict1 = {'name':'peter','phone':'12355'}
dict2 = {'name':'Paul','phone':'545435'}
dict3 = {'name':'klaus','phone':'55345'}
dict4 = {'name':'Krishna','phone':'12345'}
dict5 = {'name':'Ali','phone':'53453'}
dict6 = {'name':'Hans','phone':'765756'}
list_of_dicts = [dict1,dict2,dict3,dict4,dict5,dict6]

key_field = 'name'
list_of_dicts.sort(key=itemgetter(key_field))
# how to combine key=itemgetter(key_field) and key=str.lower? 
for list_field in list_of_dicts:
    print list_field[key_field]

should provide

Ali, Hans, klaus, Krishna, Paul, peter

and not

klaus, peter, Ali, Hans, Krishna, Paul

解决方案

In the general case, you'll want to write your key-extraction function for sorting purposes; only in special (though important) cases it happens that you can just reuse an existing callable to extract the keys for you, or just conjoin a couple of existing ones (in a "quick and dirty" way using lambda, since there's no built-in way to do function composition).

If you often need to perform these two kinds of operations for key extraction (get an item and call a method on that item), I suggest:

def combiner(itemkey, methodname, *a, **k):
  def keyextractor(container):
    item = container[itemkey]
    method = getattr(item, methodname)
    return method(*a, **k)
  return keyextractor

so listofdicts.sort(key=combiner('name', 'lower')) will work in your case.

Note that while excessive generalization has costs, tasteful and moderate generalization (leaving the item key, method name, and method arguments if any, as runtime-determined, in this case) generally has benefits -- one general function, not more complex than a dozen specific and specialized ones (with the extractor, method to call, or both, hardwired in their code), will be easier to maintain (and, of course, much easier to reuse!-).

这篇关于python:组合sort-key-functions itemgetter和str.lower的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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