淺析python中的二元嵌套列表分組
有時候,在處理數(shù)據(jù)庫時,我們需要執(zhí)行某些列表操作,這些操作更像是查詢語言,例如,將嵌套的列表元素相對于其他索引元素進(jìn)行分組。本文討論二元嵌套列表,并將每個嵌套列表元素相對于其其他索引元素進(jìn)行分組。
1. 列表解析
# initializing list test_list = [["G", 0], ["F", 0], ["B", 2], ["E", 2], ['I', 1], ['S', 1], ['S', 2], ['T', 2], ['G', 0]] # using list comprehension # to perform binary list grouping temp = set(map(lambda i: i[1], test_list)) res = [[j[0] for j in test_list if j[1] == i] for i in temp] # printing result print("The grouped list is : " + str(res))
輸出
The grouped list is : [['G', 'F', 'G'], ['I', 'S'], ['B', 'E', 'S', 'T']]
2. 使用itertools.groupby + itemgetter
我們也可以使用groupby函數(shù)來執(zhí)行這個特定的任務(wù)。該方法遵循2-3個步驟。首先,序列相對于第二個元素進(jìn)行排序,現(xiàn)在可以將其饋送以進(jìn)行分組。最后,我們根據(jù)結(jié)果的要求打印第一個元素。
from itertools import groupby from operator import itemgetter # Initializing list test_list = [["G", 0], ["F", 0], ["B", 2], ["E", 2], ['I', 1], ['S', 1], ['S', 2], ['T', 2], ['G', 0]] # Performing binary list grouping # using itertools.groupby() + itemgetter() test_list.sort(key=itemgetter(1)) groups = groupby(test_list, itemgetter(1)) res = [[i[0] for i in val] for (key, val) in groups] # Printing the resultant list print("The grouped list is : " + str(res))
輸出
The grouped list is : [['G', 'F', 'G'], ['I', 'S'], ['B', 'E', 'S', 'T']]
3. 使用collections.defaultdict()
import collections # initializing list test_list = [["G", 0], ["F", 0], ["B", 2], ["E", 2], ['I', 1], ['S', 1], ['S', 2], ['T', 2], ['G', 0]] # using collections.defaultdict() # to perform binary list grouping res = collections.defaultdict(list) for val in test_list: res[val[1]].append(val[0]) # printing result print("The grouped list is : " + str(res.values()))
輸出
The grouped list is : dict_values([['G', 'F', 'G'], ['I', 'S'], ['B', 'E', 'S', 'T']])
4. 使用for循環(huán) + sort
啟動for循環(huán)以查找唯一的1索引元素
啟動了一個嵌套的for循環(huán),將所有具有相同1索引元素的字符分組
顯示分組列表
# initializing list test_list = [["G", 0], ["F", 0], ["B", 2], ["E", 2], ['I', 1], ['S', 1], ['S', 2], ['T', 2], ['G', 0]] # using list comprehension # to perform binary list grouping x = [] for i in test_list: if i[1] not in x: x.append(i[1]) x.sort() res = [] for i in x: a = [] for j in test_list: if j[1] == i: a.append(j[0]) res.append(a) # printing result print("The grouped list is : " + str(res))
輸出
The grouped list is : [['G', 'F', 'G'], ['I', 'S'], ['B', 'E', 'S', 'T']]
5. 使用numpy
導(dǎo)入別名np的numpy庫。
創(chuàng)建輸入列表test_list。
使用np.array函數(shù)將test_list轉(zhuǎn)換為numpy數(shù)組,并將結(jié)果賦給變量arr
使用np.unique函數(shù)獲取arr第二列的唯一值,并將結(jié)果賦給變量unique_values。
創(chuàng)建名為result_list的空列表。
對于unique_values中的每個唯一值i,執(zhí)行以下操作:
a.使用布爾索引來選擇arr中第二列等于i的行
b.從選定行的第一列提取值,并將其轉(zhuǎn)換為列表。
c.將結(jié)果列表附加到result_list。
打印生成的分組列表。
import numpy as np # initializing list test_list = [["G", 0], ["F", 0], ["B", 2], ["E", 2], ['I', 1], ['S', 1], ['S', 2], ['T', 2], ['G', 0]] arr = np.array(test_list) # Storing all unique values unique_values = np.unique(arr[:, 1]) result_list = [list(arr[arr[:, 1] == i, 0]) for i in unique_values] # Printing the result print("The grouped list is:", result_list)
輸出
The grouped list is: [['G', 'F', 'G'], ['I', 'S'], ['B', 'E', 'S', 'T']]
6. 使用dictionary和setdefault
此方法使用字典按條目的第二個元素對條目進(jìn)行分組,該元素充當(dāng)字典中的鍵。setdefault方法用于創(chuàng)建一個空列表作為新鍵的值,或者將當(dāng)前項(xiàng)的第一個元素附加到與該鍵關(guān)聯(lián)的列表中。然后將生成的字典值轉(zhuǎn)換為列表以獲得最終結(jié)果。
# initializing list test_list = [["G", 0], ["F", 0], ["B", 2], ["E", 2], ['I', 1], ['S', 1], ['S', 2], ['T', 2], ['G', 0]] # Using dictionary and setdefault() to perform binary list grouping group_dict = {} for item in test_list: group_dict.setdefault(item[1], []).append(item[0]) res = list(group_dict.values()) # printing result print ("The grouped list is : " + str(res))
輸出
The grouped list is : [['G', 'F', 'G'], ['B', 'E', 'S', 'T'], ['I', 'S']]
7. 使用pandas
它初始化一個名為test_list的列表,其中包含兩個元素的子列表。然后,它將列表轉(zhuǎn)換為pandas DataFrame,按每個子列表的第二個元素對DataFrame進(jìn)行分組,應(yīng)用lambda函數(shù)選擇每個子列表的第一個元素,將結(jié)果pandas Series轉(zhuǎn)換為列表,并打印分組后的列表。
import pandas as pd # initializing list test_list = [["G", 0], ["F", 0], ["B", 2], ["E", 2], ['I', 1], ['S', 1], ['S', 2], ['T', 2], ['G', 0]] # converting list to DataFrame df = pd.DataFrame(test_list, columns=['value', 'group']) # grouping by group grouped = df.groupby('group')['value'].apply(lambda x: x.tolist()).tolist() # printing result print("The grouped list is: " + str(grouped))
輸出
The grouped list is: [['G', 'F', 'G'], ['I', 'S'], ['B', 'E', 'S', 'T']]
8. 使用列表和索引
# Initializing list test_list = [["G", 0], ["F", 0], ["B", 2], ["E", 2], ['I', 1], ['S', 1], ['S', 2], ['T', 2], ['G', 0]] num_groups = max([elem[1] for elem in test_list]) + 1 grouped = [[] for _ in range(num_groups)] # Iterating over our input list for elem in test_list: grouped[elem[1]].append(elem[0]) grouped = [lst for lst in grouped if lst] # Printing the result print("The grouped list is:", grouped)
輸出
The grouped list is: [['G', 'F', 'G'], ['I', 'S'], ['B', 'E', 'S', 'T']]
到此這篇關(guān)于淺析python中的二元嵌套列表分組的文章就介紹到這了,更多相關(guān)python二元嵌套列表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
自學(xué)python求已知DNA模板的互補(bǔ)DNA序列
這篇文章主要為大家介紹了自學(xué)python求已知DNA模板的互補(bǔ)DNA序列的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Python實(shí)現(xiàn)簡易端口掃描器代碼實(shí)例
本篇文章主要介紹了Python實(shí)現(xiàn)簡易端口掃描器的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03用python一行代碼得到數(shù)組中某個元素的個數(shù)方法
今天小編就為大家分享一篇用python一行代碼得到數(shù)組中某個元素的個數(shù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01