Python 中的namedtuple

发布时间:2022-07-24

命名图是collections模块下的一个类。它包含映射到某些值的键,就像字典类型的对象一样。在这种情况下,用户可以在键和索引的帮助下访问这些元素。 要使用它,首先,用户必须导入 collections 标准库模块。 例如:

import collections

在本文中,我们将讨论 NamedTuple 类的一些函数。

名图的访问功能

通过使用 NamedTuple 类,用户可以在索引、键和 getattr()函数的帮助下访问这些值。NamedTuple 类的属性值是有序的,因此用户可以通过索引访问这些值。 NamedTuple 类将字段名称转换为属性,以便用户可以使用 getattr()函数从这些属性中获取数据。 示例:

import collections as col
# Now we will create employees NamedTuple
Employees = col.namedtuple('Employee', ['name', 'city', 'salary'])
# we will Add four employees
employee1 = Employees('Jack', 'Goa', '25000')
employee2 = Employees('Ross', 'Kolkata', '35000')
employee3 = Employees('Joey', 'Kerela', '55000')
employee4 = Employees('John', 'Jammu', '40000')
# we will Access the elements by using index
print('The name and salary of employee1: ' + employee1[0] + ' and ' + employee1[1])
print('The name and salary of employee2: ' + employee2[0] + ' and ' + employee2[1])
# we will Access the elements using attribute name
print('The name and city of employee3: ' + employee3.name + ' and ' + employee3.city)
print('The name and city of employee4: ' + employee4.name + ' and ' + employee4.city)
# we will Access the elements using getattr()
print('The City of employee1 and employee2: ' + getattr(employee1, 'city') + ' and ' + getattr(employee2, 'salary'))
print('The City of employee3 and employee4: ' + getattr(employee3, 'city') + ' and ' + getattr(employee4, 'salary'))

输出:

The name and salary of employee1: Jack and Goa
The name and salary of employee2: Ross and Kolkata
The name and city of employee3: Joey and Kerela
The name and city of employee4: John and Jammu
The City of employee1 and employee2: Goa and 35000
The City of employee3 and employee4: Kerela and 40000

命名双类的转换过程

很少有方法用于将其他集合转换为命名集合。用户可以使用 _make()函数来转换可迭代对象,如列表、元组等。转换为 NamedTuple 类对象。 用户还可以将字典类型对象转换为命名的类对象。要将字典类型转换为命名字典类型,用户必须使用**运算符。 命名元组可以返回带有键的值作为 OrderedDict 类型对象。要将其转换为 OrderedDict,用户必须使用 _asdict()函数。 示例:

import collections as col
# Now we will create employees NamedTuple
Employees = col.namedtuple('Employee', ['name', 'city', 'salary'])
# the List of values to Employees
users_list1 = ['Jack', 'Goa', '25000']
users_list2 = ['Ross', 'Kolkata', '35000']
employee1 = Employees._make(users_list1)
employee2 = Employees._make(users_list2)
print(employee1)
print(employee2)
#we will user Dict to convert Employee
users_dict1 = {'name':'Joey', 'city' : 'Kerala', 'salary' : '55000'}
employee3 = Employees(**users_dict1)
print(employee3)
users_dict2 = {'name':'John', 'city' : 'Jammu', 'salary' : '40000'}
employee4 = Employees(**users_dict2)
print(employee4)
#Show the named tuple as dictionary
employees_dict1 = employee1._asdict()
employees_dict2 = employee2._asdict()
employees_dict3 = employee3._asdict()
employees_dict4 = employee4._asdict()
print(employees_dict1)
print(employees_dict2)
print(employees_dict3)
print(employees_dict4)

输出:

Employee(name='Jack', city='Goa', salary='25000')
Employee(name='Ross', city='Kolkata', salary='35000')
Employee(name='Joey', city='Kerala', salary='55000')
Employee(name='John', city='Jammu', salary='40000')
OrderedDict([('name', 'Jack'), ('city', 'Goa'), ('salary', '25000')])
OrderedDict([('name', 'Ross'), ('city', 'Kolkata'), ('salary', '35000')])
OrderedDict([('name', 'Joey'), ('city', 'Kerala'), ('salary', '55000')])
OrderedDict([('name', 'John'), ('city', 'Jammu'), ('salary', '40000')])

命名元组的一些附加操作

还有一些其他功能,如_ field()和 _replace()功能。用户可以使用 _fields()函数来检查 NamedTuple 类的不同字段。 _replace() 功能用于替换属性值。 示例:

import collections as col
# Now we will create employees NamedTuple
Employees = col.namedtuple('Employee', ['name', 'city', 'salary'])
# we will Add four employees
employee1 = Employees('Jack', 'Goa', '25000')
employee2 = Employees('Ross', 'Kolkata', '35000')
print(employee1)
print(employee2)
print('The fields of Employee1: ' + str(employee1._fields))
print('The fields of Employee2: ' + str(employee2._fields))
#Now we will replace the city of employees
employee1 = employee1._replace(city='New Dehli')
print(employee1)
employee2 = employee2._replace(city='Assam')
print(employee2)

输出:

Employee(name='Jack', city='Goa', salary='25000')
Employee(name='Ross', city='Kolkata', salary='35000')
The fields of Employee1: ('name', 'city', 'salary')
The fields of Employee2: ('name', 'city', 'salary')
Employee(name='Jack', city='New Dehli', salary='25000')
Employee(name='Ross', city='Assam', salary='35000')

结论

在本文中,我们已经讨论了什么是命名图,以及用户如何访问它的不同功能和操作。