0

In C language, for example,

typedef struct People_S
{
    int age;
    int grade;
}People;

People tmp[10] = {0};

Then I can use tmp[0].grade to access the data.

In Python, however, I have no ideal how to do it. If using list tmp = [[10,60],[11,50]], i can get the grade by tmp[0][1]. But too much magic number is not good for reading.

Thanks for your helf!

2
  • See data classes of python 3.7 Commented Oct 15, 2018 at 16:38
  • Python has classes. class Person:\n def __init__(self, age, grade):self.age,self.grade = age,grade Commented Oct 15, 2018 at 16:55

2 Answers 2

4

Another option besides classes or dictionaries is namedtuple:

>>> from collections import namedtuple
>>> People = namedtuple('People','age grade')
>>> People(10,60)
People(age=10, grade=60)
>>> p = People(10,60)
>>> p.age
10
>>> p.grade
60

A way to populate a People list:

>>> tmp = [[10,60],[11,50]]
>>> p = [People(age,grade) for age,grade in tmp]
>>> p
[People(age=10, grade=60), People(age=11, grade=50)]
Sign up to request clarification or add additional context in comments.

3 Comments

I like namedtuples: easy to set up, easy to use, very clear. And if your program grows in complexity you may be glad that converting a namedtuple to a class is also pretty easy.
didn't know about named tuples - nice feature.
Note: namedtuples are classes, they're just ones that Python generates for you (that subclass tuple to provide immutability guarantees and interoperability). Also note that as of 3.5, there is typing.NamedTuple, a replacement for collections.namedtuple which allows for safe typing, and added more flexibility as well in 3.6, which in turn paved the way for 3.7's dataclasses module, which provides maximally flexible class generation capabilities.
1

If you are fine with using iteration on a list, you could as well use dictionaries if you do not like full fledged classes:

dictList = []
for i in range(5):
    dictList.append({"age":i+10,"grade": i//3})

print(dictList)
print( dictList[2]["grade"], dictList[2]["age"] )

for e in dictList:
    print(e)

Output:

[{'grade': 0, 'age': 10}, {'grade': 0, 'age': 11}, {'grade': 0, 'age': 12}, 
 {'grade': 1, 'age': 13}, {'grade': 1, 'age': 14}]

(0, 12)

{'age': 10, 'grade': 0}
{'age': 11, 'grade': 0}
{'age': 12, 'grade': 0}
{'age': 13, 'grade': 1}
{'age': 14, 'grade': 1}

Or the previously mentioned classes (see PyTut: A first look at classes):

class People:
    def __init__(self,age,grade):
        self.age=age
        self.grade=grade

    def __str__(self):
        return f"Age {self.age} Grade {self.grade}"

classList = []
for i in range(5):
    classList.append( People(i+10,i//3))

print(*classList,sep="\n")

Output:

Age 10 Grade 0
Age 11 Grade 0
Age 12 Grade 0
Age 13 Grade 1
Age 14 Grade 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.