命名圖是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')
在本文中,我們已經討論了什麼是命名圖,以及用戶如何訪問它的不同功能和操作。
原創文章,作者:CA3JT,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/126320.html