简体   繁体   中英

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. 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. 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.

    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.) :

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. For another, you have x = 0 , but never use x . Etc.

Using itertools is really best, but if you're trying to learn how to do these things yourself, that's fine. 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. I don't think you're expecting anything but digits.

Your letterstwo is the string "1" (always, because you never change n ). But it doesn't much matter, because you set hits and nonhits to 0 every time your for g in letters loop iterates. So hits and nonhits can never be bigger than 1.

Which answers your literal question ;-) combolisttwo.append(i) is never executed because hits + nonhits == 4 is never true. That's why combolisttwo remains at its initial value ( [1] ).

Put some calls to print() in your code? That will help you see what's going wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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