小编dm2*_*111的帖子

List comprehension combine 2 elements in nested list

Is it possible to use list comprehension to combine 2 elements in a nested list based on the occurrence of a character, eg: if you encounter '+' combine with the next element? I have some code that does this using nested loops, but trying to achieve using list comprehension.

Input: l = [['A-2', 'A-3', 'A-4', '+', '100', 'A-5'],['B-2', 'B-3', 'B-4', '+', '500', 'B-5']]

Output: l = [['A-2', 'A-3', 'A-4', '+100', 'A-5'],['B-2', 'B-3', 'B-4', '+500', 'B-5']]

Code:

for nested in l: …
Run Code Online (Sandbox Code Playgroud)

python loops list-comprehension list nested-lists

1
推荐指数
1
解决办法
107
查看次数

标签 统计

list ×1

list-comprehension ×1

loops ×1

nested-lists ×1

python ×1