473,507 Members | 9,962 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to work with sets and dicts in Python?

4 New Member
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
2 1910
bvdet
2,851 Recognized Expert Moderator Specialist
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
Azel Mr G
4 New Member
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

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

Similar topics

0
1403
by: jose flanigan | last post by:
I don't have root access, but managed to install Tcl/Tk in my local directory. I've also managed to install python 2.2 in my local directory. The problem is that for some reason Tkinter isn't...
7
2199
by: Steve | last post by:
This post has two parts. First is my feedback on sets. (Hello? Last summer called, they want their discussion thread back...) Second is some questions about my implementation of a partition...
11
2050
by: sandravandale | last post by:
I can think of several messy ways of making a dict that sets a flag if it's been altered, but I have a hunch that experienced python programmers would probably have an easier (well maybe more...
14
3437
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
852
27941
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
7
3504
Motoma
by: Motoma | last post by:
When I use the python interpreter on my remote machine, the arrow keys do not function properly. Instead of repositioning the cursor and recalling previous commands, I get ^[[A ^[[B ^[[C ^[[D written...
22
2063
by: sapsi | last post by:
Hello, I recently tried using the set function in Python and was surprised to find that a= ] doesn't work with 'set', throwing TyperError (unhashable exception). I found out that this is...
4
2150
by: sigkill9 | last post by:
I'm trying to create a script that will accept a user entered integer and find only one set of prime numbers that add up to that integer but need some help. The script is supposed to only accept...
0
7221
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7109
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1537
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.