python字典的键可以是元组吗_Python:元组/字典作为键,select,s

数据库,dict的dict,dictionary列表的dictionary,命名元组(它是一个子类),sqlite,冗余。。。

我不相信我的眼睛。还有什么?"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; "

是啊!我以为

所以,在我看来,一个元组列表就足够了: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

结果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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值