Home > OS >  How to create an ordered list of keys based on their value in a dict?
How to create an ordered list of keys based on their value in a dict?

Time:11-18

I'm quite stuck with this problem in Python and I'm pretty sure it should be pretty easy to solve.

Please find this dict example:

d = {
    "a": "abc1",
    "b": "abc1",
    "c": "abc2",
    "d": "abc3",
    "e": "abc3",
    "f": "abc3",
    "g": "abc4"
}

Now I want a to create a list where 'a' till 'g' will be put in an order that is mixing up abc values as much as possible, but I require all keys in the list so:

['a','c','d','g','b','e'] 

Then 'f' will be left over (because e also has value abc3) and can be added to a leftover list.

I tried the following:

s = []
for x in d: 
    if len(s) < 1:
        s.append(x)
    if d[s[-1]] is not d[x]:
        s.append(x)

But this will produce just:

['a', 'c', 'd', 'g']

I need to go back and try again until no solutions are possible.

Thanks a lot for your time and suggestions!

CodePudding user response:

This is the first code I got working. And now it also removes f. Any other ideas?

d = {
    "a": "abc1",
    "b": "abc1",
    "c": "abc2",
    "d": "abc3",
    "e": "abc3",
    "f": "abc3",
    "g": "abc4"
}

list_number = {}
key_lists = []

for key, value in d.items():
    if value in list_number:
        index = list_number[value] = list_number[value]   1
    else:
        index = list_number[value] = 0
    if index < len(key_lists):
        key_lists[index].append(key)
    else:
        key_lists.append([key])
result = []
remaining = []
for key_list in key_lists:
    if not result or len(key_list) > 1:
            result.extend(key_list)
    else:
        remaining.extend(key_list)
print("# Result", result)
print("# Remaining:", remaining)
# Result ['a', 'c', 'd', 'g', 'b', 'e']
# Remaining: ['f']

CodePudding user response:

Here is my solution:

d = {
    "a": "abc1",
    "b": "abc1",
    "c": "abc2",
    "d": "abc3",
    "e": "abc3",
    "f": "abc3",
    "g": "abc4"
}

counted = {}
done = False
while not done:
    # Assume done
    done = True
    last_value = None
    for key, value in d.items():
        if key not in counted and value != last_value:
            done = False
            last_value = value
            # Add the key to the result dict, the value 1 is arbitrary
            counted[key] = 1

result = list(counted)
print(f"Result: {result}")

Output:

Result: ['a', 'c', 'd', 'g', 'b', 'e', 'f']

Notes

  • The counted dictionary will have the same keys as in d, but ordered the way we want. The values are arbitrary. I could have used a list, but the look up key not in counted will take longer if d is a large dictionary.
  • The while loop will keep going until we added all keys to the counted dictionary.
  • The inner for loop will go through the keys/values and only add those keys that are qualified by the if conditions.
  • Related