擅长:python、mysql、java
<pre><code>simpleArray = [ 2, 54, 32 ]
simpleDict = dict()
for index,item in enumerate(simpleArray):
simpleDict["item{0}".format(index)] = item
print(simpleDict)
</code></pre>
<p>好的,第一行是输入,第二行是空字典。我们会马上把它装满。在</p>
<p>现在我们需要迭代,但是在C中的普通迭代被认为是非python的。Enumerate将从数组中给出索引和所需的项。看这个:<a href="https://stackoverflow.com/questions/522563/accessing-the-index-in-python-for-loops">Accessing the index in Python 'for' loops</a>。在</p>
<p>因此,在每次迭代中,我们将从数组中获取一个项,并在字典中插入括号中字符串中的键。我正在使用格式,因为不鼓励使用%。请看这里:<a href="https://stackoverflow.com/questions/5082452/python-string-formatting-vs-format">Python string formatting: % vs. .format</a>。在</p>
<p>最后我们将打印。使用打印功能以获得更多兼容性。在</p>