Python实战171202元组访问

学生信息系统中数据为固定格式:

(名字,年龄,性别,邮箱地址,......)

 学生数量很大为了减小存储开销,对每个学生信息用元组表示:

('jim',18,'male','jim8765@gmail.com')

访问时,我们使用引擎(index)访问,大量索引降低程序可读性。

1,枚举

  student=('jim',18,'male','jim8765@gmail.com')

   NAME=0

   AGE=1

   SEX=2

   EMAIL=3

 #或者  NAME,AGE,SEX,EMAIL=range(4)

   print[NAME]

2,函数

import collections

student=collections.namedtuple('student',['name','age','sex','email'])

s=student('jim',18,'male','jim8765@gmail.com')

s.name

print(s.age)

 #以类对象访问减小开销

 print(isinstance(s,tuple))  #为了保证tuple的属性,故变量起名不与全局变量相同  ture

---恢复内容结束---

原文地址:https://www.cnblogs.com/3daytears/p/8058739.html