Convert a string key to int in a Dictionary

前端 未结 3 1966
我寻月下人不归
我寻月下人不归 2020-12-08 06:55

My question is very similar to this one, except I have a dictionary of lists and I\'m interested in changing both the key value and all elements in every list form str

相关标签:
3条回答
  • 2020-12-08 07:08

    This solution will work for the case where you have an iterable as your value, as in the json you provided.

    my_dict = {"0": ["1", "2", "3", "4"], "1": ["0", "2", "3", "4", "27", "94", "95", "97", "128", "217", "218", "317"], "2": ["0", "1", "3", "4", "94", "95"], "3": ["0", "1", "2", "4", "377"], "4": ["0", "1", "2", "3", "27", "28"], "5": ["6", "7", "8"], "6": ["5", "7", "8"], "7": ["5", "6", "8", "14", "23", "40", "74", "75", "76", "362", "371", "372"], "8": ["5", "6", "7", "66"], "9": ["10", "11", "12"], "10": ["9", "11", "12", "56", "130", "131"]}
    
    output_dict = {}
    for key, value in my_dict.iteritems():
        output_dict[int(key)] = [int(item) for item in value]
    
    output_dict
    

    Output:

    {0: [1, 2, 3, 4],
     1: [0, 2, 3, 4, 27, 94, 95, 97, 128, 217, 218, 317],
     2: [0, 1, 3, 4, 94, 95],
     3: [0, 1, 2, 4, 377],
     4: [0, 1, 2, 3, 27, 28],
     5: [6, 7, 8],
     6: [5, 7, 8],
     7: [5, 6, 8, 14, 23, 40, 74, 75, 76, 362, 371, 372],
     8: [5, 6, 7, 66],
     9: [10, 11, 12],
     10: [9, 11, 12, 56, 130, 131]}
    

    For the second part of the question, you can use a dict comprehension in line as you read the file. It's obfuscated as hell though.

    with open('coauthorshipGraph.txt', 'r') as f:
        json_data = { int(key) : [int(item) for item in value] for key, value in json.load(f).iteritems()}
    
    json_data
    

    This yields the same output as above.

    0 讨论(0)
  • 2020-12-08 07:15
    d = {'1':'145' , '2':'254' , '3':'43'}
    d = {int(k):int(v) for k,v in d.items()}
    >>> d
    {1: 145, 2: 254, 3: 43}
    

    for lists in values

    >>> d = { '1':['1', '2', '3', '4'] , '2':['1', '4'] , '3':['43','176'] }
    >>> d = {int(k):[int(i) for i in v] for k,v in d.items()}
    

    in your case:

    coautorshipDictionary = {int(k):int(v) for k,v in json.load(json_data)}
    

    or

    coautorshipDictionary = {
        int(k):[int(i) for i in v] for k,v in json.load(json_data)}
    
    0 讨论(0)
  • 2020-12-08 07:23

    Similar to Decency's answer, but taking advantage of the object_hook argument:

    coautorshipDictionary = json.load(json_data, object_hook=lambda d: {int(k): [int(i) for i in v] if isinstance(v, list) else v for k, v in d.items()}) # iteritems() for Python 2
    

    The main advantage of this method is that, if you ever end up with any nested dicts, the loader will handle each nested dict on its own as it loads the data without you having to write code to walk through your result dict. You could also add in checks for cases where values in lists are not numeric strings or the lists themselves contain dicts as well, if your JSON structure gets more complicated, and if your data will only have lists as the values for your top-level dict you can remove the if isinstance(v, list) else v part.

    0 讨论(0)
提交回复
热议问题