Python: Tuples/dictionaries as keys, select, sort

后端 未结 8 1713
独厮守ぢ
独厮守ぢ 2020-12-07 07:53

Suppose I have quantities of fruits of different colors, e.g., 24 blue bananas, 12 green apples, 0 blue strawberries and so on. I\'d like to organize them in a data structur

8条回答
  •  借酒劲吻你
    2020-12-07 08:34

    Database, dict of dicts, dictionary of list of dictionaries, named tuple (it's a subclass), sqlite, redundancy... I didn't believe my eyes. What else ?

    "It might well be that dictionaries with tuples as keys are not the proper way to handle this situation."

    "my gut feeling is that a database is overkill for the OP's needs; "

    Yeah! I thought

    So, in my opinion, a list of tuples is plenty enough :

    from operator import itemgetter
    
    li = [  ('banana',     'blue'   , 24) ,
            ('apple',      'green'  , 12) ,
            ('strawberry', 'blue'   , 16 ) ,
            ('banana',     'yellow' , 13) ,
            ('apple',      'gold'   , 3 ) ,
            ('pear',       'yellow' , 10) ,
            ('strawberry', 'orange' , 27) ,
            ('apple',      'blue'   , 21) ,
            ('apple',      'silver' , 0 ) ,
            ('strawberry', 'green'  , 4 ) ,
            ('banana',     'brown'  , 14) ,
            ('strawberry', 'yellow' , 31) ,
            ('apple',      'pink'   , 9 ) ,
            ('strawberry', 'gold'   , 0 ) ,
            ('pear',       'gold'   , 66) ,
            ('apple',      'yellow' , 9 ) ,
            ('pear',       'brown'  , 5 ) ,
            ('strawberry', 'pink'   , 8 ) ,
            ('apple',      'purple' , 7 ) ,
            ('pear',       'blue'   , 51) ,
            ('chesnut',    'yellow',  0 )   ]
    
    
    print set( u[1] for u in li ),': all potential colors'
    print set( c for f,c,n in li if n!=0),': all effective colors'
    print [ c for f,c,n in li if f=='banana' ],': all potential colors of bananas'
    print [ c for f,c,n in li if f=='banana' and n!=0],': all effective colors of bananas'
    print
    
    print set( u[0] for u in li ),': all potential fruits'
    print set( f for f,c,n in li if n!=0),': all effective fruits'
    print [ f for f,c,n in li if c=='yellow' ],': all potential fruits being yellow'
    print [ f for f,c,n in li if c=='yellow' and n!=0],': all effective fruits being yellow'
    print
    
    print len(set( u[1] for u in li )),': number of all potential colors'
    print len(set(c for f,c,n in li if n!=0)),': number of all effective colors'
    print len( [c for f,c,n in li if f=='strawberry']),': number of potential colors of strawberry'
    print len( [c for f,c,n in li if f=='strawberry' and n!=0]),': number of effective colors of strawberry'
    print
    
    # sorting li by name of fruit
    print sorted(li),'  sorted li by name of fruit'
    print
    
    # sorting li by number 
    print sorted(li, key = itemgetter(2)),'  sorted li by number'
    print
    
    # sorting li first by name of color and secondly by name of fruit
    print sorted(li, key = itemgetter(1,0)),'  sorted li first by name of color and secondly by name of fruit'
    print
    

    result

    set(['blue', 'brown', 'gold', 'purple', 'yellow', 'pink', 'green', 'orange', 'silver']) : all potential colors
    set(['blue', 'brown', 'gold', 'purple', 'yellow', 'pink', 'green', 'orange']) : all effective colors
    ['blue', 'yellow', 'brown'] : all potential colors of bananas
    ['blue', 'yellow', 'brown'] : all effective colors of bananas
    
    set(['strawberry', 'chesnut', 'pear', 'banana', 'apple']) : all potential fruits
    set(['strawberry', 'pear', 'banana', 'apple']) : all effective fruits
    ['banana', 'pear', 'strawberry', 'apple', 'chesnut'] : all potential fruits being yellow
    ['banana', 'pear', 'strawberry', 'apple'] : all effective fruits being yellow
    
    9 : number of all potential colors
    8 : number of all effective colors
    6 : number of potential colors of strawberry
    5 : number of effective colors of strawberry
    
    [('apple', 'blue', 21), ('apple', 'gold', 3), ('apple', 'green', 12), ('apple', 'pink', 9), ('apple', 'purple', 7), ('apple', 'silver', 0), ('apple', 'yellow', 9), ('banana', 'blue', 24), ('banana', 'brown', 14), ('banana', 'yellow', 13), ('chesnut', 'yellow', 0), ('pear', 'blue', 51), ('pear', 'brown', 5), ('pear', 'gold', 66), ('pear', 'yellow', 10), ('strawberry', 'blue', 16), ('strawberry', 'gold', 0), ('strawberry', 'green', 4), ('strawberry', 'orange', 27), ('strawberry', 'pink', 8), ('strawberry', 'yellow', 31)]   sorted li by name of fruit
    
    [('apple', 'silver', 0), ('strawberry', 'gold', 0), ('chesnut', 'yellow', 0), ('apple', 'gold', 3), ('strawberry', 'green', 4), ('pear', 'brown', 5), ('apple', 'purple', 7), ('strawberry', 'pink', 8), ('apple', 'pink', 9), ('apple', 'yellow', 9), ('pear', 'yellow', 10), ('apple', 'green', 12), ('banana', 'yellow', 13), ('banana', 'brown', 14), ('strawberry', 'blue', 16), ('apple', 'blue', 21), ('banana', 'blue', 24), ('strawberry', 'orange', 27), ('strawberry', 'yellow', 31), ('pear', 'blue', 51), ('pear', 'gold', 66)]   sorted li by number
    
    [('apple', 'blue', 21), ('banana', 'blue', 24), ('pear', 'blue', 51), ('strawberry', 'blue', 16), ('banana', 'brown', 14), ('pear', 'brown', 5), ('apple', 'gold', 3), ('pear', 'gold', 66), ('strawberry', 'gold', 0), ('apple', 'green', 12), ('strawberry', 'green', 4), ('strawberry', 'orange', 27), ('apple', 'pink', 9), ('strawberry', 'pink', 8), ('apple', 'purple', 7), ('apple', 'silver', 0), ('apple', 'yellow', 9), ('banana', 'yellow', 13), ('chesnut', 'yellow', 0), ('pear', 'yellow', 10), ('strawberry', 'yellow', 31)]   sorted li first by name of color and secondly by name of fruit
    

提交回复
热议问题