Convert string key to int in dictionary

My question is very similar to this , except that I have a dictionary of lists, and I'm interested in changing both the key value and all the elements in each list of the string form to int .

So, for example, I need a dictionary:

 { '1':['1', '2', '3', '4'] , '2':['1', '4'] , '3':['43','176'] } 

to become:

 { 1:[1, 2, 3, 4] , 2:[1, 4] , 3:[43,176] } 

Is it possible?

In general, since I created this dictionary from a JSON file

{"0": ["1", "2", "3", "4"], "1": ["0", "2", "3", "4", "27", "94 "," 9 "," 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 "]}

with the following instructions:

 json_data = open("coauthorshipGraph.txt") coautorshipDictionary = json.load( json_data ) json_data.close() 

Is there a way to do this directly at boot time?

+13
json python dictionary
Jan 17 '14 at 18:56
source share
3 answers
 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)} 
+24
Jan 17 '14 at
source share

This solution will work if you have iterability as your value, as in the provided json.

 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]} 

In the second part of the question, you can use the understanding of line by line when reading a file. However, he is confused as hell.

 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 gives the same result as above.

+2
Jan 17 '14 at 19:02
source share

As in the Decency answer, but using 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 nested dicts, the loader will process each nested dict on its own, as it loads data without having to write code to go through your dict result. You can also add checks for cases where the values ​​in the lists are not numeric strings and the lists themselves also contain dicts if your JSON structure becomes more complex and if your data will only have lists as values ​​for your top level dict you can delete part if isinstance(v, list) else v .

+2
Jan 17 '14 at 19:16
source share



All Articles