2017-10-12 69 views
2

我有一个双字符串元组的排序列表。我还有一些这些元组中的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)] 

我有条款,OA3B9K44保存在另一个列表。我想之间(含)集团一切这些字词的元组,像这样:

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 
+0

这不是很清楚你问什么。也许一个简单的例子和​​一些显示你的努力的代码会很好。 – Unni

+0

它不清楚,包括你的第二个清单,并明确提出预期的输入和输出。 –

回答

2

我假设你有你想拆就代码的列表。据此,看看这段代码是否适合你。

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 
+0

似乎已经从原始列表中删除了4个字符(code_list中的字符)。我真正需要做的是在original_list的某些位置放置更多的括号,从code_list中包含4个字符的元组开始,直到包含4个字符的下一个元组。 –

+0

@ david_10001:输出对我来说是正确的。或者至少,类似于你写的输出应该是什么。 –

+0

是的,你是对的。我在前面的代码中有一个错误。感谢您的帮助 –

3

从Ruby的背景来看,我经常觉得需要在Python中使用类似Enumerable#slice_before的东西。它基本上将任何迭代分割成块。该切片在谓词为真的每个元素之前完成。

基于Rubinius implementation,我将代码移植到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')]] 
+0

如何使用大括号将code_list转换为该格式? –

+0

@ david_10001:我不确定我是否理解这个问题。如果你有一个'['1321','0A3B','9K44']'的列表并且想要一个集合:你可以写'set(['1321','0A3B','9K44'])'或直接写成'{ '1321','0A3B','9K44'}' –

+0

是的,我已经尝试使用set(code_list),然后打印(code_list),它打印出来就像以前一样。 –

相关问题