Home > Net >  represent list of dicts as a string
represent list of dicts as a string

Time:10-27

I have many lists of dictionaries with keys: origin/destination that I want to represent in a simpler and shorter way.

What I want is to represent the origins with lowercase letters and the destinations with uppercase letters. In case any value is repeated, the same letter that was already used must be used. Each dictionary value must be separated by a hyphen and each dictionary separated by a space.

For example, the following list would be: a-A a-B a-C a-D a-E

[{'destination': '0390170374', 'origin': 'AR01'},
 {'destination': '0319399037', 'origin': 'AR01'},
 {'destination': '0350939840', 'origin': 'AR01'},
 {'destination': '0312087944', 'origin': 'AR01'},
 {'destination': '9054843126', 'origin': 'AR01'}]

the following list would be: a-A b-B c-C

[{'destination': '331247128', 'origin': '00001'},
 {'destination': '354298982', 'origin': '00002'},
 {'destination': '354299148', 'origin': '00003'}]

the following list would be: a-A b-A

[{'destination': 'DEDUS', 'origin': 'DEMAL'},
 {'destination': 'DEDUS', 'origin': 'SP_SITE-MARL'}]

I was trying with some for loops and creating lists with the letters, but it has not worked for me.

Any idea how I could do this representation of the values?

CodePudding user response:

I interpreted 3 answers from this:

Introduction

All work regardless of list length

I believe the use of this is to have a dictionary without repeat values? (answer 3) but you use the term list a lot so it might be for readability to use? (answer 2). An individual dict for each? (Answer 1)

Is list 3 meant to be a-A, b-B not a-A, b-A?

lists is both lists together

    list1 = [{'destination': '0390170374', 'origin': 'AR01'},
            {'destination': '0319399037', 'origin': 'AR01'},
            {'destination': '0350939840', 'origin': 'AR01'},
            {'destination': '0312087944', 'origin': 'AR01'},
            {'destination': '9054843126', 'origin': 'AR01'}]
    list2 = [{'destination': '331247128', 'origin': '00001'},
            {'destination': '354298982', 'origin': '00002'},
            {'destination': '354299148', 'origin': '00003'}]
    lists = [list1,list2]

Answer 1

Changing the dictionary names to a, A.

example

{'a': '0390170374', 'A': 'AR01'}
{'b': '0319399037', 'B': 'AR01'}

code

def ansr1(lists):
    letterlist = ['a', 'b', 'c', 'd','e','f','g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't','u','v','w','x','y','z']
    for list in lists:
        letter = 0
        amountofletters = 1
        for dict in list:
            if letter == 26:
                amountofletters  = 1
                letter = 0
            else:
                print({str(letterlist[letter]*amountofletters): dict['destination'], str(letterlist[letter].capitalize()*amountofletters): dict['origin']})
                letter  = 1

Answer 2

Getting dictionary values and formatting them into a string, then appending to a list.

example

['0390170374-AR01', '0319399037-AR01', '0350939840-AR01', '0312087944-AR01', '9054843126-AR01']

code

def ansr2(lists):
    for list in lists:
        templist = []
        for dict in list:
            templist.append(f"{dict['destination']}-{dict['origin']}")
        else:
            print(templist)

Answer 3

Changing the dictionary names to a, A, then appending to a dict

example

{'a': '0390170374', 'A': 'AR01', 'b': '0319399037', 'B': 'AR01', 'c': '0350939840', 'C': 'AR01', 'd': '0312087944', 'D': 'AR01', 'e': '9054843126', 'E': 'AR01'}

code

def ansr3(lists):
    letterlist = ['a', 'b', 'c', 'd','e','f','g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't','u','v','w','x','y','z']
    for list in lists:
        letter = 0
        amountofletters = 1
        tempdict = {}
        for dict in list:
            if letter == 26:
                amountofletters  = 1
                letter = 0
            else:
                tempdict[str(letterlist[letter]*amountofletters)] = (str(dict['destination']))
                tempdict[str(letterlist[letter].capitalize()*amountofletters)] = (str(dict['origin']))
                letter  = 1
        else:
            print(tempdict)

CodePudding user response:

What you need is simply something that picks the next available value if not found. Assuming you have less than 26 distinct origins and destinations, this would do it:

li = [{'destination': '0390170374', 'origin': 'AR01'},
 {'destination': '0319399037', 'origin': 'AR01'},
 {'destination': '0350939840', 'origin': 'AR01'},
 {'destination': '0312087944', 'origin': 'AR01'},
 {'destination': '9054843126', 'origin': 'AR01'}]

import string
ori,dst = {},{}

def get_sub(di, value, possibilities):
    res = di.get(value)
    if not res:
        res = di[value] = possibilities[len(di)]
    return res

def fmt(pair):
    return f"{get_sub(ori,pair['origin'],string.ascii_lowercase)}-{get_sub(dst,pair['destination'],string.ascii_uppercase)}"

li2 = [fmt(pair) for pair in li]
print(li2)

output:

['a-A', 'a-B', 'a-C', 'a-D', 'a-E']

That said, while intriguing, this seems of limited use. A client program would not "know" what the encoded pairs corresponded to without having access to the 2 substitution dictionaries. And you still have that 26 distinct value limit, unless you build something even more complicated. Finally, your shorthand notation is not stable and what say b-C means will vary from run to run and even depends on the order of the incoming data.

  • Related