'Iterate between two python dictionaries and be able to "increment" to the next string index separately
Trying to learn python so I don't even know if there's a feature for this. but lets say I have the following two dictionaries
dict1 = {
'key1': 1
'key2': 2
'key3': 3
}
dict2 = {
'key1': 0
'key2': 2
'key3': 4
}
Now I want to iterate between the two dictionaries but if one of the keys' value is 0, I want the other dictionary to move to the next string index. You can assume both dictionaries have the same number of keys and key names.
Basically I want something like this:
for someKey, anotherKey in dict1, dict2:
if dict1[someKey] == 0:
#Move someKey to the next string index
#In other words move someKey from 'key1' -> 'key2' (On loop 0)
if dict2[anotherKey] == 0:
#Move anotherKey to the next string index
#In other words move someKey from 'key1' -> 'key2' (On loop 0)
Was looking into zip, but I don't think it solves the problem of maintaining separate string indices
So example output would be like:
(loop 1)
1 == 0? no
0 == 0? yes
Now only move anotherKey from 'key1' to 'key2'
(loop 2)
1 == 0? no
2 == 0? no
Now here it would infinitely loop here, but I have some code
that does some dice rolling and decrements one of the
values so it guarantees one number will eventually be 0.
Just keep that in mind.
Solution 1:[1]
I don't believe there is a "pythonistic" way to solve it. Instead, you can try an approach more "algorithmic" using while:
keys1 = list(dict1.keys())
keys2 = list(dict2.keys())
i = j = 0
while i < len(keys1) and j < len(keys2):
if dict1[keys1[i]] == 0:
i += 1
if dict2[keys2[j]] == 0:
j += 1
I think may be there is another solution but this is practical.
Solution 2:[2]
One way to do this is using iterators. See iter() and next(), both Python built-in functions documented here and here for some background.
dict1 = {
'key1': 1,
'key2': 2,
'key3': 3
}
dict2 = {
'key1': 0,
'key2': 2,
'key3': 4
}
it1 = iter(dict1)
it2 = iter(dict2)
v1 = next(it1, None)
v2 = next(it2, None)
count = 0
while v1 is not None or v2 is not None:
print(f"v1, dict1[v1] {v1, dict1[v1] if v1 is not None else 'na'}, v2, dict2[v2] {v2, dict2[v2] if v2 is not None else 'na'}")
if v1 is not None and dict1[v1] == 0:
v1 = next(it1, None)
if v2 is not None and dict2[v2] == 0:
v2 = next(it2, None)
# simulate dice rolling and decrement mentioned in the question
count += 1
if count == 3:
dict1['key1'] = 0
elif count == 6:
dict2['key2'] = 0
elif count == 9:
dict1['key2'] = 0
dict2['key3'] = 0
elif count == 12:
dict1['key3'] = 0
print(f"v1, dict1[v1] {v1, dict1[v1] if v1 is not None else 'na'}, v2, dict2[v2] {v2, dict2[v2] if v2 is not None else 'na'}")
Output:
v1, dict1[v1] ('key1', 1), v2, dict2[v2] ('key1', 0)
v1, dict1[v1] ('key1', 1), v2, dict2[v2] ('key2', 2)
v1, dict1[v1] ('key1', 1), v2, dict2[v2] ('key2', 2)
v1, dict1[v1] ('key1', 0), v2, dict2[v2] ('key2', 2)
v1, dict1[v1] ('key2', 2), v2, dict2[v2] ('key2', 2)
v1, dict1[v1] ('key2', 2), v2, dict2[v2] ('key2', 2)
v1, dict1[v1] ('key2', 2), v2, dict2[v2] ('key2', 0)
v1, dict1[v1] ('key2', 2), v2, dict2[v2] ('key3', 4)
v1, dict1[v1] ('key2', 2), v2, dict2[v2] ('key3', 4)
v1, dict1[v1] ('key2', 0), v2, dict2[v2] ('key3', 0)
v1, dict1[v1] ('key3', 3), v2, dict2[v2] (None, 'na')
v1, dict1[v1] ('key3', 3), v2, dict2[v2] (None, 'na')
v1, dict1[v1] ('key3', 0), v2, dict2[v2] (None, 'na')
v1, dict1[v1] (None, 'na'), v2, dict2[v2] (None, 'na')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharman |
| Solution 2 | constantstranger |
