How to work with sets and dicts in Python?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Azel Mr G
    New Member
    • Oct 2010
    • 4

    How to work with sets and dicts in Python?

    Hello Guys!
    I need to create a program similar to this explanation
    Code:
    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)
    I am new to Python so for me it's very difficult to solve that. Can anyone help me?

    Thank you in advance
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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?

    Comment

    • Azel Mr G
      New Member
      • Oct 2010
      • 4

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

      Comment

      Working...