[英]Python Concatenate consecutive True values in list of lists
I have a list where each item is also a list.我有一个列表,其中每个项目也是一个列表。 Each item contains some value and then a True or False value.每个项目都包含一些值,然后是 True 或 False 值。
eg例如
list = [
['purple', False],
['yellow', True],
['red', True],
['blue', False]
['green', True],
['orange', True],
['black', True],
['cyan', False]
]
the desired output is to concatenate any True values that occur consecutively like so:所需的 output 是连接任何连续出现的 True 值,如下所示:
desired_list = [
['purple', False],
['yellowred', True],
['blue', False]
['greenorangeblack', True],
['cyan', False]
]
where, for example, 'yellow' and 'red' now both exist as a single string.例如,“黄色”和“红色”现在都作为单个字符串存在。
Thanks in advance for any help提前感谢您的帮助
Solution without itertools
没有itertools
解决方案
data = [
['purple', False],
['yellow', True],
['red', True],
['blue', False],
['green', True],
['orange', True],
['black', True],
['cyan', False]
]
out = []
prev = False
for i in data:
if not prev or not i[1]:
out.append(i)
else:
out[-1][0] += i[0]
prev = i[1]
print(out)
[['purple', False],
['yellowred', True],
['blue', False],
['greenorangeblack', True],
['cyan', False]]
You can use groupby
from the itertools
package and a list comprehension to do this.您可以使用itertools
package 中的groupby
和列表理解来执行此操作。 Below groupby
groups items of the list together by the last element of each item.下面的groupby
按每个项目的最后一个元素将列表中的项目组合在一起。 The for-loop iterates over each grouping, merges the strings, and add the True/False. for 循环遍历每个分组,合并字符串,并添加 True/False。
from itertools import groupby
x = [
['purple', False],
['yellow', True],
['red', True],
['blue', False],
['green', True],
['orange', True],
['black', True],
['cyan', False]
]
[[''.join(e[0] for e in g), k] for k, g in groupby(x, key=lambda x: x[-1])]
# returns:
[['purple', False],
['yellowred', True],
['blue', False],
['greenorangeblack', True],
['cyan', False]]
You can nicely solve it by recursion:您可以通过递归很好地解决它:
def compress(list_, i):
if i + 1 == len(list_):
return
if list[i][1] and list[i+1][1]:
list[i][0] += list[i+1][0]
del list[i+1]
compress(list_, i)
compress(list_, i + 1)
This doesn't create another list.这不会创建另一个列表。 It modifies the existing list.它修改现有列表。
list = [
['purple', False],
['yellow', True],
['red', True],
['blue', False],
['green', True],
['orange', True],
['black', True],
['cyan', False]
]
for item in list:
i_now = list.index(item)
if item[1]:
while list[i_now+1][1]:
item[0] = item[0] +'-'+ list[i_now+1][0]
list.remove(list[i_now+1])
print(list)
Try using this function:尝试使用这个 function:
def get_concatenated(my_list):
result = []
concatenated_string = ''
for i, item in enumerate(my_list):
if item[1]:
concatenated_string += item[0]
# if last element is true
if i == len(my_list) - 1:
result.append([concatenated_string, True])
continue
if concatenated_string:
result.append([concatenated_string, True])
result.append(item)
concatenated_string = ''
return result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.