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']
Panda数据框
这是一种非常先进的方法,用于解压数据,仅在需要高精度处理大量数据的情况下使用。让我们通过一个例子更好地理解它:
示例
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]
结论
要成为高效的程序员,必须了解可以用于解压列表元组的不同方法。可以根据方便和应用领域使用不同的方法。以上文章中提到了可以使用的所有不同方法。