Mathmos
Mathmos

Reputation: 151

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. 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.

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

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!')

Upvotes: 1

Views: 134

Answers (2)

Anzel
Anzel

Reputation: 20553

languages[toungue] is already a set, you don't need to set(languages[touge]). Also you don't need to loop the dictionary you can simply get those set from dictionary by 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.

A more detailed explanation is that, under a for/loop you are going to compare 1 set to another set. 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. I use Counter here as what you want to count if anyone has only got 1 language.

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
Counter({'Tim': 3, 'Nicky': 2, 'John': 2, 'Ben': 1, 'James': 1, 'Tara': 1})

Upvotes: 0

Oliver W.
Oliver W.

Reputation: 13459

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))

Upvotes: 1

Related Questions