6 Replies - 281 Views - Last Post: 05 October 2012 - 05:19 PM Rate Topic: -----

#1 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 280
  • Joined: 22-July 09

How to approach this

Posted 05 October 2012 - 01:58 PM

i do not want code given to me but a point int he right direction as i am unsure where to start.
say i have a list of names, i would like to randomly choose from them 100 times and then break it down by percentages. i know how i would randomly choose from the array but how would i store the results if i do not know how many names will be present or in what order they might be in?
I should mention I am using Python 3.3.

This post has been edited by blank_program: 05 October 2012 - 02:04 PM

Is This A Good Question/Topic? 0
  • +

Replies To: How to approach this

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

Re: How to approach this

Posted 05 October 2012 - 02:22 PM

Quote

and then break it down by percentages.


Can you clarify? Break what down by percentage?

Quote

i do not know how many names


Use a dynamic structure (E.g. list, set, map).

Quote

in what order they might be in?


Must they be in some particular order? Would sorting be an option?
Was This Post Helpful? 0
  • +
  • -

#3 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 280
  • Joined: 22-July 09

Re: How to approach this

Posted 05 October 2012 - 02:37 PM

Say I have alpha, beta, charlie, delta, and echo. I add them all to one list. Now I use the random class to randomly choose from that list. I want to iterate 100 times and then determine which was selected most, second, third, etc. So it would be sorted by the number of times each was called. Should I just use random to choose then make a second list of 100 items and then use a counter or somethign with that?
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

Re: How to approach this

Posted 05 October 2012 - 02:52 PM

The most straightforward (list) way would be: add the 100 choices to a second list, and then for each unique element in the first list, go through the second and count how many times each occurred. Then you could insert that into a list based on the count.

I better way would be to create a map associating a word from the set (alpha, beta, etc.) to a count. For every random selection, increment that count. This is surely to have the least amount of code.

This post has been edited by blackcompe: 05 October 2012 - 02:52 PM

Was This Post Helpful? 0
  • +
  • -

#5 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 280
  • Joined: 22-July 09

Re: How to approach this

Posted 05 October 2012 - 03:08 PM

Do you have a link I could use to work with the maps you talk about?
Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

Re: How to approach this

Posted 05 October 2012 - 03:21 PM

Python Docs - dictionaries

You can google around for tutorials.
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: How to approach this

Posted 05 October 2012 - 05:19 PM

Incidentally, there's a data structure called a Counter that you might be really interested in. I briefly discussed it in my Python Gems post. It basically does everything blackcompe suggested.

This post has been edited by atraub: 05 October 2012 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1