By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,095 Members | 1,658 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,095 IT Pros & Developers. It's quick & easy.

How to work with sets and dicts in Python?

P: 4
Hello Guys!
I need to create a program similar to this explanation
Expand|Select|Wrap|Line Numbers
  1. for each word in the input text:
  2.        set a variable equal to the length of the set
  3.        add word to the set
  4.        if the length of the set is greater than the length of the "old" set:
  5.            add the word to the dict 
  6.    for k, v in words.items():
  7.        print(k, v)
I am new to Python so for me it's very difficult to solve that. Can anyone help me?

Thank you in advance
Nov 17 '10 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 2.5K+
P: 2,851
Why not just add the words to the dictionary and skip the intermediate step? Are the words the dictionary keys? What are the values supposed to be?
Nov 17 '10 #2

P: 4
Hi bvdet

The goal here is to accept input from the user and create a list. Then you will add the elements of the list to a set. If the set size increases, add the word to a dict with the word being a key and the value being the length of the set, which is also the number of
pairs in the dict at the time it was added. In psuedocode, you will need something like:

for each word in the input text:
set a variable equal to the length of the set
add word to the set
if the length of the set is greater than the length of the "old" set:
add the word to the dict
for k, v in words.items():
print(k, v)
Nov 18 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.