如何在字典中找到共享相同键的值?

时间:2016-03-22 19:10:37

标签: python dictionary

我希望输入为actorsInCommon(movies, "Moneyball", "Oceans Eleven")

我希望输出为["Brad Pitt", "Joe Smith"]

movies = {"Moneyball": ["Brad Pitt", "Jonah Hill", "Joe Smith"], 
          "Oceans Eleven": ["Brad Pitt", "Joe Smith", "George Clooney"]}

def find(key, dictionary):
    for (k, value) in dictionary:
        if key == k:
            return value
    return None

def actorsInCommon(dictionary, movie1, movie2):
    movie1Cast = find(movie1, dictionary)
    movie2Cast = find(movie2, dictionary)
    return list(set(movie1Cast).intersection(movie2Cast))

3 个答案:

答案 0 :(得分:4)

movies = {"Moneyball": ["Brad Pitt", "Jonah Hill", "Joe Smith"], 
          "Oceans Eleven": ["Brad Pitt", "Joe Smith", "George Clooney"]}

从两个词典中的值中创建两个集合的交集。对于缺少的电影名称,请将get()与默认的空列表(如果使用将转换为集合)一起使用。转换为匹配所需输出的列表:

def actorsInCommon(dictionary, movie1, movie2):
    return list(set(dictionary.get(movie1, [])) & set(dictionary.get(movie2, [])))

>>> actorsInCommon(movies, "Moneyball", "Oceans Eleven")
['Brad Pitt', 'Joe Smith']

答案 1 :(得分:1)

您想使用集合:

>>> movies = {"Moneyball": ["Brad Pitt", "Jonah Hill", "Joe Smith"], 
              "Oceans Eleven": ["Brad Pitt", "Joe Smith", "George Clooney"]}    
>>> set(movies['Moneyball']) & set(movies['Oceans Eleven'])
set(['Brad Pitt', 'Joe Smith'])

您可能希望更改dict以存储集合本身,因此您不必将它们转换为集合(请注意{}设置文字):

>>> movies = {"Moneyball": {"Brad Pitt", "Jonah Hill", "Joe Smith"}, 
              "Oceans Eleven": {"Brad Pitt", "Joe Smith", "George Clooney"}}    
>>> movies['Moneyball'] & movies['Oceans Eleven']
set(['Brad Pitt', 'Joe Smith'])

答案 2 :(得分:1)

如果您确实想检查所有值:

send: 'POST /upload/groups/v1/groups/group-name%40lists.domain.com/archive?uploadType=media&alt=json HTTP/1.1\r\nHost: www.googleapis.com\r\ncontent-length: 711\r\naccept-encoding: gzip, deflate\r\naccept: application/json\r\nuser-agent: google-api-python-client/1.4.0 (gzip)\r\ncontent-type: message/rfc822\r\nauthorization: Bearer *******\r\n\r\nDate: Mon, 10 Feb 2014 10:58:41 -0600\nReply-To: Bob\'s test list <GROUP-NAME@LISTSERV.DOMAIN.COM>,\n Bob Boberson <bob@EXCHANGE.DOMAIN.COM>\nSender: Bob\'s test list <bob@LISTSERV.DOMAIN.COM>\nFrom: Bob Boberson <bob@EXCHANGE.DOMAIN.COM>\nSubject: blah blah blah\nMime-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nContent-Type: text/plain; charset="UTF-8"\nMessage-ID: <6949565692507828.WA.bobexchange.domain.com@listserv.domain.com>\n\n\nTest message.'

reply: 'HTTP/1.1 500 Internal Server Error\r\n'

header: X-GUploader-UploadID: *****
header: Vary: Origin
header: Vary: X-Origin
header: Content-Type: application/json; charset=UTF-8
header: Content-Length: 177
header: Date: Wed, 23 Mar 2016 21:10:20 GMT
header: Server: UploadServer
header: Alternate-Protocol: 443:quic,p=1
header: Alt-Svc: quic=":443"; ma=2592000; v="31,30,29,28,27,26,25"

或者只需要将两个键投射到一个集合中并使用 set.intersection

movies = {"Moneyball": ["Brad Pitt", "Jonah Hill", "Joe Smith"],
          "Oceans Eleven": ["Brad Pitt", "Joe Smith", "George Clooney"]}


print(set.intersection(*map(set, movies.values())))
set(['Brad Pitt', 'Joe Smith'])