Python中名为tuple
的图是模块collections下的一个类。它包含映射到特定值的键,如字典类型对象。在这种情况下,用户可以借助按钮和索引来访问元素。
使用时,首先用户必须导入该集合的标准库模块。
示例:
import collections
在本文中,我们将讨论NamedTuple类的一些功能。
著名的图像访问功能
通过使用NamedTuple类,用户可以借助index、key和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')
结论
在本文中,我们讨论了图表是什么以及用户如何访问各种功能和操作。
版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
code前端网