python基础一 ------如何对元组各个元素进行命名

对元组各个元素进行命名

1,通过对元组索引值的命名

2,通过标准库中的collections.nametuple替代内置touple

通过对元组索引值的命名

好比在c中的defined详细见代码

1 name,gender,age = range(3)
2 student = ("ruioniao","man","19")
3 student["name"]
4 student["age"]
5 student["gender"]
6 #输出
7 #"ruoniao"
8 #19
9 #man

使用标准库中collections.nametuple代替内置的tuple

 

s这个变量名可以直接通过属性方式访问

 Student是namedtuple的名称,后面的列表是其元素创建时还可以

1 s= Student(name="ruoniao",age="19",sex="man")
2 #输出Student(name='ruoniao', age='19', sex='man')

可以通过‘点’像类访问属性那样进行访问

原文地址:https://www.cnblogs.com/ruoniao/p/6826828.html