1

I'm newish to Python and have been learning most of what i need to do for my research here from you guys on stackoverflow. I'm hoping someone can help me with a problem? I'm really sorry ahead of time for anything I'm naively butchering, and very thankful for your help.

Problem: So i have a 2000+ row, 6 column data set that i have sorted by the 2nd column (a bunch of angles from 0 to 360,) and i need to be able to input a degree value to separate my data and then back up that group of stuff (the appropriate rows + all the columns) into the appropriate array.

Note: I don't need them to be named "string#". I just need to have Python be able to make 100 of these if i need pie slices even smaller, and call them something like dataset1, dataset2, dataset2 etc.

a={}
dd = input("What is the angle in degrees? ") #i want a graph split into "pie slices"
numdd = int(360 / dd) #i want 360/dd number of premade and prenamed arrays
for x in range(1,listnum):
        a["string{0}".format(x)]="blah" #just to test that i can fill them with something
print a

if dd=45, it returns

a={'string6': 'blah', 'string7': 'blah', 'string4': 'blah', 'string5': 'blah', 'string2': 'blah', 'string3': 'blah', 'string1': 'blah'}

Question: Why does it start at 6??

if dd=30, it returns

a={'string10': 'blah', 'string11': 'blah', 'string8': 'blah', 'string9': 'blah', 'string6': 'blah', 'string7': 'blah', 'string4': 'blah', 'string5': 'blah', 'string2': 'blah', 'string3': 'blah', 'string1': 'blah'}

I looked through as many posts as i could and this seems to be the easiest way to do it but i'm pulling my hair out over here over something that's probably really trivial.

Thanks again!

EDIT: Follow up question so that i'm not clogging the comments section..

How can I now call upon dataset{k} if I input what I want "k" to be? For example, if I want to have a loop acting on k=1 and then move onto k=2 for dataset2? My original 3000x6 data set is called "posvc". This is what i have so far but i understand it needs work..

begin = 0
end = dd
k = 0
for k in range(numdd):
    j = 0
    for j in range(len(posvcs)):
        if (posvcs[j,2] >= begin) & (posvcs[j,2] < end):
            dataset{k}.append(posvcs[:,:])
            j += 1
        begin = begin + end
        end = begin + dd
    k += 1

EDIT 2: In response to Robin's edit, i changed my code as he recommended and i received the following error

File "C:/Python27/Research_RC/pieslices.py", line 203, in <module>
dataset.append(posvcs[:,:])

AttributeError: 'numpy.ndarray' object has no attribute 'append'

To clarify, yes I am just trying to take rows 'begin' to 'end' and all the data (several columns and rows) that corresponds to it, and place it into the pre-created array "dataset1." Then, to edit the 'begin' and 'end' values and do the same thing for the next array, ie. "dataset2" and so on. Thank you SO much for your help!

layces
  • 161
  • 2
  • 2
  • 12

1 Answers1

2

Dictionaries in Python are not ordered. You can use an OrderedDict, it's in the standard library:

from collections import OrderedDict
a = OrderedDict()
# the rest of your code remains the same

Edit for follow up question:

I think this is what you want:

begin = 0
end = dd
for dataset in a.values():
    for j in range(len(posvcs)):
        if (posvcs[j,2] >= begin) & (posvcs[j,2] < end):
            dataset.append(posvcs[:,:])
        begin = begin + end
        end = begin + dd

You don't need to do j = 0 or j += 1, and to me it just seems like you want to amend the values in a, is that right?

Jasmijn
  • 9,370
  • 2
  • 29
  • 43
  • Thank you, this is exactly what i needed. How can i now call upon dataset{i} if i input what i want "i" to be? For example, if i want to have a loop acting on i=1 and then move onto i=2 for dataset2? – layces Jan 07 '16 at 09:58
  • You can loop over the ordereddict like you would a dict. This comment section is not exactly suited for asking and answering questions, unfortunately. If you want more specific info, it would help if you would be very precise about how you want to use the data. – Jasmijn Jan 07 '16 at 10:03