当我满足特定条件时,如何在 i 的任一侧组合 Python 列表元素
How to combine Python list elements on either side of i when i mets a certain condition
Stack Overflow 上已经有类似的问题,但我在引用 i 之前的项目时遇到问题。我正在处理一个字符串列表,并且只需要在某个字符串以特定字符开头时组合列表中的相邻字符串,因为该字符串错误地划分了相邻的字符串。例如:
list = ["a","b","<s16","c","d"]
在这种情况下,我想组合与以 "<s16"
开头的字符串相邻的任何两个元素(开头是因为每次出现都包含不同的数字)。所以正确的列表应该是这样的:list = ["a","bc","d"]
我尝试了几种方法,反复出现的问题是:
i.startswith
不适用于整数对象(当我尝试使用 range(len(list))
作为字符串索引时)
- 尝试在
i
之前引用对象(例如 list.pop(i-1))
导致不支持的操作数类型的类型错误,我猜是因为它认为我正在尝试从字符串中减去 1而不是引用以 <s16>
开头的给定元素之前的元素
我已经尝试使用 re.match
和 re.findall
来解决第一个问题,但它似乎并没有准确地找到正确的列表项。
if any(re.match('<s16') for i in list):
提前感谢您的帮助,我也为我的无知提前道歉,我是新手。
最容易在此处使用 while
循环:
def join(l, sep="<s16"):
i = 1
while i < len(l) - 1:
if l[i].startswith(sep):
l.pop(i) # remove the separator (at current index)
l[i-1] += l.pop(i) # join next element to previous
else:
i += 1
l = ["a","b","<s16abc","c","d", "<s16def", "e", "f"]
join(l)
print(l)
# ['a', 'bc', 'de', 'f']
也不要命名您的列表 list
,因为它会以该名称隐藏内置项,这不是一个好主意。
最好使用re
模块
import re
mylist = ["<s1", "a","b","<s16", "<s18", "c", "d", "e", "f", "<s16", "g", "h", "i", "j", "<s135"]
# So you will catch strings which starts with "<s" followed by some digits
# and after zero or more entries of any caracter.
r = "^<s\d+.*"
i = 0
while i < len(mylist):
item = mylist[i]
# If you are at the start of the list just pop the first item
if (i == 0) and re.search(r, item):
mylist.pop(i)
# If you are at the end of the list just pop the last item
elif (i == len(mylist) - 1) and re.search(r, item):
mylist.pop(i)
# If you have found a wrong item inside the list
# continue until you delete all consecutive entries
elif re.search(r, item):
mylist.pop(i)
item = mylist[i]
while re.search(r, item):
mylist.pop(i)
item = mylist[i]
mylist[i-1] += mylist[i]
mylist.pop(i)
else:
i += 1
print(mylist)
# ['a', 'bc', 'd', 'e', 'fg', 'h', 'i', 'j']
PS:您可以添加更多选项,使用更多正则表达式来捕捉不同的情况[=13=]
Stack Overflow
list = ["a","b","<s16","c","d"]
在这种情况下,我想组合与以 "<s16"
开头的字符串相邻的任何两个元素(开头是因为每次出现都包含不同的数字)。所以正确的列表应该是这样的:list = ["a","bc","d"]
我尝试了几种方法,反复出现的问题是:
i.startswith
不适用于整数对象(当我尝试使用range(len(list))
作为字符串索引时)- 尝试在
i
之前引用对象(例如list.pop(i-1))
导致不支持的操作数类型的类型错误,我猜是因为它认为我正在尝试从字符串中减去 1而不是引用以<s16>
开头的给定元素之前的元素
我已经尝试使用 re.match
和 re.findall
来解决第一个问题,但它似乎并没有准确地找到正确的列表项。
if any(re.match('<s16') for i in list):
提前感谢您的帮助,我也为我的无知提前道歉,我是新手。
最容易在此处使用 while
循环:
def join(l, sep="<s16"):
i = 1
while i < len(l) - 1:
if l[i].startswith(sep):
l.pop(i) # remove the separator (at current index)
l[i-1] += l.pop(i) # join next element to previous
else:
i += 1
l = ["a","b","<s16abc","c","d", "<s16def", "e", "f"]
join(l)
print(l)
# ['a', 'bc', 'de', 'f']
也不要命名您的列表 list
,因为它会以该名称隐藏内置项,这不是一个好主意。
最好使用re
模块
import re
mylist = ["<s1", "a","b","<s16", "<s18", "c", "d", "e", "f", "<s16", "g", "h", "i", "j", "<s135"]
# So you will catch strings which starts with "<s" followed by some digits
# and after zero or more entries of any caracter.
r = "^<s\d+.*"
i = 0
while i < len(mylist):
item = mylist[i]
# If you are at the start of the list just pop the first item
if (i == 0) and re.search(r, item):
mylist.pop(i)
# If you are at the end of the list just pop the last item
elif (i == len(mylist) - 1) and re.search(r, item):
mylist.pop(i)
# If you have found a wrong item inside the list
# continue until you delete all consecutive entries
elif re.search(r, item):
mylist.pop(i)
item = mylist[i]
while re.search(r, item):
mylist.pop(i)
item = mylist[i]
mylist[i-1] += mylist[i]
mylist.pop(i)
else:
i += 1
print(mylist)
# ['a', 'bc', 'd', 'e', 'fg', 'h', 'i', 'j']
PS:您可以添加更多选项,使用更多正则表达式来捕捉不同的情况[=13=]