首页 > 编程 > Python > 正文

Python 如何提高元组的可读性

2019-11-25 11:51:42
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Python 如何提高元组的可读性,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

假设学生系统中数据为固定格式:(名字,年龄,性别,邮箱)

('jack','16','male','jack3001@gmail.com')('eric','17','male','eric@qq.com')('xander','16','female','lucy123@yahoo.com')

方案一:

from enum import IntEnumNAME,AGE,SEX,EMAIL=range(4)s=('jim','16','male','jim8721@gmail.com')# 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','16','male','jim8721@gmail.com')s3=Student('eric','16','male','eric3421@gmail.com')print(s2) # Student(name='jim', age='16', sex='male', email='jim8721@gmail.com')print(s3) # Student(name='eric', age='16', sex='male', email='eric3421@gmail.com')

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表