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?
JavaScript
x
25
25
1
one = {
2
"A" : "A",
3
"B" : "B",
4
"C" : "C",
5
"D" : "D",
6
"E" : "E",
7
"F" : "F",
8
"G" : "G",
9
"H" : "H"
10
}
11
two = {
12
"A" : "A",
13
"B" : "B",
14
"C" : "C",
15
"D" : "D",
16
"E" : "E",
17
"F" : "F",
18
"G" : "G",
19
"H" : "H"
20
}
21
22
for botname,bot in one.items():
23
for topname, top in two.items():
24
print (str(bot) + str(top))
25
Output:
JavaScript
1
65
65
1
AA*
2
AB
3
AC
4
AD
5
AE
6
AF
7
AG
8
AH
9
BA*
10
BB*
11
BC
12
BD
13
BE
14
BF
15
BG
16
BH
17
CA*
18
CB*
19
CC*
20
CD
21
CE
22
CF
23
CG
24
CH
25
DA*
26
DB*
27
DC*
28
DD*
29
DE
30
DF
31
DG
32
DH
33
EA*
34
EB*
35
EC*
36
ED*
37
EE*
38
EF
39
EG
40
EH
41
FA*
42
FB*
43
FC*
44
FD*
45
FE*
46
FF*
47
FG
48
FH
49
GA*
50
GB*
51
GC*
52
GD*
53
GE*
54
GF*
55
GG*
56
GH
57
HA*
58
HB*
59
HC*
60
HD*
61
HE*
62
HF*
63
HG*
64
HH*
65
‘*’ it’s not output, outputs with * I want to skip
Thanks in advance!
Advertisement
Answer
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
.
JavaScript
1
5
1
for bot in one.values():
2
for top in two.values():
3
if bot < top:
4
print(bot + top)
5
There’s no need to use .items()
if you’re only using the values, not the keys.