Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我想将字典键从字符串更改为整数_Python_Dictionary - Fatal编程技术网

Python 我想将字典键从字符串更改为整数

Python 我想将字典键从字符串更改为整数,python,dictionary,Python,Dictionary,数据变量包括整数作为键,另一个字典作为值。但是,0在被视为字符串的引号中。我想把它转换成整数 data[keys].keys() dict_keys(['0', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 修字典的简单方法是 d[0] = d['0'] del d['0'] 但是,这里有一个方便的函数,可用于将所有字符int键转换为int: def convert_dict(d): keys=列表(d.keys()) 对于键入键: 如果isinstance(键,str):

数据变量包括整数作为键,另一个字典作为值。但是,0在被视为字符串的引号中。我想把它转换成整数

data[keys].keys()
dict_keys(['0', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

修字典的简单方法是

d[0] = d['0']
del d['0']
但是,这里有一个方便的函数,可用于将所有字符int键转换为int:

def convert_dict(d):
keys=列表(d.keys())
对于键入键:
如果isinstance(键,str):
尝试:
int_key=int(key)
d[int_key]=d[key]
德尔d[钥匙]
除值错误外:
通过
返回d
试验

d={'0':0,1:1,2:2,'hello':'world'}
转换指令(d)
{1:1,2:2,‘你好’:‘世界’,0:0}

试试这个:
data[0]=data['0']
然后
deldata['0']