1.在leetcode上刷題的時候,經常需要使用統計元素個數,例如
words = [“a”,“b”,“c”,“d”,“a”,“c”,“r”]
統計列表words中每個元素及其出現的次數
words = [ "a" , "b" , "c" , "d" , "a" , "c" , "r" , "r" ]
from collections import Counter
words_dict_1 = Counter( words)
print ( words_dict_1. most_common( ) )
print ( words_dict_1. most_common( 3 ) )
words_dict_2 = { }
for word in words:
if word not in words_dict_2:
words_dict_2[ word] = 1
else :
words_dict_2[ word] += 1
words_dict_3 = collections. defaultdict( int )
for word in words:
words_dict_3[ word] += 1
words_dict_4 = { }
for word in words:
words_dict_4[ word] = words_dict_4. get( word, 0 ) + 1
print ( words_dict_1== words_dict_2== words_dict_3== words_dict_4)
print ( words_dict_2)
1.1 在用1中的方法統計好了每個元素出現的次數的時候,有時需要按某個順序來遍歷dict中的元素。
print ( words_dict_1. most_common( ) )
1.2 關於添加元素到dict中,若元素不在字典中的情況,以leetcode-49爲例
class Solution ( object ) :
def groupAnagrams ( self, strs) :
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
ans = { }
for s in strs:
ans[ tuple ( sorted ( s) ) ] = ans. get( tuple ( sorted ( s) ) , [ ] ) + [ s]
return ans. values( )
2.字典的查找
min ( record, key= record. get) , 返回最小的value對應的key,如果有多個value相同,只返回第一個。
max ( record, key= record. get) ,返回最大的value對應的key,如果有多個value相同,只返回第一個。
3.字典的排序
sort()方法只能用於list的排序,具體用法爲:
alist = [ 1 , 2 , 3 , 4 , 2 , 1 , 5 , 6 , 7 ]
alist. sort( )
print ( alist)
要想對字典,字符串,set等進行排序,可以使用sorted()函數,sorted 可以對所有可迭代的對象進行排序操作。返回一個新的list(注意,不管操作什麼對象,都返回list)
>> > alist = [ 1 , 5 , 4 , 3 , 2 ]
>> > sorted ( alist)
[ 1 , 2 , 3 , 4 , 5 ]
>> > strs = "15423aweffASW"
>> > sorted ( strs)
[ '1' , '2' , '3' , '4' , '5' , 'A' , 'S' , 'W' , 'a' , 'e' , 'f' , 'f' , 'w' ]
>> > record = { 1 : 4 , 6 : 3 , 2 : 8 , 9 : 1 , 4 : 10 }
>> > sorted ( record)
[ 1 , 2 , 4 , 6 , 9 ]
>> > sorted ( record. items( ) )
[ ( 1 , 4 ) , ( 2 , 8 ) , ( 4 , 10 ) , ( 6 , 3 ) , ( 9 , 1 ) ]
Python2:sorted(iterable, cmp=None, key=None, reverse=False)
Python3:sorted(iterable, key=None, reverse=False)
注:cmp可以傳入兩個參數進行比較,key只能傳入一個參數進行比較
>> > record = { 1 : 4 , 6 : 3 , 2 : 8 , 9 : 1 , 4 : 10 , 5 : 4 , 8 : 1 , 10 : 1 }
>> > sorted ( record. items( ) , key= lambda x: x[ 1 ] )
[ ( 8 , 1 ) , ( 9 , 1 ) , ( 10 , 1 ) , ( 6 , 3 ) , ( 1 , 4 ) , ( 5 , 4 ) , ( 2 , 8 ) , ( 4 , 10 ) ]
>> > sorted ( record. items( ) , cmp = lambda x, y: cmp ( x[ 1 ] , y[ 1 ] ) )
> [ ( 8 , 1 ) , ( 9 , 1 ) , ( 10 , 1 ) , ( 6 , 3 ) , ( 1 , 4 ) , ( 5 , 4 ) , ( 2 , 8 ) , ( 4 , 10 ) ]
>> > import functools
>> > sorted ( record. items( ) , key= functools. cmp_to_key( lambda x, y: x[ 1 ] - y[ 1 ] ) )
[ ( 8 , 1 ) , ( 9 , 1 ) , ( 10 , 1 ) , ( 6 , 3 ) , ( 1 , 4 ) , ( 5 , 4 ) , ( 2 , 8 ) , ( 4 , 10 ) ]
>> > sorted ( record. items( ) , key= lambda x: x[ 0 ] )
[ ( 1 , 4 ) , ( 2 , 8 ) , ( 4 , 10 ) , ( 5 , 4 ) , ( 6 , 3 ) , ( 8 , 1 ) , ( 9 , 1 ) , ( 10 , 1 ) ]
>> > sorted ( record. items( ) , key= lambda x: ( x[ 0 ] , - x[ 1 ] ) )
[ ( 1 , 4 ) , ( 2 , 8 ) , ( 4 , 10 ) , ( 5 , 4 ) , ( 6 , 3 ) , ( 8 , 1 ) , ( 9 , 1 ) , ( 10 , 1 ) ]
>> > sorted ( record. items( ) , key= lambda x: ( - x[ 0 ] , x[ 1 ] ) )
[ ( 10 , 1 ) , ( 9 , 1 ) , ( 8 , 1 ) , ( 6 , 3 ) , ( 5 , 4 ) , ( 4 , 10 ) , ( 2 , 8 ) , ( 1 , 4 ) ]
>> > sorted ( record. items( ) , key= lambda x: ( x[ 1 ] , x[ 0 ] ) )
[ ( 8 , 1 ) , ( 9 , 1 ) , ( 10 , 1 ) , ( 6 , 3 ) , ( 1 , 4 ) , ( 5 , 4 ) , ( 2 , 8 ) , ( 4 , 10 ) ]
>> > sorted ( record. items( ) , key= lambda x: ( x[ 1 ] , - x[ 0 ] ) )
[ ( 10 , 1 ) , ( 9 , 1 ) , ( 8 , 1 ) , ( 6 , 3 ) , ( 5 , 4 ) , ( 1 , 4 ) , ( 2 , 8 ) , ( 4 , 10 ) ]
>> > sorted ( record. items( ) , key= lambda x: ( x[ 0 ] + x[ 1 ] , - x[ 0 ] ) )
[ ( 1 , 4 ) , ( 8 , 1 ) , ( 6 , 3 ) , ( 5 , 4 ) , ( 9 , 1 ) , ( 2 , 8 ) , ( 10 , 1 ) , ( 4 , 10 ) ]
class Solution ( object ) :
def largestNumber ( self, nums) :
"""
:type nums: List[int]
:rtype: str
"""
if not nums:
return ""
numbers = [ str ( number) for number in nums]
numbers. sort( cmp = lambda x, y: cmp ( y + x, x + y) )
return "" . join( numbers) . lstrip( '0' ) or '0'
class Solution :
def largestNumber ( self, nums: List[ int ] ) - > str :
import functools
if not nums:
return ""
numbers = [ str ( number) for number in nums]
numbers. sort( key= functools. cmp_to_key( lambda x, y: int ( y + x) - int ( x + y) ) )
return "" . join( numbers) . lstrip( '0' ) or '0'
a = [ 5 , 5 , 4 , 4 , 6 , 7 , 5 , 8 , 9 , 9 , 2 , 3 , 1 ]
返回的結果是: [ 5 , 4 , 6 , 7 , 8 , 9 , 2 , 3 , 1 ]
使用下面語句即可:
sorted ( list ( set ( a) ) , key= a. index)