简体   繁体   English

比较Python字典中的集合

[英]Comparing Sets held in a Dictionary in Python

I've got the following which takes multiple lines of user input such as 'English Bob Luke' to store a Set in a Dictionary about which people speak which language. 我得到以下内容,这些内容需要多行用户输入,例如“ English Bob Luke”,以将Set存储在词典中,有关哪些人说哪种语言。 I've used a Dictionary to hold the multiple lines of input to create multiple Sets, but I now need to compare the difference between Sets to see if someone only speaks one language. 我使用了一个字典来容纳多行输入以创建多个Set,但是现在我需要比较Set之间的差异,以查看是否有人只会说一种语言。

languages = {}
while True:
  info = input('Line: ').split()
  if info != []:
    languages[info[0]] = set(info[1:])
  else:
    break

I can print the sets using the code below, but it doesn't seem to really get me anywhere! 我可以使用下面的代码打印这些集,但似乎并没有真正使我受益!

for tongue in languages:
  print(set(languages[tongue]))

Totally stuck - any help would be greatly appreciated! 完全卡住-任何帮助将不胜感激!

UPDATE UPDATE

Here in example of what I am trying to achieve: 这里是我要实现的示例:

Line: English Tim Nicky James John Ben
Line: German Nicky Tim Tara
Line: Mandarin Tim John
Line: 
James is monolingual.
Tara is monolingual.
Ben is monolingual.

SOLUTION

Completely re-though my approach and ditched the Dictionary! 完全重新考虑我的方法,放弃了字典! :

english = input("Line: ").split()      
en_speakers = set(english[1:len(english)])

multi_speakers = set()

while True:
  language = input("Line: ").split()
  lan_speakers = language[1:len(language)] 
  if language == []:
    break
  else:
    multi_speakers |= set(lan_speakers)

monolinguals = en_speakers.difference(multi_speakers)

for person in monolinguals:
  print(person, 'is monolingual.')

if multi_speakers == en_speakers:
  print ('Everyone is multilingual!')

I'd reverse the order of keys and values in your dictionary, because it makes the problem so much easier. 我颠倒了字典中键和值的顺序,因为它使问题变得更加容易。 So rather than storing per language the different users that speak it, just create a per user profile of all the languages that person speaks: 因此,与其存储每种语言的不同用户,不如创建一个该用户所使用的所有语言的每个用户的个人资料:

from collections import defaultdict

user_langs = defaultdict(list)

while True:
    info = map(lambda s: s.strip(), raw_input('Line: ').split())
    if info:
        for user in info[1:]:
            user_langs[user].append(info[0])
    else:
        break

for user in user_langs:
    if len(user_langs[user]) < 2:
        print("{} is monolingual.".format(user))

languages[toungue] is already a set, you don't need to set(languages[touge]) . languages[toungue]已经设置好了,您不需要set(languages[touge]) Also you don't need to loop the dictionary you can simply get those set from dictionary by lanuages.values() . 同样,您也不需要循环字典,只需通过lanuages.values()从字典中获取那些集合lanuages.values()

Not entirely sure what you want to achieve here though. 虽然不完全确定您要在这里实现什么。 At a wild guess, you may want the unique value from the languages values? 也许您会想从语言值中获得唯一值?

You can achieve this by updating values to a new set: 您可以通过将值更新为新集合来实现此目的:

Change this: 更改此:

for tongue in languages:
  print(set(languages[tongue]))

To this: 对此:

new_set = set()
for v in languages.values():
    new_set.update(v)

new_set # dummy lanugages
{'English', 'Japanese', 'whatever', 'yeah'}

Updated 更新

To achieve what you want, you can use Counter and return the key if value == 1. 要实现所需的功能,可以使用Counter并在value == 1时返回键。

A more detailed explanation is that, under a for/loop you are going to compare 1 set to another set. 更详细的解释是,在for/loop您将比较1套与另一套。 But what you actually need,is to compare all sets, so I choose to use c to update all individual set values under a for/loop , and afterward do whatever you want out from that c . 但是您真正需要的是比较所有集合,因此我选择使用c更新一个for/loop下的所有单个集合值,然后从该c做您想做的任何事情。 I use Counter here as what you want to count if anyone has only got 1 language. 如果有人只有一种语言,我在这里使用Counter作为您要计数的内容。

from collections import Counter
c = Counter()

for v in languages.values():
    c.update(v)

for k,v in c.iteritems():
    if v == 1:
        print k, " is monolingual"

Ben  is monolingual
James  is monolingual
Tara  is monolingual

The c looks like this: c看起来像这样:

c
Counter({'Tim': 3, 'Nicky': 2, 'John': 2, 'Ben': 1, 'James': 1, 'Tara': 1})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM