Twister Joe
Twister Joe

Reputation: 131

Python3.x Converting Dict's Keys to Readable Strings

I have some numbers to show the user and here I used dictionaries but I couldn't get values.

Given this input, in list form:

inp = ['7965077', '741300', '873720', '36860', '82753', '33190', '5206', '5361','7273', '6201', '7862', '025', '437', '990', '798', '791', '70', '68', '21', '0', '2']

...I converted to this intermediate dict form:

{('Last', 7, 'Digits', 0): '7965077', ('Last', 6, 'Digits', 1): '741300', ('Last', 6, 'Digits', 2): '873720', ('Last', 5, 'Digits', 3): '36860', ('Last', 5, 'Digits', 4): '82753', ('Last', 5, 'Digits', 5): '33190', ('Last', 4, 'Digits', 6): '5206', ('Last', 4, 'Digits', 7): '5361', ('Last', 4, 'Digits', 8): '7273', ('Last', 4, 'Digits', 9): '6201', ('Last', 4, 'Digits', 10): '7862', ('Last', 3, 'Digits', 11): '025', ('Last', 3, 'Digits', 12): '437', ('Last', 3, 'Digits', 13): '990', ('Last', 3, 'Digits', 14): '798', ('Last', 3, 'Digits', 15): '791', ('Last', 2, 'Digits', 16): '70', ('Last', 2, 'Digits', 17): '68', ('Last', 2, 'Digits', 18): '21', ('Last', 1, 'Digits', 19): '0', ('Last', 1, 'Digits', 20): '2'}

But I really want to convert to this dictionary end-form like this:

Last 7 Digits
7965077
Last 6 Digits
741300
873720
Last 5 Digits
36860
82753
33190
Last 4 Digits
5361
5206
7273
6201
7862


['7965077', '741300', '873720', '36860', '82753', '33190', '5206', '5361', '7273', '6201', '7862', '025', '437', '990', '798', '791', '70', '68', '21', '0', '2']
{('Last', 7, 'Digits', 0): '7965077', ('Last', 6, 'Digits', 1): '741300', ('Last', 6, 'Digits', 2): '873720', ('Last', 5, 'Digits', 3): '36860', ('Last', 5, 'Digits', 4): '82753', ('Last', 5, 'Digits', 5): '33190', ('Last', 4, 'Digits', 6): '5206', ('Last', 4, 'Digits', 7): '5361', ('Last', 4, 'Digits', 8): '7273', ('Last', 4, 'Digits', 9): '6201', ('Last', 4, 'Digits', 10): '7862', ('Last', 3, 'Digits', 11): '025', ('Last', 3, 'Digits', 12): '437', ('Last', 3, 'Digits', 13): '990', ('Last', 3, 'Digits', 14): '798', ('Last', 3, 'Digits', 15): '791', ('Last', 2, 'Digits', 16): '70', ('Last', 2, 'Digits', 17): '68', ('Last', 2, 'Digits', 18): '21', ('Last', 1, 'Digits', 19): '0', ('Last', 1, 'Digits', 20): '2'}

Upvotes: 0

Views: 151

Answers (3)

J. Bakker
J. Bakker

Reputation: 356

I changed your intermediate dictionary as it contains text that are 'constant'. I added the code to construct a new dictionary by using a defaultdict.

# construct the dict from the input data
from collections import defaultdict
inp = ['7965077', '741300', '873720', '36860', '82753', '33190', '5206', '5361','7273', '6201', '7862', '025', '437', '990', '798', '791', '70', '68', '21', '0', '2']
out = defaultdict(list)
[out[len(x)].append(x) for x in inp]

Dictionaries are not ordered you could convert it to an OrderedDict, I use the sorted method to generate a list of sorted keys. Which I reverse to put the major on top.

Now we can just loop and print out the result.

# print the result in the desired way.
for key, values in [(x, out[x]) for x in reversed(sorted(out.keys()))]:
    print("Last {key} Digits".format(key=key))
    print("\n".join(values)

Upvotes: 1

quamrana
quamrana

Reputation: 39404

You can use a defaultdict to collect up the numbers:

from collections import defaultdict

multikey = {('Last', 7, 'Digits', 0): '7965077', ('Last', 6, 'Digits', 1): '741300', ('Last', 6, 'Digits', 2): '873720', ('Last', 5, 'Digits', 3): '36860', ('Last', 5, 'Digits', 4): '82753', ('Last', 5, 'Digits', 5): '33190', ('Last', 4, 'Digits', 6): '5206', ('Last', 4, 'Digits', 7): '5361', ('Last', 4, 'Digits', 8): '7273', ('Last', 4, 'Digits', 9): '6201', ('Last', 4, 'Digits', 10): '7862', ('Last', 3, 'Digits', 11): '025', ('Last', 3, 'Digits', 12): '437', ('Last', 3, 'Digits', 13): '990', ('Last', 3, 'Digits', 14): '798', ('Last', 3, 'Digits', 15): '791', ('Last', 2, 'Digits', 16): '70', ('Last', 2, 'Digits', 17): '68', ('Last', 2, 'Digits', 18): '21', ('Last', 1, 'Digits', 19): '0', ('Last', 1, 'Digits', 20): '2'}

singlekey = defaultdict(list)

for k,v in multikey.items():
    a,b,c,_ = k
    singlekey[(a,b,c)].append(v)

for k,v in singlekey.items():
    print(' '.join((str(s) for s in k)))
    for n in v:
        print(n)

Output:

Last 7 Digits
7965077
Last 6 Digits
741300
873720
Last 5 Digits
36860
82753
33190
Last 4 Digits
5206
5361
7273
6201
7862
Last 3 Digits
025
437
990
798
791
Last 2 Digits
70
68
21
Last 1 Digits
0
2        

Upvotes: 0

Igl3
Igl3

Reputation: 5108

If I get it right, you want to have a dictionary with the length of the numbers as key and a list of the matching numbers as value.

Iterate over the first list and group them in a dict by their length like this:

your_list = ['7965077', '741300', '873720', '36860', '82753', '33190', '5206', '5361','7273', '6201', '7862', '025', '437', '990', '798', '791', '70', '68', '21', '0', '2']

res = {}

for item in your_list:
    res.setdefault(len(item), []).append(item)

for k,v in res.items():
    print('Last {} Digits: '.format(k) + ' '.join(v)

# Prints:
# Last 7 Digits: 7965077
# Last 6 Digits: 741300 873720
# Last 5 Digits: 36860 82753 33190
# Last 4 Digits: 5206 5361 7273 6201 7862
# Last 3 Digits: 025 437 990 798 791
# Last 2 Digits: 70 68 21
# Last 1 Digits: 0 2

Upvotes: 0

Related Questions