python+字典的遍历与排序以及后续的学习记录

来源:互联网 发布:手机淘宝买彩票 编辑:程序博客网 时间:2024/05/22 12:50

字典的遍历

dict_ = {'a':2,'b':3,'c':6}dict.items()
dict_items([('a', 2), ('b', 3), ('c', 6)])
dict_.iteritems()#python2中的应用
---------------------------------------------------------------------------AttributeError                            Traceback (most recent call last)<ipython-input-7-05408c3fc25a> in <module>()----> 1 dict_.iteritems()#python2中的应用AttributeError: 'dict' object has no attribute 'iteritems'
for item in dict_.items():    print(item)
('a', 2)('b', 3)('c', 6)

字典的排序

dict_1 = sorted(dict_)print(dict_1)
['a', 'b', 'c']
dict_1 = sorted(dict_.items())print(dict_1)
[('a', 2), ('b', 3), ('c', 6)]
dict_2 = sorted(dict_,key = lambda k: dict_[k])print(dict_2)
['a', 'b', 'c']

operator.itemgetter函数用于获取对象的哪些维数据参数为一些序号

from operator import itemgetterstudents = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]stuends_sorted= sorted(students, key=lambda student : student[2],reverse=False)print(stuends_sorted)stuends_sorted= sorted(students, key=itemgetter(2),reverse=True)print(stuends_sorted)
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)][('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
stuends_sorted= sorted(students, key=itemgetter(1,2),reverse=True)print(stuends_sorted)
[('jane', 'B', 12), ('dave', 'B', 10), ('john', 'A', 15)]
class Student:    def __init__(self, name, grade, age):            self.name = name            self.grade = grade            self.age = age    def __repr__(self):            return repr((self.name, self.grade, self.age))students = [    Student('jane', 'B', 12),    Student('john', 'A', 12),    Student('dave', 'B', 10),]
from operator import itemgetter, attrgetter# 对students按照年龄排序print(sorted(students, key=attrgetter('age')))# 其等价于print (sorted(students, key=lambda o: o.age))# 输出: >>> [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]# 亦可以按多个key排序, 先按age再按grade排序print (sorted(students, key=attrgetter('age', 'grade')))# 输出: >>> [('dave', 'B', 10), ('john', 'A', 12), ('jane', 'B', 12)]#print (sorted(students, key=itemgetter('age', 'grade')))这样的用法不对
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 12)][('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 12)][('dave', 'B', 10), ('john', 'A', 12), ('jane', 'B', 12)]

itertools.groupy()把迭代器中相邻的重复的元素挑出来放在一起。itertools提供的处理全部是处理迭代功能的函数,它们的返回值不是list,而是Iterator,只有用for循环迭代的时候才真正计算。

rows = [{'address' :'5412 N CLARK','date':'07/01/2012'},        {'address' :'5148 N CLARK','date' :'07/04/2012'},        {'address' :'5800 E 58TH','date': '07/02/2012' },        {'address' :'2122 N CLARK','date': '07/02/2012'},        {'address' :'5645 N RAVENSWOOD','date' :'07/02/2012' },        {'address' :'1060 W ADDISON','date': '07/02/2012' },        {'address': '1039 W GRANVILE','date': '07/04/2012'}       ]from operator import itemgetterfrom itertools import groupbyrows.sort(key = itemgetter('date'))print(rows)
[{'date': '07/01/2012', 'address': '5412 N CLARK'}, {'date': '07/02/2012', 'address': '5800 E 58TH'}, {'date': '07/02/2012', 'address': '2122 N CLARK'}, {'date': '07/02/2012', 'address': '5645 N RAVENSWOOD'}, {'date': '07/02/2012', 'address': '1060 W ADDISON'}, {'date': '07/04/2012', 'address': '5148 N CLARK'}, {'date': '07/04/2012', 'address': '1039 W GRANVILE'}]
for date ,items in groupby(rows,key = itemgetter('date')):    print(date)    for i in items:        print(i)
07/01/2012{'date': '07/01/2012', 'address': '5412 N CLARK'}07/02/2012{'date': '07/02/2012', 'address': '5800 E 58TH'}{'date': '07/02/2012', 'address': '2122 N CLARK'}{'date': '07/02/2012', 'address': '5645 N RAVENSWOOD'}{'date': '07/02/2012', 'address': '1060 W ADDISON'}07/04/2012{'date': '07/04/2012', 'address': '5148 N CLARK'}{'date': '07/04/2012', 'address': '1039 W GRANVILE'}
from collections import defaultdictrows_by_date = defaultdict(list)for row in rows:    print(row)    rows_by_date[row['date']].append(row)
{'date': '07/01/2012', 'address': '5412 N CLARK'}{'date': '07/02/2012', 'address': '5800 E 58TH'}{'date': '07/02/2012', 'address': '2122 N CLARK'}{'date': '07/02/2012', 'address': '5645 N RAVENSWOOD'}{'date': '07/02/2012', 'address': '1060 W ADDISON'}{'date': '07/04/2012', 'address': '5148 N CLARK'}{'date': '07/04/2012', 'address': '1039 W GRANVILE'}
for item in rows_by_date.items():    print(item)
('07/04/2012', [{'date': '07/04/2012', 'address': '5148 N CLARK'}, {'date': '07/04/2012', 'address': '1039 W GRANVILE'}])('07/02/2012', [{'date': '07/02/2012', 'address': '5800 E 58TH'}, {'date': '07/02/2012', 'address': '2122 N CLARK'}, {'date': '07/02/2012', 'address': '5645 N RAVENSWOOD'}, {'date': '07/02/2012', 'address': '1060 W ADDISON'}])('07/01/2012', [{'date': '07/01/2012', 'address': '5412 N CLARK'}])
for r in rows_by_date['07/02/2012']:    print(r)
{'date': '07/02/2012', 'address': '5800 E 58TH'}{'date': '07/02/2012', 'address': '2122 N CLARK'}{'date': '07/02/2012', 'address': '5645 N RAVENSWOOD'}{'date': '07/02/2012', 'address': '1060 W ADDISON'}
原创粉丝点击