6 Replies - 2183 Views - Last Post: 09 October 2011 - 12:53 PM Rate Topic: -----

#1 kwillheat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-October 11

user defined/dynamic multidimensional array/list

Posted 08 October 2011 - 08:47 AM

Hello everyone,
this is my first post and i will try to do my best with showing what my issue is. i will be honest this is an assignment for one of my classes focusing on probability. i have to create a user defined multidimensional/dynamic something to hold data and it's relationships between groups. i just need some help knowing the best way to approach this. i have tried multidimensional lists and for loops, but the amount of for loops you need really takes knowing how many dimensions that you will need beforehand, and with that you either write code for every situation, or do something else. we are supposed to be able to handle up to 4 or 5 relationships/dimensions, for example how the economy does vs stock prices, or how many students in a cs or math major are what gender, age, ethnicity, etc... i would post the code i have now, but it is a mess and i have had to rewrite it three times and i have gotten to the point that i really don't know what is going on, much less showing someone else. i just need a better way to approach this problem. i have thought about using a dictionary and using each relation ship as a key (dictionary['cs','cs2455','male','18-25'] = 10) but i still run into issues with how i loop through each relationship to be able to have user input all of the values for those relationships.

Is This A Good Question/Topic? 0
  • +

Replies To: user defined/dynamic multidimensional array/list

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4364
  • View blog
  • Posts: 12,182
  • Joined: 18-April 07

Re: user defined/dynamic multidimensional array/list

Posted 08 October 2011 - 09:58 AM

I assume you got a hand out or a web site stating what they are asking for. Can you show us the exact text they have given you? I think the main problem here is that you are not sure what exactly is being asked and if we can't get an idea of the problem, there is no way we can help with a solution. Show the assignment problem, word for word here.

And just so you know, you don't need to know the dimensions to traverse a list of lists. You can do a for loop like... for element in list: statement. So to loop through several you take this a step further...

for item in list:
    for subitem in item:
         for subsubitem in subitem:
             # do stuff here



But if you can show the problem we can certainly help figure out what exactly you need. :)
Was This Post Helpful? 0
  • +
  • -

#3 kwillheat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-October 11

Re: user defined/dynamic multidimensional array/list

Posted 08 October 2011 - 10:19 AM

View PostMartyr2, on 08 October 2011 - 10:58 AM, said:

I assume you got a hand out or a web site stating what they are asking for. Can you show us the exact text they have given you? I think the main problem here is that you are not sure what exactly is being asked and if we can't get an idea of the problem, there is no way we can help with a solution. Show the assignment problem, word for word here.

And just so you know, you don't need to know the dimensions to traverse a list of lists. You can do a for loop like... for element in list: statement. So to loop through several you take this a step further...

for item in list:
    for subitem in item:
         for subsubitem in subitem:
             # do stuff here



But if you can show the problem we can certainly help figure out what exactly you need. :)

Thank you for your response. Unfortunately my professor gives all assignments verbally and the information i gave here is pretty much what he said. from what i can remember "Write a program that will take in the user defined amount of dimensions and use baye's theorem to show all of the relationships between the dimensions." from the discussion in class that we had he wanted it like this video but let the user define the relationships, instead of Countries and gender, one could show relationships between economy and stock growth, which i believe is also in that video. then we have to use baye's theorem to calculate the all of the probabilities that are in all of the relationships (from video, if you have a boy, what is the chance the are french, or given someone is a Canadian, what is the chance that they are a girl.)
this very moment i am working on getting input of the names of the relationships (France, UK, Canada, Male, Female) then storing those in a 2d list. then using a dictionary to store actual values. does that make sense?
also thank you for letting me know about the for element in list thing. i am new to python and have only coded in c++ before. to my knowledge nothing like that exists in c++, and i still think in c++ at times.
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4364
  • View blog
  • Posts: 12,182
  • Joined: 18-April 07

Re: user defined/dynamic multidimensional array/list

Posted 08 October 2011 - 11:08 AM

Well lets use the video as an example. We have three countries. Each country has two groups of people, male and female. We could represent this as a list of lists or a dictionary of dictionaries.

problem_set = {'France': {'Female': 10, 'Male': 10}, 'Uk' : {'Female': 20, 'Male': 10}, 'Canada': {'Female': 30, 'Male': 10}}




What this is going to afford us being able to get at each item using problem_set['France']['Male'] which will return 10 but also allow you to loop through countries, loop through genders for a given country etc. Because you will need to be able to loop through the entire thing to get the entire population, loop through a country to get probabilities within that country etc.

How would we get the probability of a male from France? Nested loop to loop through the countries and then genders, add them up and divide them by problem_set['France']['Male'] value or 10 out of 90 aka 1/9.

:)
Was This Post Helpful? 0
  • +
  • -

#5 kwillheat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-October 11

Re: user defined/dynamic multidimensional array/list

Posted 08 October 2011 - 11:32 AM

when you say it it makes it extremely easy... how could i set that up to have the user enter in
first group, 3 sections, france, uk, and canada
second group, 2 sections male, female
then correlate it to make the problem_set?
and also make this able to do even more relationships? the way you have it there is great. i guess i just am at my wits end with the whole thing. i tried to organize the dimensions using a 2 d list of strings (which really turned out to be like a 4d list because using the strings made the amount of [] i could use to access individual letters which was confusing) but i couldn't wrap my brain around how to do it. i'm sorry, i have never had this much trouble approaching a problem. usually for the most part i can see each step how to do things, but this is winning right now.
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4364
  • View blog
  • Posts: 12,182
  • Joined: 18-April 07

Re: user defined/dynamic multidimensional array/list

Posted 08 October 2011 - 04:18 PM

Just collect the name and add it straight into the dictionary. Take another country? Add it to the dictionary...

problem_set['DIC'] = {'Female': 1, 'Male': 1,220,000}



Want to add another category to a given country?

problem_set['DIC']['Other'] = 1;



Collect the name from the user, add it in the appropriate dictionary. You can add to a dictionary that already exists, just specify the key and its value.

This is really all I can do to give you the answer without having done the whole assignment for you. Once you get this structure in place, all that is really left is the mechanism for building the dictionary of dictionaries and then looping through it to find the appropriate sums and probabilities.

:)
Was This Post Helpful? 0
  • +
  • -

#7 kwillheat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-October 11

Re: user defined/dynamic multidimensional array/list

Posted 09 October 2011 - 12:53 PM

Thank you so much Martyr2. That has been a great help. I didn't realize the you could work dictionaries like that. I will give it all a try. I really appreciate your help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1