3 Replies - 397 Views - Last Post: 15 November 2012 - 08:50 PM Rate Topic: -----

#1 medaa  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

occurrence question

Posted 15 November 2012 - 04:34 PM

numb=input("Enter numbers separated by spaces")
dict={}
for x in numb:
    (number,number)=numb.rsplit(" ",1)
    occ=(numb.count(x))
    dict[number]=occ
    print(dict)


Output:
Enter numbers separated by spaces4 5 6 7 8 4
{'4': 2}
{'4': 5}
{'4': 1}
{'4': 5}
{'4': 1}
{'4': 5}
{'4': 1}
{'4': 5}
{'4': 1}
{'4': 5}
{'4': 2}

This program is suppose to take the numbers in numb and count how many times they occurs. I dont know whats happening, Im having a really hard time with this, can someone please help me out.

Is This A Good Question/Topic? 0
  • +

Replies To: occurrence question

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,198
  • Joined: 19-March 11

Re: occurrence question

Posted 15 November 2012 - 04:53 PM

You're using that rsplit, but here you just want a regular split.

Before, you were taking in a (city, temp) combination. Since city names, fully spelled out, may contain commas (ie, "Sao Paolo, Brazil") we used the rsplit, limited to 1, to just peel off the last comma-separated item, assuming the rest is part of the city name.

Here, you're doing something different: you want the whole list, separated into its components. So just use split(" ") to split at each and every space.
Like so:


>>> "4 6 5 7 8 4".split(" ")
['4', '6', '5', '7', '8', '4']
Was This Post Helpful? 0
  • +
  • -

#3 medaa  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

Re: occurrence question

Posted 15 November 2012 - 05:57 PM

Oh I never knew that, thanks.
My problem is that I still don't understand how to count how many times a digit occurs in a number or word in a paragraph, etc. Can you help me with that or show a place that explains it.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,198
  • Joined: 19-March 11

Re: occurrence question

Posted 15 November 2012 - 08:50 PM

Break the problem down into steps. You have a String which consists of numbers separated by spaces. Now you know how to turn that into a List. You know how to iterate a list, right?

for i in lst:
  # do something with each i




What do you want to do with each i? Well, you've seen dicts, right? So one way would be to see if you have a dict entry for i. If not, make an entry for i and set it to 1. If not, increment the entry.
That's one way to do it.

Another way to do it would be to figure out what the largest entry you need to deal with is going to be - there's a max function for that. Now suppose we have some number m as the max item in your input list. Make a new list, occurrences, which is long enough to hold m as an index, so m+1 items long, where each value in the list is zero. There are a few ways to do this. You'd probably find a for loop to be the most intuitive at this stage, but there are other interesting constructions for this.
Now you have the same case as you have with the dictionary: for each i in your input list, just increment occurrences[i]

Or, if you wanted to be clever, you could make a set from your input list: now you just have the unique values from that list. There are a few ways you could turn that into a dictionary, including making a dict() and then for each item in your unique list, adding a new entry, with value set to zero.

unique_list = list(set(input_list))
occurrences = dict()
for i in unique_list:
  occurrences[i]=0
# now you can count occurrences from your input_list



These are just a few things that you could try. The thing is, you will not learn much just by copying some code that I give you. The point of trying to do these things is to discover how to make these things work, so you know how to discover.

Read the documentation for lists. There are some useful methods there. Try them out - you want to have each of them in your toolkit, and doing problems like this will make you understand how they work.

If a method doesn't make sense to you, try it out in the interpreter. Make a list, and see what happens when you apply the method. The only way to really understand this stuff is to get in there and experiment.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1