Python中的Namedtuple

Python支持一种容器类型,比如 字典 “叫” namedtuple() “出现在模块中,” 收藏 “。与字典一样,它们包含散列到特定值的键。但相反,它支持从键值访问和迭代,这是字典所缺乏的功能。

null

例子:

Python3

# Python code to demonstrate namedtuple()
from collections import namedtuple
# Declaring namedtuple()
Student = namedtuple( 'Student' , [ 'name' , 'age' , 'DOB' ])
# Adding values
S = Student( 'Nandini' , '19' , '2541997' )
# Access using index
print ( "The Student age using index is : " , end = "")
print (S[ 1 ])
# Access using name
print ( "The Student name using keyname is : " , end = "")
print (S.name)


输出:

The Student age using index is : 19The Student name using keyname is : Nandini

让我们看看namedtuple()上的各种操作

访问操作

  • 按索引访问: namedtuple()的属性值是有序的,可以使用索引号来访问,这与索引无法访问的字典不同。
  • 按密钥名访问: 在字典中,也允许通过键名进行访问。
  • 使用getattr(): 这是访问该值的另一种方法,它将namedtuple和key value作为参数。

Python3

# Python code to demonstrate namedtuple() and
# Access by name, index and getattr()
# importing "collections" for namedtuple()
import collections
# Declaring namedtuple()
Student = collections.namedtuple( 'Student' , [ 'name' , 'age' , 'DOB' ])
# Adding values
S = Student( 'Nandini' , '19' , '2541997' )
# Access using index
print ( "The Student age using index is : " , end = "")
print (S[ 1 ])
# Access using name
print ( "The Student name using keyname is : " , end = "")
print (S.name)
# Access using getattr()
print ( "The Student DOB using getattr() is : " , end = "")
print ( getattr (S, 'DOB' ))


输出:

The Student age using index is : 19The Student name using keyname is : NandiniThe Student DOB using getattr() is : 2541997

转换操作

  • _make():- 此函数用于返回 iterable中的namedtuple() 作为论据通过。
  • _asdict():- 此函数返回 这个 OrderedDict() 根据namedtuple()的映射值构造。
  • 使用“**”(双星)运算符 :-此函数用于 将字典转换为namedtuple()。

Python3

# Python code to demonstrate namedtuple() and
# _make(), _asdict() and "**" operator
# importing "collections" for namedtuple()
import collections
# Declaring namedtuple()
Student = collections.namedtuple( 'Student' ,
[ 'name' , 'age' , 'DOB' ])
# Adding values
S = Student( 'Nandini' , '19' , '2541997' )
# initializing iterable
li = [ 'Manjeet' , '19' , '411997' ]
# initializing dict
di = { 'name' : "Nikhil" , 'age' : 19 , 'DOB' : '1391997' }
# using _make() to return namedtuple()
print ( "The namedtuple instance using iterable is  : " )
print (Student._make(li))
# using _asdict() to return an OrderedDict()
print ( "The OrderedDict instance using namedtuple is  : " )
print (S._asdict())
# using ** operator to return namedtuple from dictionary
print ( "The namedtuple instance from dict is  : " )
print (Student( * * di))


输出:

The namedtuple instance using iterable is  : Student(name='Manjeet', age='19', DOB='411997')The OrderedDict instance using namedtuple is  : OrderedDict([('name', 'Nandini'), ('age', '19'), ('DOB', '2541997')])The namedtuple instance from dict is  : Student(name='Nikhil', age=19, DOB='1391997')

附加操作

  • _字段: 此函数用于返回 所有的关键字 声明的命名空间的名称。
  • _替换(): _replace()与str.replace()类似,但以命名字段为目标(不修改原始值)

Python3

# Python code to demonstrate namedtuple() and
# _fields and _replace()
# importing "collections" for namedtuple()
import collections
# Declaring namedtuple()
Student = collections.namedtuple( 'Student' , [ 'name' , 'age' , 'DOB' ])
# Adding values
S = Student( 'Nandini' , '19' , '2541997' )
# using _fields to display all the keynames of namedtuple()
print ( "All the fields of students are : " )
print (S._fields)
# ._replace returns a new namedtuple, it does not modify the original
print ( "returns a new namedtuple : " )
print (S._replace(name = 'Manjeet' ))
# original namedtuple
print (S)


输出:

All the fields of students are : ('name', 'age', 'DOB')The modified namedtuple is : Student(name='Manjeet', age='19', DOB='2541997') 

本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享