如何在while循环中省略it.combinations()的一些结果

时间:2018-01-04 11:17:25

标签: python python-3.x while-loop combinations itertools

以下是创建特定图表的代码片段:

good=[]

其中:

z1, z2 - 以自然数[0,1,2,3,4,5,...]的形式列出当前值。

good=[] - it.combinations()列表中当前值的最大偶数和奇数。

执行while循环,直到图形边缘满足条件。但是,在while循环的每个后续步骤中,添加后续偶数和奇数以满足条件。

问题是在添加偶数和奇数后,函数while从头开始检查所有组合,包括那些已经检查过的组合。什么不必要地扩展了循环的操作。 不过,我希望z1 + h*2循环的后续步骤仅使用包含新添加的值z2 + h*2from string import punctuation, whitespace def first_word(s): to_strip = punctuation + whitespace return s.lstrip(to_strip).split(' ', 1)[0].rstrip(to_strip) tests = [ "Hello world", "a word", "don't touch it", "greetings, friends", "... and so on ...", "hi"] for test in tests: print('#{}#'.format(first_word(test))) 的组合。

如何做到这一点?

1 个答案:

答案 0 :(得分:1)

您可以在每个步骤中记住for的结果,并在下一步中仅针对h=0 previous_combinations = set() while len(added)<i+1: g.add_node(z1+h*2) g.add_node(z2+h*2) good.append(z1+h*2) good.append(z2+h*2) current_combinations = set(it.combinations(good, 8)) for u in current_combinations - previous_combinations: u1 = list(u) # the rest of the code... h=h+1 previous_combinations = current_combinations 循环中这些当前和之前结果的补充(差异)进行迭代。我建议使用集合上的操作来快速计算补充。

public class CustomerData
{
    public string Name { get; set; }

    public string PublicKey { get; set; }
}