-2

What is the PEP8 standard for systematic multi-dimensional (nested) arrays?

There many ways to structure data in Python, some examples of which are given below. And clearly the 'best' method is the one that meets your needs, even though we have all seen our share of 'sloppy' data structures. But is there a systematic way of actually structuring data that is established as a standard?

For example, a dictionary filled by an ambiguous [list]/(tuple):

# print John's grade
students = {'John':['1','7'],'Kate':['PreK','4']}
print(students['John'][0])

Or a slightly less ambiguous dictionary filled by a dictionary:

# print Kate's grade
students = {'John':{'grade':'1','age':'7'},'Kate':{'grade':'PreK','age':'4'}}
print(students['Kate']['grade'])

Another possible method is to create your own data type and structure using a class:

class timeZone:
    def __init__(self, zone_code, city, offset):
        self.zone_code = zone_code
        self.city = city
        self.offset = offset
        self.DST_offset = DST_offset

Z = timeZone('Z','Zulu Time',int(0))
Y = timeZone('Y','Fiji',int(-12))
X = timeZone('X','American Samoa',int(-11))
W = timeZone('W','Honolulu, HI',int(-10))
V = timeZone('V','Juneau, AK',int(-9))

Or is there another method/library that is best?

Justin
  • 43
  • 5
  • 1
    What exactly are you asking? What is an ambiguous tuple? (BTW, there are no tuples in that example - the values are lists). From the Zen of Python (`import this`), explicit is better than implicit, but really you should use the data structure that works for your needs. – MattDMo May 17 '15 at 02:50
  • 1
    Please visit [help] and search for "opinion". – GreenAsJade May 17 '15 at 02:57
  • 1
    @GreenAsJade, the question could also be asked as "What does PEP8 standard recommend for styling multi-dimensional/nested arrays?" – Justin May 17 '15 at 03:22
  • 1
    @MattDMo, by saying it is ambiguous I mean that a list/tuple holds multiple values without context. A list/tuple is just a bunch of "stuff" to the program, whereas a dictionary has explicit key/value relationships for the "stuff". – Justin May 17 '15 at 03:26
  • 2
    What GreenAsJade meant was that this question is asking for opinions, which is off-topic on Stack Overflow, and all Stack Exchange sites. There is no single answer to this question - do what suits you and the needs of the code best. In some cases, explicit (nested dicts) may be better, in other cases a simple list will do the trick. If something is ambiguous, then you're doing it wrong. A list with a clearly defined order of members is not ambiguous, a list where a reasonable person looking at the code would have no idea what the order meant *is* ambiguous. It all depends on the context. – MattDMo May 17 '15 at 03:35
  • @Justin I arrived at your question in the "close vote" review queue. That means that someone(s) have voted to close it based on the fact that is is clearly opinion based, which is explicitly out of scope for SO. If you reword it in the way you suggested, it might have a better chance of not being closed and deleted, and a better chance of attracting good answers. As it stands, reviewers have no choice but to vote to close it. – GreenAsJade May 17 '15 at 04:00

2 Answers2

1

Generally speaking, Python recommends clarity and explicit coding. In your first example, the meaning of the values in the list is indeed not explicit, so I do not recommend it in general.

Now, you might sometimes want to write such code, because of some other, more important reasons. I would say: always be as explicit as you can be, so that the meaning of the data is easily seen.

Now, indicating the meaning of your data explicitly can be done in many ways:

  • Through an efficient collections.namedtuple that contains the class and age (this is more efficient that storing the same thing in a dictionary, and it can be more efficient, thanks to attribute access like .age).
  • Through a pandas dataframe that associates each student with class and age columns.
  • Through a NumPy record arrays: you store the student's name, class and age as a record in a 1-dimensional array (but given a name, you have to go through the whole array to find it, so this method might not suit your particular application).
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
-1

This is not about pythonic or best-practice, but what you really need.

If you need more explicitness, second solution is better. However if you somehow need to pass all values as a whole to a function like

def foo(grade, age, sex, height, ...):

You may prefer the first one, cause you can do foo(*students['John']) which saves your time.

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
laike9m
  • 18,344
  • 20
  • 107
  • 140
  • I don't see any reason why my answer gets downvote. Care to explain? – laike9m May 17 '15 at 04:40
  • 1
    I don't know why this was downvoted, but I want to make two remarks: (1) function `foo()` would probably typically take a `student` and then use its field, instead of taking a host of variables. In fact, this is more robust against an extension of the student data. (2) Even with the function in your example, using named tuples instead of plain tuples would be more explicit (when printing the value of a student, for example). – Eric O. Lebigot May 17 '15 at 09:32
  • Sure I always love namedtuple, I was just trying to tell OP the answer may differ in different situations. – laike9m May 17 '15 at 09:44