Python 中的namedtuple

命名圖是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-hk/n/126320.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
CA3JT的頭像CA3JT
上一篇 2024-10-03 23:07
下一篇 2024-10-03 23:07

相關推薦

  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • Python編程二級證書考試相關現已可以上網購買

    計算機二級Python考試是一項重要的國家級認證考試,也是Python編程的入門考試。與其他考試一樣,Python編程二級證書的考生需要進入正式考試,而為了備考,這篇文章將詳細介紹…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29

發表回復

登錄後才能評論