【问题标题】:How do I create a string of array combinations given a list of "source code" strings?给定“源代码”字符串列表,如何创建数组组合字符串?
【发布时间】:2019-05-03 05:44:24
【问题描述】:

基本上,我得到一个字符串列表,例如:

["structA.structB.myArr[6].myVar",
"structB.myArr1[4].myArr2[2].myVar",
"structC.myArr1[3][4].myVar",
"structA.myArr1[4]",
"structA.myVar"]

这些字符串描述了来自多个结构的变量/数组。数组中的整数描述了每个数组的大小。给定一个字符串有一个/多个数组(1d 或 2d),我想生成一个字符串列表,该列表遍历该字符串数组中的每个索引组合。我想过使用 for 循环,但问题是在运行脚本之前我不知道给定字符串中有多少个数组。所以我不能做类似的事情

for i in range (0, idx1):
    for j in range (0, idx2):
         for k in range (0, idx3):
               arr.append(“structA.myArr1[%i][%i].myArr[%i]” %(idx1,idx2,idx3))

但问题是我不知道如何根据索引数量创建多个/动态 for 循环,以及如何创建动态追加语句,该语句从原始列表中的每个字符串更改,因为每个字符串都有不同数量的索引和数组将位于字符串的不同位置。

我能够编写一个正则表达式来查找我的字符串列表中每个字符串的所有索引:

indexArr = re.findall('\[(.*?)\]', myString)
//after looping, indexArr = [['6'],['4','2'],['3','4'],['4']]

但是,我真的很纠结如何实现“动态 for 循环”或为此使用递归。我想让我的字符串结束列表看起来像:

[
["structA.structB.myArr[0].myVar",
"structA.structB.myArr[1].myVar",
...
"structA.structB.myArr[5].myVar”],

[“structB.myArr1[0].myArr2[0].myVar",
"structB.myArr1[0].myArr2[1].myVar",
"structB.myArr1[1].myArr2[0].myVar",
…
"structB.myArr1[3].myArr2[1].myVar”],

[“structC.myArr1[0][0].myVar",
"structC.myArr1[0][1].myVar",
…
"structC.myArr1[2][3].myVar”],

[“structA.myArr1[0]”,
…
"structA.myArr1[3]”],

[“structA.myVar”] //this will only contain 1 string since there were no arrays
]

我真的坚持这一点,任何帮助表示赞赏。非常感谢。

【问题讨论】:

  • 输入内容和输出内容有点混乱。如果输入已经是这样的字符串数组,为什么需要这样的输出?目标是评估输入所指的内容吗?
  • 输入是代表源代码的各种字符串的数组,但是,它们具有包含数组大小的数组。例如,“structA.myArr[4]”。我想扩展该字符串以获得一整套源代码,这些源代码说明了数组的所有组合。所以从“structA.myArr[4]”我想得到“structA.myArr[0]”、“structA.myArr[1]”、“structA.myArr[2]”、“structA.myArr[3]”。输入已经是一个字符串数组,但是每个字符串都引用它自己的一组源代码,并且需要像上面的示例一样组合其数组

标签: arrays algorithm python-2.7 recursion dynamic


【解决方案1】:

关键是使用itertools.product 生成一组范围的所有可能组合,并将它们替换为适当构造的字符串模板的数组索引。

import itertools
import re
def expand(code):
    p = re.compile('\[(.*?)\]')
    ranges = [range(int(s)) for s in p.findall(code)]
    template = p.sub("[{}]", code)
    result = [template.format(*s) for s in itertools.product(*ranges)]
    return result

expand("structA.structB.myArr[6].myVar") 的结果是

['structA.structB.myArr[0].myVar',
 'structA.structB.myArr[1].myVar',
 'structA.structB.myArr[2].myVar',
 'structA.structB.myArr[3].myVar',
 'structA.structB.myArr[4].myVar',
 'structA.structB.myArr[5].myVar']

expand("structB.myArr1[4].myArr2[2].myVar")

['structB.myArr1[0].myArr2[0].myVar',
 'structB.myArr1[0].myArr2[1].myVar',
 'structB.myArr1[1].myArr2[0].myVar',
 'structB.myArr1[1].myArr2[1].myVar',
 'structB.myArr1[2].myArr2[0].myVar',
 'structB.myArr1[2].myArr2[1].myVar',
 'structB.myArr1[3].myArr2[0].myVar',
 'structB.myArr1[3].myArr2[1].myVar']

而角落案例expand("structA.myVar")自然会产生效果

['structA.myVar']

【讨论】:

  • 这正是我想要的!还有一个非常干净、精确的解决方案,也适用于角落案例。我没怎么用过itertools,但它看起来很强大。谢谢!
猜你喜欢
  • 2011-08-20
  • 1970-01-01
  • 2017-01-25
  • 2019-07-07
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多