如何在python中将数组转换为dict

cit*_*ity 3 python

现在,我想将数组转换为这样的字典:

dict = {'item0': arr[0], 'item1': arr[1], 'item2': arr[2]...}
Run Code Online (Sandbox Code Playgroud)

如何在python中优雅地解决这个问题?

DSM*_*DSM 11

你可以使用enumerate和字典理解:

>>> arr = ["aa", "bb", "cc"]
>>> {'item{}'.format(i): x for i,x in enumerate(arr)}
{'item2': 'cc', 'item0': 'aa', 'item1': 'bb'}
Run Code Online (Sandbox Code Playgroud)