简体   繁体   English

python无法一次遍历两个列表

[英]Cannot iterate through two lists at once python

I am having a problem with my python code, but I am not sure what it is. 我的python代码有问题,但是我不确定它是什么。 I am creating a program that creates a table with all possible combinations of four digits provided the digits do not repeat, which I know is successful. 我正在创建一个程序,该程序将创建一个包含四个数字的所有可能组合的表格,但前提是这些数字不会重复,我知道这是成功的。 Then, I create another table and attempt to add to this secondary table all of the values which use the same numbers in a different order (so I do not have, say, 1234, 4321, 3241, 3214, 1324, 2413, etc. on this table.) However, this does not seem to be working, as the second table has only one value. 然后,我创建另一个表,并尝试将使用相同数字的所有值以不同顺序添加到此辅助表中(因此我没有1234、4321、3241、3214、1324、2413等)。但是,这似乎不起作用,因为第二个表只有一个值。 What have I done wrong? 我做错了什么? My code is below. 我的代码如下。 Oh, and I know that the one value comes from appending the 1 at the top. 哦,我知道一个值来自在顶部附加1。

    combolisttwo = list()
    combolisttwo.append(1)
    combolist = {(a, b, c, d) for a in {1, 2, 3, 4, 5, 6, 7, 8, 9, 0} for b in {1, 2, 3, 4, 5, 6, 7, 8, 9, 0} for c in {1, 2, 3, 4, 5, 6, 7, 8, 9, 0} for d in {1, 2, 3, 4, 5, 6, 7, 8, 9, 0} if a != b and a != c and a != d and b != c and b != d and c!=d}
    for i in combolist:
        x = 0
        letternums = str(i)
        letters = list(letternums)
        for g in letters:
            n = 0
            hits = 0
            nonhits = 0
            letterstwo = str(combolisttwo[n])
            if g == letterstwo[n]:
                hits = hits + 1
            if g != letterstwo[n]:
                nonhits = nonhits + 1
            if hits == 4:
                break
            if hits + nonhits == 4:
                combolisttwo.append(i)
                break




    x = len(combolisttwo)

    print (x)

All possible combinations of four digits provided the digits do not repeat : 四个数字的所有可能组合,但不能重复

import itertools as IT
combolist = list(IT.combinations(range(10), 4))

Then, I create another table and attempt to add to this secondary table all of the values which use the same numbers in a different order (so I do not have, say, 1234, 4321, 3241, 3214, 1324, 2413, etc. on this table.) : 然后,我创建另一个表,并尝试将使用相同数字的所有值以不同顺序添加到此辅助表中(因此我没有1234、4321、3241、3214、1324、2413等)。在这张桌子上。)

combolist2 = [item for combo in combolist
              for item in IT.permutations(combo, len(combo))]

Useful references: 有用的参考资料:

  • combinations -- for enumerating collections of elements without replacement 组合 -用于枚举元素集合而无需替换
  • permutations -- for enumerating collections of elements in all possible orders 排列 -用于以所有可能的顺序枚举元素的集合

This code is pretty confused ;-) For example, you have n = 0 in your inner loop, but never set n to anything else. 这段代码很混乱;-)例如,您的内部循环中n = 0 ,但从未将n设置为其他任何值。 For another, you have x = 0 , but never use x . 另外,您有x = 0 ,但从不使用x Etc. 等等。

Using itertools is really best, but if you're trying to learn how to do these things yourself, that's fine. 使用itertools确实是最好的选择,但是如果您想自己学习如何做这些事情,那很好。 For a start, change your: 首先,请更改以下内容:

    letters = list(letternums)

to

    letters = list(letternums)
    print(letters)
    break

I bet you'll be surprised at what you see! 我敢打赌,您会对看到的内容感到惊讶! The elements of your combolist are tuples, so when you do letternums = str(i) you get a string with a mix of digits, spaces, parentheses and commas. 你的元素combolist是元组,所以当你letternums = str(i)你得到的数字,空格,括号和逗号的混合串。 I don't think you're expecting anything but digits. 我认为除了数字之外,您什么都没期望

Your letterstwo is the string "1" (always, because you never change n ). 您的letterstwo是字符串“ 1”(总是,因为您永远不会更改n )。 But it doesn't much matter, because you set hits and nonhits to 0 every time your for g in letters loop iterates. 但这没什么大不了的,因为每次您的for g in letters循环迭代时,您都将hitsnonhits设置为0。 So hits and nonhits can never be bigger than 1. 因此, hitsnonhits hits nonhits不得大于1。

Which answers your literal question ;-) combolisttwo.append(i) is never executed because hits + nonhits == 4 is never true. 这将回答您的字面问题;-) combolisttwo.append(i)永远不会执行,因为hits + nonhits == 4永远不会成立。 That's why combolisttwo remains at its initial value ( [1] ). 这就是为什么combolisttwo保持其初始值( [1] )的原因。

Put some calls to print() in your code? 在代码中添加对print()调用? That will help you see what's going wrong. 这将帮助您了解问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM