如何解壓 Python 元組列表
Python 是一種在全球範圍內用於各種目的的程式語言,例如 Web 開發、資料科學、機器學習以及執行許多不同的自動化流程。元組是 Python 的一個非常有用的特性,它有助於將來自多個數據集(如字典、列表等)的資料儲存在一個層級上。在本文中,我們將學習有關可用於解壓 Python 元組列表的不同方法。
解壓 Python 元組列表的不同方法
列表推導式
列表推導式用於逐個檢查列表中存在的每個元素。在這種方法中,我們將藉助列表推導式來解壓元組列表。讓我們舉個例子來更好地理解它。
示例
Places = [('Ahmedabad', 'Gujarat'), ('Hyderabad', 'Telangana'), ('Silchar', 'Assam'), ('Agartala', 'Tripura'), ('Namchi', 'Sikkim')] # The input of tuples is given
City, States = zip(*Places) # The zip function of list comprehension separate the data into different cities and states
print(City)
print(States)
輸出
以上示例的輸出如下所示
('Ahmedabad', 'Hyderabad', 'Silchar', 'Agartala', 'Namchi')
('Gujarat', 'Telangana', 'Assam', 'Tripura', 'Sikkim')
Itertools
Itertools 主要用於在迴圈中迭代資料中的元素。在存在大量包含大量資料的元組的情況下,此方法更可取。讓我們舉個例子來更好地理解它。
示例
from itertools import zip_longest # Do not forget to import itertools or else error might occur
Places = [('Ahmedabad', 'Gujarat'), ('Hyderabad', 'Telangana'), ('Silchar', 'Assam'), ('Agartala', 'Tripura'), ('Namchi', 'Sikkim')] # The input of tuples is given
City, State = zip_longest(*Places) # We use zip_longest so that the tuples which are not same in length can also be unzipped
City = [City for City in City if City is not None] # The different lists formed might consist of `none` value for missing element
State = [State for State in State if State is not None] # The list comprehension to maintain the same length and remove the none values
print(City)
print(State)
輸出
以上示例的輸出如下所示
('Ahmedabad', 'Hyderabad', 'Silchar', 'Agartala', 'Namchi')
('Gujarat', 'Telangana', 'Assam', 'Tripura', 'Sikkim')
迴圈方法
在 Python 中沒有引入不同的特性時,使用過此方法。它是解壓元組列表的最簡單和最基本的方法之一。讓我們舉個例子來更好地理解它。
示例
Places = [('Ahmedabad', 'Gujarat'), ('Hyderabad', 'Telangana'), ('Silchar', 'Assam'), ('Agartala', 'Tripura'), ('Namchi', 'Sikkim')] # The input of tuples is given
Cities = []
States = []
for City, State in Places: # We check each value in the list and then extract the different city and state element
Cities.append(City)
States.append(State)
print(Cities)
print(States)
輸出
以上示例的輸出如下所示
('Ahmedabad', 'Hyderabad', 'Silchar', 'Agartala', 'Namchi')
('Gujarat', 'Telangana', 'Assam', 'Tripura', 'Sikkim')
Numpy
當元組列表包含不同的數值資料時,此特性主要很有用。在這種情況下,我們可以輕鬆地使用 numpy 的函式。讓我們舉個例子來更好地理解它。
示例
import numpy as np # DO not forget to import numpy or else error might occur
Places = [('Gujarat', 31), ('Punjab', 237), ('Assam', 33), ('Tripura', 14), ('Sikkim', 6)] # The input of tuples is given
States, number_of_cities = np.array(Places).T.tolist() # We will use array operation to unzip the data
#np.array will help us convert the tuple into array and .T will help us to unzip and separate the data and tolist will convert into list
print(States)
print(number_of_cities)
輸出
以上示例的輸出如下所示
['Gujarat', 'Punjab', 'Assam', 'Tripura', 'Sikkim'] ['31', '237', '33', '14', '6']
Pandas 資料框
這是一種非常高階的解壓資料方法,僅在需要對大量資料進行高精度處理的情況下使用。讓我們舉個例子來更好地理解它。
示例
import pandas as pd # Do not forget to import pandas data frame or else error might occur
Places = [('Ahmedabad', 'Gujarat'), ('Hyderabad', 'Telangana'), ('Silchar', 'Assam'), ('Agartala', 'Tripura'), ('Namchi', 'Sikkim')] # The input of tuples is given
df = pd.DataFrame(Places, columns=['City', 'State']) # The list is first converted into pandas data frame with the help of pd.dataframe which help us to have access to elements separately
Cities = df['City'].tolist() #tolist will help to convert these dataframes back into standard lists
States = df['State'].tolist()
print(Cities)
print(States)
輸出
以上示例的輸出如下所示
('Ahmedabad', 'Hyderabad', 'Silchar', 'Agartala', 'Namchi')
('Gujarat', 'Telangana', 'Assam', 'Tripura', 'Sikkim')
運算子模組
我們將使用運算子模組的函式來解壓元組列表。此方法也是一種複雜的方法,在極少數情況下使用。讓我們舉個例子來更好地理解它。
示例
from operator import itemgetter # Do not forget to import operator module or else error might occur
Places = [('Ahmedabad', 21), ('Hyderabad', 32), ('Silchar', 43), ('Agartala', 24), ('Namchi', 21)] # The input of tuples is given
Cities, people = map(list, zip(*map(itemgetter(0), Places))), map(itemgetter(1), Places)
people = list(people)# itemgetter 0 & 1 will used to get the city and state element respectively and the map function helps them to find the elements in the places list and with the help of zip we will unzip the list
print(people)
輸出
以上示例的輸出如下所示
[21, 32, 43, 24, 21]
結論
要成為一名高效的程式設計師,必須瞭解可用於解壓元組列表的不同方法。可以根據便利性和應用領域使用不同的方法。本文中提到了所有可使用的方法。
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP