找到重叠的键


map(function, iterable, ...)

Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed,function must take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended withNoneitems. If function isNone, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.


python中的reduce内建函数是一个二元操作函数,他用来将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给reduce中的函数 func()(必须是一个二元操作函数)先对集合中的第1,2个数据进行操作,得到的结果再与第三个数据用func()函数运算,最后得到一个结果。

from random import randint

from random import sample
s=[1,2,3]




s1= {x:randint(1,4) for x in sample('abcdefg',randint(3,6))}
s2= {x:randint(1,4) for x in sample('abcdefg',randint(3,6))}
s3= {x:randint(1,4) for x in sample('abcdefg',randint(3,6))}
res=[]
for k in s1:
    if k in s2 and k in s3:
        res.append(k)
print res


c= s1.viewkeys() & s2.viewkeys() &s3.viewkeys()
print c


c2=map(dict.viewkeys,[s1,s2,s3])
print c2


c3=reduce(lambda a ,b :a & b,map(dict.viewkeys,[s1,s2,s3]))
print c3 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值