[英]How to group a list of tuples based on the strings they contain?
我有一個兩串元組的排序列表。 我還列出了其中一些元組中的4個字符串。 我想從這4個字符串之一到列表中的下一個分組。 有點難以解釋,所以我將演示。
original_list = [('1321', 01), ('MessageXZY', 02), ('DescriptionSKS', 03), ('S7_6', 04), ('S7_3', 05), ('0A3B', 06), ('MessageZYA', 07),
('DescriptionKAM', 08), ('9K44', 09), ('MessageYAL', 10),
('DescriptionAUS', 11), ('S7_2', 12)]
我將術語1321 , OA3B和9K44保存在另一個列表中。 我想將這些術語之間(包括其中)的所有內容歸為一個元組,如下所示:
grouped_list = [(('1321', 01), ('MessageXZY', 02), ('DescriptionSKS', 03), ('S7_6', 04), ('S7_3', 05)), (('0A3B', 06), ('MessageZYA', 07),
('DescriptionKAM', 08)), (('9K44', 09), ('MessageYAL', 10),
('DescriptionAUS', 11), ('S7_2', 12))]
如果包含我的4個字符術語的列表稱為代碼 ,而包含元組的列表稱為original_list ,我需要什么代碼來實現呢?
編輯:這是我起床的地方:
grouped_list = []
for tuple in original_list:
for string in tuple:
if string in code:
grouped_list = list(zip ##zip that tuple and all consecutive tuples until the next item in code
來自Ruby背景,我經常感到有必要在Python中使用類似Enumerable#slice_before
東西。 基本上,它將所有可迭代的對象拆分為多個塊。 切片在謂詞為真的每個元素之前完成。
基於Rubinius實現 ,我將代碼移植到Python。
def slice_before(iterable, predicate):
chunk = None
for elem in iter(iterable):
if predicate(elem):
if chunk:
yield chunk
chunk = [elem]
else:
if not chunk:
chunk = []
chunk.append(elem)
if chunk:
yield chunk
這里有一些例子:
>>> list(slice_before(range(12), lambda i: i % 3 == 0))
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]
>>> list(slice_before('LetsTestSliceBefore', str.isupper))
[['L', 'e', 't', 's'], ['T', 'e', 's', 't'], ['S', 'l', 'i', 'c', 'e'], ['B', 'e', 'f', 'o', 'r', 'e']]
您只需要初始化數據即可。 請注意, code_list
可以設置為快速查找:
original_list = [('1321', '01'), ('MessageXZY', '02'), ('DescriptionSKS', '03'), ('S7_6', '04'), ('S7_3', '05'), ('0A3B', '06'), ('MessageZYA', '07'), ('DescriptionKAM', '08'), ('9K44', '09'), ('MessageYAL', '10'),
('DescriptionAUS', '11'), ('S7_2', '12')]
code_list = {'1321', '0A3B','9K44'}
您的問題所需的代碼成為slice_before
:
print(list(slice_before(original_list, lambda x_y: x_y[0] in code_list)))
# [[('1321', '01'), ('MessageXZY', '02'), ('DescriptionSKS', '03'), ('S7_6', '04'), ('S7_3', '05')], [('0A3B', '06'), ('MessageZYA', '07'), ('DescriptionKAM', '08')], [('9K44', '09'), ('MessageYAL', '10'), ('DescriptionAUS', '11'), ('S7_2', '12')]]
我假設您有要拆分的代碼列表。 據此,看看此代碼是否對您有用。
original_list = [('1321', '01'), ('MessageXZY', '02'), ('DescriptionSKS', '03'), ('S7_6', '04'), ('S7_3', '05'), ('0A3B', '06'), ('MessageZYA', '07'), ('DescriptionKAM', '08'), ('9K44', '09'), ('MessageYAL', '10'),
('DescriptionAUS', '11'), ('S7_2', '12')]
code_list = ['1321', '0A3B','9K44']
grouped_tuples = []
for entry in original_list:
if entry[0] in code_list:
new_tuple = []
new_tuple.append(entry)
for i in range(original_list.index(entry)+1, len(original_list)):
if(original_list[i][0] not in code_list):
new_tuple.append(original_list[i])
else:
break
grouped_tuples.append(tuple(new_tuple))
print grouped_tuples
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.