[英]How to insert an empty string into list if there are not 3 strings between matches
How to insert an empty string to list if there are not 3 strings between matches. 如果匹配项之间没有3个字符串,如何插入一个空字符串以列出。 I want to find out if every 4th element is some kind of number (ie 12.1234., 1.12.13, etc.) and if not I want to insert an empty string before the second number so there are always 3 strings between each match.
我想找出每个第4个元素是否都是某种数字(即12.1234。,1.12.13等),如果不是,我想在第二个数字之前插入一个空字符串,以便每次匹配之间始终有3个字符串。
list = ['1.1', 'ab','ac','','1.2','dd','','1.3','cb','dd','', '1.4', 'de','']
wanted_list =['1.1', 'ab','ac','','1.2','dd','','', '1.3','cb','dd','', '1.4', 'de','','']
This is what Ii got so far, but the loop never ends and inserts way to many empty strings at the end (not just when there are not 3 strings between the matches). 这就是我到目前为止所获得的,但是循环永远不会结束,并且会在最后插入许多空字符串(不仅是匹配之间没有3个字符串的情况)。
list = ['1.1', 'ab','ac','','1.2','dd','','1.3','cb','dd','', '1.4', 'de','']
start_rx = re.compile('|'.join(
['\d\d\.\d\d\.\d\d\.\d\d\d', '\d\d\.\d\d\.\d\d\.', '\d\d\.\d\d\d\d', '\d\.\d\.\d\.', '\d\.\d\.\d\.\d\d\.',
'\d\.\d\.\d\.\d\d\d\.', 'A\d\d\d\d', '^\d\.', '^\d\.\d', '^\d\.\d\.\d', '^\d\.\d\.\d\d', '\d\d\.\d\d\.\d\d\d\d', '\d.\d']))
count = 1
for i, line in enumerate(list):
count += 4
if re.match(start_rx, line):
pass
else:
i=count
list.insert(i, '')
print (list)
The following approach groups the list into nested lists of digit/decimal values and others and iterates over the non-digit/decimal groups (odd indexed groups) to determine whether they contain the required 3 elements and fill with empty strings if needed. 以下方法将列表分为数字/十进制值和其他嵌套列表,并遍历非数字/十进制组(奇数索引组),以确定它们是否包含所需的3个元素,并在需要时用空字符串填充。 You could use regex in the
isfloat()
function below in line with your initial attempt but it seemed easier just to test for digits after removing decimals. 您可以根据您的初次尝试在下面的
isfloat()
函数中使用regex,但仅在删除小数后测试数字似乎更容易。
from itertools import groupby
def isfloat(s):
return s.replace('.','').isdigit()
items = ['1.1', 'ab','ac','','1.2','dd','','1.3','cb','dd','', '1.4', 'de','']
groups = [list(g) for _,g in groupby(items, key=isfloat)]
for group in groups[1::2]:
group += [''] * (3 - len(group))
result = [item for group in groups for item in group]
print(result)
# OUTPUT
# ['1.1', 'ab', 'ac', '', '1.2', 'dd', '', '', '1.3', 'cb', 'dd', '', '1.4', 'de', '', '']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.