python集合和字典创建通讯录_如何创建一个字典,将字符串映射到给定列表和元组的集合?...

说实话,我想说你的问题是你的代码变得太麻烦了。抵制“一衬”的诱惑,创造一个功能。一切都会变得更容易!在mydataset = (

('Amy', '1', None, '1'),

('Kat', None, '1', '1'),

('Leo', None, None, '1')

)

def states(cols, data):

"""

This function receives one of the tuples with data and returns a pair

where the first element is the name from the tuple, and the second

element is a set with all matched states. Well, at least *I* think

it is more readable :)

"""

name = data[0]

states = set(state for state, value in zip(cols, data) if value == '1')

return name, states

pairs = (states(state_cols, data) for data in mydataset)

# Since dicts can receive an iterator which yields pairs where the first one

# will become a key and the second one will become the value, I just pass

# a list with all pairs to the dict constructor.

print dict(pairs)

结果是:

^{pr2}$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值