【2017-09-07】数据结构与算法(六)

2017-09-07  本文已影响0人  小蜗牛的成长

序列

>>> items = [0, 1, 2, 3, 4, 5, 6]
>>>#一般写法
>>> items[2:4]
[2, 3]
>>>#使用切片函数
>>> pice=slice(2,4)
>>> items[pice]
[2, 3]
>>> items[2:4]=[6,7]
>>> items
[0, 1, 6, 7, 4, 5, 6]
>>> items[pice]=[9,10]
>>> items
[0, 1, 9, 10, 4, 5, 6]
>>> pice.start
2
>>> pice.indices(len(items))
(2, 4, 1)
>>>#切片对象值stop最大为序列长度
>>> pice=slice(2,10)
>>> items[pice]
[9, 10, 4, 5, 6]
>>> pice.indices(len(items))
(2, 7, 1)
>>>#注意*的使用
>>> for i in range(*pice.indices(len(items))):
    print(items[i])
    
9
10
4
5
6
>>> from collections import Counter
>>> words = [
'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
'my', 'eyes', "you're", 'under'
]
>>> words_count=Counter(words)
>>> words_count
Counter({'eyes': 8, 'the': 5, 'look': 4, 'into': 3, 'my': 3, 'around': 2, "don't": 1, "you're": 1, 'not': 1, 'under': 1})
>>> words_count['eyes']
8
>>> words_count.most_common(1)
[('eyes', 8)]

另外,Counter 实例一个鲜为人知的特性是它们可以很容易的跟数学运算操作相结合。

>>> from collections import Counter
>>> words = [
'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
'my', 'eyes', "you're", 'under'
]
>>> moreword=['why','are','you','not','looking','in','my','eyes']
>>> words_count=Counter(words)
>>> moreword_count=Counter(moreword)
>>> words_count
Counter({'eyes': 8, 'the': 5, 'look': 4, 'into': 3, 'my': 3, 'around': 2, "don't": 1, "you're": 1, 'not': 1, 'under': 1})
>>> moreword_count
Counter({'in': 1, 'eyes': 1, 'looking': 1, 'why': 1, 'my': 1, 'not': 1, 'you': 1, 'are': 1})
>>> words_count+moreword_count
Counter({'eyes': 9, 'the': 5, 'look': 4, 'my': 4, 'into': 3, 'not': 2, 'around': 2, 'looking': 1, 'under': 1, 'you': 1, 'in': 1, "don't": 1, 'are': 1, "you're": 1, 'why': 1})
>>> words_count-moreword_count
Counter({'eyes': 7, 'the': 5, 'look': 4, 'into': 3, 'my': 2, 'around': 2, "don't": 1, 'under': 1, "you're": 1})
>>> 
>>> rows = [
{'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
{'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
{'fname': 'John', 'lname': 'Cleese', 'uid': 1001},
{'fname': 'Big', 'lname': 'Jones', 'uid': 1004}
]
>>> sorted(rows,key=lambda r:r["fname"])
[{'fname': 'Big', 'uid': 1004, 'lname': 'Jones'}, {'fname': 'Brian', 'uid': 1003, 'lname': 'Jones'}, {'fname': 'David', 'uid': 1002, 'lname': 'Beazley'}, {'fname': 'John', 'uid': 1001, 'lname': 'Cleese'}]
>>> sorted(rows,key=lambda r:(r["fname"],r["uid"]))
[{'fname': 'Big', 'uid': 1004, 'lname': 'Jones'}, {'fname': 'Brian', 'uid': 1003, 'lname': 'Jones'}, {'fname': 'David', 'uid': 1002, 'lname': 'Beazley'}, {'fname': 'John', 'uid': 1001, 'lname': 'Cleese'}]
>>> 

运用itemgetter() 函数,它负责创建一个 callable 对象,传递多个参数时,函数返回一个元组,多条件排序的顺序将按照元组的顺序。
示例1

#上述示例可变为:
>>> from operator import itemgetter
>>> rows_by_fname = sorted(rows, key=itemgetter('fname'))
>>> rows_by_fname_and_uid=sorted(rows, key=itemgetter('fname',"uid"))
>>> rows_by_fname
[{'fname': 'Big', 'uid': 1004, 'lname': 'Jones'}, {'fname': 'Brian', 'uid': 1003, 'lname': 'Jones'}, {'fname': 'David', 'uid': 1002, 'lname': 'Beazley'}, {'fname': 'John', 'uid': 1001, 'lname': 'Cleese'}]
>>> rows_by_fname_and_uid
[{'fname': 'Big', 'uid': 1004, 'lname': 'Jones'}, {'fname': 'Brian', 'uid': 1003, 'lname': 'Jones'}, {'fname': 'David', 'uid': 1002, 'lname': 'Beazley'}, {'fname': 'John', 'uid': 1001, 'lname': 'Cleese'}]
>>> #key接收一个函数,指明按照什么排序
>>> min(rows, key=itemgetter('uid'))
{'fname': 'John', 'uid': 1001, 'lname': 'Cleese'}
>>> 

示例2

>>> from operator import itemgetter

>>> students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]

>>> sorted(students, key = itemgetter(2))

[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
>>> 
上一篇下一篇

猜你喜欢

热点阅读