简体   繁体   English

如果匹配项之间没有3个字符串,如何将一个空字符串插入列表

[英]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.

相关问题 在字典和列表之间查找匹配的字符串并用字符串替换匹配项 - Find matching strings between dictionary and list and replace matches with string 正则表达式匹配项之间的字符串列表 - List of strings between regex matches 如何将字符串插入字符串列表的每个标记? - How to insert string to each token of a list of strings? 如果字符串与列表中的字符串匹配,我如何从句子中删除字符串 - How can i remove strings from sentences if string matches with strings in list 在 Python 中打印时如何在其他两个字符串之间插入一个字符串 - How to insert a string between two other strings while printing in Python 如何在子列表中的字符串之间找到完全匹配 - How to find exact matches between strings in sublists 如何获取字符串列表并查找名称与列表中的字符串匹配的文件? - How can I take a list of strings and find files who name matches a string in the list? 如果列表中的一个或多个字符串与特定列中的值(字符串)匹配,那么如何用匹配的字符串填充新列? - If a string or multiple strings from a list matches the value(strings) from a specific column then, how to populate a new column with matched strings? 查找数据框(字符串列表)和字典键(元组)之间的部分匹配 - Find partial matches between a dataframe (list of strings) and dictionary keys (tuples) 查找输入字符串和一组固定字符串之间的匹配项 - Finding matches between an input string and a fixed set of strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM