Python - 如何连接列表中由特定值分隔的元素
Python - How to join elements in a list that are separated by a certain value
假设我有一个如下所示的列表:
['1', '+', '2', '+', '3', 'C', '4', 'C', '5', '-', '6', '+', '7', 'C', '8']
我想修改它,将 'C'
分隔的所有元素连接在一起。结果应如下所示:
['1', '+', '2', '+', '345', '-', '6', '+', '78']
我一直在尝试迭代地执行此操作,但问题是每当我删除一个元素时,列表的大小都会发生变化并且迭代会越界。任何指针表示赞赏。谢谢!
过滤掉 "C" 然后将连续的数字分组:
from itertools import groupby
data = ['1', '+', '2', '+', '3', 'C', '4', 'C', '5', '-', '6', '+', '7', 'C', '8']
new_data = [''.join(g) if k else next(g) for k, g in groupby((el for el in data if el != 'C'), key=str.isdigit)]
# ['1', '+', '2', '+', '345', '-', '6', '+', '78']
您可以使用仅在下一个值不是 C
时才生成值的生成器函数,否则会累积:
def join_by(it, sep):
it = iter(it)
prev = next(it)
for elem in it:
if elem == sep:
prev += next(it)
else:
yield prev
prev = elem
yield prev
演示;这不仅限于数字,也不限于特定的分隔符:
>>> orig = ['1', '+', '2', '+', '3', 'C', '4', 'C', '5', '-', '6', '+', '7', 'C', '8']
>>> list(join_by(orig, 'C'))
['1', '+', '2', '+', '345', '-', '6', '+', '78']
>>> list(join_by(['foo', '-', 'bar', 'baz'], '-'))
['foobar', 'baz']
要解决这个难题,您 可以 作弊并在此处使用 eval()
:
>>> import operator
>>> from itertools import product
>>> digits = '123456789'
>>> ops = (' - ', ' + ', '')
>>> for combo in product(ops, repeat=len(digits) - 1):
... expr = zip(digits, combo + ('',))
... expr = ''.join([c for digit_oper in expr for c in digit_oper])
... result = eval(expr)
... if result == 100:
... print(expr)
...
1 + 2 + 3 - 4 + 5 + 6 + 78 + 9
1 + 2 + 34 - 5 + 67 - 8 + 9
1 + 23 - 4 + 5 + 6 + 78 - 9
1 + 23 - 4 + 56 + 7 + 8 + 9
12 - 3 - 4 + 5 - 6 + 7 + 89
12 + 3 - 4 + 5 + 67 + 8 + 9
12 + 3 + 4 + 5 - 6 - 7 + 89
123 - 4 - 5 - 6 - 7 + 8 - 9
123 - 45 - 67 + 89
123 + 4 - 5 + 67 - 89
123 + 45 - 67 + 8 - 9
lst = ['1', '+', '2', '+', '3', 'C', '4', 'C', '5', '-', '6', '+', '7', 'C', '8']
newlist = [lst[0]]
for idx, item in enumerate(lst[1:]):
if item == 'C':
continue # do nothing if it's a C
if lst[idx] == 'C':
newlist[-1] += item # add it to the last item if it follows a C
else:
newlist.append(item) # add it to the list
结果:
['1', '+', '2', '+', '345', '-', '6', '+', '78']
此代码适用于任何给定的列表和值。
old_list = ['1', '+', '2', '+', '3', 'C', '4', 'C', '5', '-', '6', '+', '7', 'C', '8']
new_list = []
for index, val in enumerate(old_list):
if val == 'C':
new_list[-1] += old_list[index+1]
elif old_list[index-1] != 'C':
new_list.append(val)
无需列表理解,只需找到分隔符并连接其余部分
a = ['1', '+', '2', '+', '3', 'C', '4', 'C', '5', '-', '6', '+', '7', 'C', '8']
firstIndex = a.index('C')
revertedLastIndex = a[::-1].index('C') # index in reverted list
lastIndex = len(a) - 1 - revertedLastIndex # -1 for 0-indexed lists
所以我们现在可以加入 firstIndex
和 lastIndex
之间的列表部分,然后连接其余部分
newList = a[:firstIndex] + ["".join(a[firstIndex:lastIndex])] + a[lastIndex:]
编辑:"no need for list comprehension" 我的意思是当然不需要 显式 迭代,index
函数在内部完成
import ast
L=['1', '+', '2', '+', '3', 'C', '4', 'C', '5', '-', '6', '+', '7', 'C', '8']
s=str(L).replace(", 'C', ", "")
out=ast.literal_eval(s)
print out
输出:
['1', '+', '2', '+', '345', '-', '6', '+', '78']
Python 的奇特功能非常好,因为它们使代码的逻辑非常非常容易理解。如果他们不这样做,我更愿意保持简单和可验证:
stuff = ['1', '+', '2', '+', '3', 'C', '4', 'C', '5', '-', '6', '+', '7', 'C', '8']
concat = False
newstuff = []
for a in stuff:
if concat:
newstuff[-1] += a
concat = False
elif a == "C":
concat = True
else:
newstuff.append(a)
假设我有一个如下所示的列表:
['1', '+', '2', '+', '3', 'C', '4', 'C', '5', '-', '6', '+', '7', 'C', '8']
我想修改它,将 'C'
分隔的所有元素连接在一起。结果应如下所示:
['1', '+', '2', '+', '345', '-', '6', '+', '78']
我一直在尝试迭代地执行此操作,但问题是每当我删除一个元素时,列表的大小都会发生变化并且迭代会越界。任何指针表示赞赏。谢谢!
过滤掉 "C" 然后将连续的数字分组:
from itertools import groupby
data = ['1', '+', '2', '+', '3', 'C', '4', 'C', '5', '-', '6', '+', '7', 'C', '8']
new_data = [''.join(g) if k else next(g) for k, g in groupby((el for el in data if el != 'C'), key=str.isdigit)]
# ['1', '+', '2', '+', '345', '-', '6', '+', '78']
您可以使用仅在下一个值不是 C
时才生成值的生成器函数,否则会累积:
def join_by(it, sep):
it = iter(it)
prev = next(it)
for elem in it:
if elem == sep:
prev += next(it)
else:
yield prev
prev = elem
yield prev
演示;这不仅限于数字,也不限于特定的分隔符:
>>> orig = ['1', '+', '2', '+', '3', 'C', '4', 'C', '5', '-', '6', '+', '7', 'C', '8']
>>> list(join_by(orig, 'C'))
['1', '+', '2', '+', '345', '-', '6', '+', '78']
>>> list(join_by(['foo', '-', 'bar', 'baz'], '-'))
['foobar', 'baz']
要解决这个难题,您 可以 作弊并在此处使用 eval()
:
>>> import operator
>>> from itertools import product
>>> digits = '123456789'
>>> ops = (' - ', ' + ', '')
>>> for combo in product(ops, repeat=len(digits) - 1):
... expr = zip(digits, combo + ('',))
... expr = ''.join([c for digit_oper in expr for c in digit_oper])
... result = eval(expr)
... if result == 100:
... print(expr)
...
1 + 2 + 3 - 4 + 5 + 6 + 78 + 9
1 + 2 + 34 - 5 + 67 - 8 + 9
1 + 23 - 4 + 5 + 6 + 78 - 9
1 + 23 - 4 + 56 + 7 + 8 + 9
12 - 3 - 4 + 5 - 6 + 7 + 89
12 + 3 - 4 + 5 + 67 + 8 + 9
12 + 3 + 4 + 5 - 6 - 7 + 89
123 - 4 - 5 - 6 - 7 + 8 - 9
123 - 45 - 67 + 89
123 + 4 - 5 + 67 - 89
123 + 45 - 67 + 8 - 9
lst = ['1', '+', '2', '+', '3', 'C', '4', 'C', '5', '-', '6', '+', '7', 'C', '8']
newlist = [lst[0]]
for idx, item in enumerate(lst[1:]):
if item == 'C':
continue # do nothing if it's a C
if lst[idx] == 'C':
newlist[-1] += item # add it to the last item if it follows a C
else:
newlist.append(item) # add it to the list
结果:
['1', '+', '2', '+', '345', '-', '6', '+', '78']
此代码适用于任何给定的列表和值。
old_list = ['1', '+', '2', '+', '3', 'C', '4', 'C', '5', '-', '6', '+', '7', 'C', '8']
new_list = []
for index, val in enumerate(old_list):
if val == 'C':
new_list[-1] += old_list[index+1]
elif old_list[index-1] != 'C':
new_list.append(val)
无需列表理解,只需找到分隔符并连接其余部分
a = ['1', '+', '2', '+', '3', 'C', '4', 'C', '5', '-', '6', '+', '7', 'C', '8']
firstIndex = a.index('C')
revertedLastIndex = a[::-1].index('C') # index in reverted list
lastIndex = len(a) - 1 - revertedLastIndex # -1 for 0-indexed lists
所以我们现在可以加入 firstIndex
和 lastIndex
之间的列表部分,然后连接其余部分
newList = a[:firstIndex] + ["".join(a[firstIndex:lastIndex])] + a[lastIndex:]
编辑:"no need for list comprehension" 我的意思是当然不需要 显式 迭代,index
函数在内部完成
import ast
L=['1', '+', '2', '+', '3', 'C', '4', 'C', '5', '-', '6', '+', '7', 'C', '8']
s=str(L).replace(", 'C', ", "")
out=ast.literal_eval(s)
print out
输出:
['1', '+', '2', '+', '345', '-', '6', '+', '78']
Python 的奇特功能非常好,因为它们使代码的逻辑非常非常容易理解。如果他们不这样做,我更愿意保持简单和可验证:
stuff = ['1', '+', '2', '+', '3', 'C', '4', 'C', '5', '-', '6', '+', '7', 'C', '8']
concat = False
newstuff = []
for a in stuff:
if concat:
newstuff[-1] += a
concat = False
elif a == "C":
concat = True
else:
newstuff.append(a)