how to parse a string into a dictionary

后端 未结 5 1827
伪装坚强ぢ
伪装坚强ぢ 2020-12-21 12:39

I am trying to parse a string to separate the lists within the string. I currently have the string:

string = \"[[\'q1\', \'0\', \'q1\'], [\'q1\', \'1\', \'q2         


        
相关标签:
5条回答
  • 2020-12-21 13:16

    You can use JSON but the string format has to be a dict and you cannot have 2 times the same key:

    import json
    
    string ='{"q": ["0", "q1"], "q1": ["1", "q2"], "q3": ["1", "q1"], "q2": ["0", "q2"]}'
    
    dict = json.loads(string)
    
    print dict
    Output: {'q': ['0', 'q1'], 'q1': ['1', 'q2'], 'q3': ['1', 'q1'], 'q2': ['0', 'q2']}
    
    0 讨论(0)
  • 2020-12-21 13:26

    try this

    >>> import ast
    >>> ast.literal_eval(string)
    [['q1', '0', 'q1'], ['q1', '1', 'q2'], ['q2', '0', 'q2'], ['q2', '1', 'q1']]
    >>> list=ast.literal_eval(string)
    >>> d={}
    >>> for l in list:
    ...     d[l[0]]=tuple(l[1:])
    >>> d
    {'q1': ('1', 'q2'), 'q2': ('1', 'q1')}
    

    key is always unique in dict thats why in result showing updated key value pair

    0 讨论(0)
  • 2020-12-21 13:27

    To keep the duplicates and conform the input format:

    import collections
    import json
    string = "[['q1', '0', 'q1'], ['q1', '1', 'q2'], ['q2', '0', 'q2'], ['q2', '1', 'q1']]"
    d = collections.defaultdict(list)
    for (k, v1, v2) in json.loads(string.replace("'",'"')):
        d[k].append((v1, v2))
    

    With eval (if you trust your input):

    import collections
    string = "[['q1', '0', 'q1'], ['q1', '1', 'q2'], ['q2', '0', 'q2'], ['q2', '1', 'q1']]"
    d = collections.defaultdict(list)
    for (k, v1, v2) in eval(string):
        d[k].append((v1, v2))
    

    Contents of d:

    defaultdict(<type 'list'>, {
        'q1': [('0', 'q1'), ('1', 'q2')],
        'q2': [('0', 'q2'), ('1', 'q1')]
    })
    

    EDIT: and with no library at all.

    string = "[['q1', '0', 'q1'], ['q1', '1', 'q2'], ['q2', '0', 'q2'], ['q2', '1', 'q1']]"
    d = {}
    for (k, v1, v2) in eval(string):
        d.setdefault(k, []).append((v1, v2))
    

    I'm unable to make it a one-liner though :-)

    0 讨论(0)
  • 2020-12-21 13:28

    Insted of dictionary you can have list:
    you can use ast.literal_eval to parse python data structure from string

    >>> import ast
    >>> my_string = "[['q1', '0', 'q1'], ['q1', '1', 'q2'], ['q2', '0', 'q2'], ['q2', '1', 'q1']]"
    >>> k = ast.literal_eval(my_string)
    >>> k
    [['q1', '0', 'q1'], ['q1', '1', 'q2'], ['q2', '0', 'q2'], ['q2', '1', 'q1']]
    >>> [[x[0],tuple(x[1:])] for x in k]
    [['q1', ('0', 'q1')], ['q1', ('1', 'q2')], ['q2', ('0', 'q2')], ['q2', ('1', 'q1')]]
    
    0 讨论(0)
  • 2020-12-21 13:28

    You could get nested dictionaries from your string (a little less straightforward than the previous answers). Thanks to Alex Martelli for his answer to Update value of a nested dictionary of varying depth

    import ast
    
    def update(d, u):
        for k, v in u.iteritems():
            if isinstance(v, dict):
                r = update(d.get(k, {}), v)
                d[k] = r
            else:
                d[k] = u[k]
        return d
    
    def listToDict(l):
        temp = l[-1]
        for value in l[-2::-1]:
            temp = {value: temp}
        return temp
    
    input = "[['q1', '0', 'q1'], ['q1', '1', 'q2'], ['q2', '0', 'q2'], ['q2', '1', 'q1']]"
    
    res = {}
    for l in ast.literal_eval(input):
        update(res ,listToDict(l))
    print res
    

    Output:

    {'q1': {'1': 'q2', '0': 'q1'}, 'q2': {'1': 'q1', '0': 'q2'}}
    
    0 讨论(0)
提交回复
热议问题