project.py
project.py

Reputation: 103

How to skip some results using double for loop?

I have a problem, so I made it simplified code, everything how it is here need to stay that way because of my bigger problem. I have 2 dictionaries and I have 2 for loops. I need to skip outputs with same result like AA, BB, CC etc... and I need to remove one of AB or BA, I need to keep AB, but I don't need BA. Can you help me with this problem, but I need to keep it simple for my real problem?

one = {
    "A" : "A",
    "B" : "B",
    "C" : "C",
    "D" : "D",
    "E" : "E",
    "F" : "F",
    "G" : "G",
    "H" : "H"
}
two = {
    "A" : "A",
    "B" : "B",
    "C" : "C",
    "D" : "D",
    "E" : "E",
    "F" : "F",
    "G" : "G",
    "H" : "H"
}

for botname,bot in one.items():
    for topname, top in two.items():
            print (str(bot) + str(top))

Output:

AA*
AB
AC
AD
AE
AF
AG
AH
BA*
BB*
BC
BD
BE
BF
BG
BH
CA*
CB*
CC*
CD
CE
CF
CG
CH
DA*
DB*
DC*
DD*
DE
DF
DG
DH
EA*
EB*
EC*
ED*
EE*
EF
EG
EH
FA*
FB*
FC*
FD*
FE*
FF*
FG
FH
GA*
GB*
GC*
GD*
GE*
GF*
GG*
GH
HA*
HB*
HC*
HD*
HE*
HF*
HG*
HH*

'*' it's not output, outputs with * I want to skip

Thanks in advance!

Upvotes: 0

Views: 185

Answers (4)

Han Parlak
Han Parlak

Reputation: 777

You can create a new set to store the opposite values that you've added to your list. For example:

  • If you added AA or AB to your list, you can add AA and BA to your set.
  • If you added BC or CD to your list, now your set comprises CB and DC
  • Since sets only keep one distinct value, you can store the recurrent values without any extra write operations on memory.

So, after all, your code should look like this.:

one = {
    "A" : "A",
    "B" : "B",
    "C" : "C",
    "D" : "D",
    "E" : "E",
    "F" : "F",
    "G" : "G",
    "H" : "H"
}
two = {
    "A" : "A",
    "B" : "B",
    "C" : "C",
    "D" : "D",
    "E" : "E",
    "F" : "F",
    "G" : "G",
    "H" : "H"
}

values = set()
for botname,bot in one.items():
    for topname, top in two.items():
      if bot != top and bot+top not in values: # checks if the value is not AA or already in values set (which consists of opposites of what we've printed already)
        print (str(bot) + str(top))
        values.add(top+bot)                    # add the opposite of the printed text to our set

Upvotes: 2

Daisy Welham
Daisy Welham

Reputation: 235

I would do something like this, as it seems to have your desired output:

one = ["A","B", "C", "D", "E", "F", "G", "H"]
two = one
s =""
for j in range(len(one)):
    for k in range(len(two)):
        if j != k:
            print(s.join([one[j],two[k]]))

At present the "two = one" part is kind of redundant because "two" is exactly the same as "one" here anyway so we could have just used "one" twice, but coding it this way gives the option for "one" and "two" to be different without breaking the script.

Upvotes: 2

Barmar
Barmar

Reputation: 782166

You can just keep the pairs where bot < top. This will discard the duplicates where top < bot, as well as the pairs where top == bot.

for bot in one.values():
    for top in two.values():
        if bot < top:
            print(bot + top)

There's no need to use .items() if you're only using the values, not the keys.

Upvotes: 3

msi_gerva
msi_gerva

Reputation: 2078

Combing with Barmar answer to 2 lines of code:

letterlist = ['A','B','C','D','E','F','G','H']
output = [val+valb for val in letterlist for valb in letterlist if val<valb]

Your letterlist can also be the keys of the dictionaries:

dd = [val+valb for val in one.keys() for valb in two.keys() if val<valb ]

Upvotes: 1

Related Questions