Python 提高元组的可读性

Python 提高元组的可读性,第1张

概述假设学生系统中数据为固定格式:(名字,年龄,性别,邮箱) (‘jack‘,‘16‘,‘male‘,‘[email protected]‘)(‘eric‘,‘17‘,‘male‘,‘[email protected]‘)(‘xander‘,‘16‘,‘female‘,‘[email protected]‘) 方案一: from enum import IntEnumNAME,AGE,SEX,E 假设学生系统中数据为固定格式:(名字,年龄,性别,邮箱)

(‘jack‘,‘16‘,‘male‘,‘[email protected]‘)(‘eric‘,‘17‘,‘[email protected]‘)(‘xander‘,‘female‘,‘[email protected]‘)

方案一:

from enum import IntEnumname,AGE,SEX,EMAIL=range(4)s=(‘jim‘,‘[email protected]‘)# print(name)  # 0class StudentEnum(IntEnum):    name=0    AGE=1    SEX=2    EMAIL=3print(s[StudentEnum.name]) # jimprint(isinstance(StudentEnum.name,int)) # True

方案二:

from collections import namedtupleStudent=namedtuple(‘Student‘,[‘name‘,‘age‘,‘sex‘,‘email‘])s2=Student(‘jim‘,‘[email protected]‘)s3=Student(‘eric‘,‘[email protected]‘)print(s2) # Student(name=‘jim‘,age=‘16‘,sex=‘male‘,email=‘[email protected]‘)print(s3) # Student(name=‘eric‘,email=‘[email protected]‘)
总结

以上是内存溢出为你收集整理的Python 提高元组的可读性全部内容,希望文章能够帮你解决Python 提高元组的可读性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1191756.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存