[英]How to combine two for loops python
所以我有這兩個 for 循環
for pokemon in pokemonList:
for key in pokemon:
print(key)
break
for files in os.walk("./sprites/"):
for filename in files:
print(filename)
我正在嘗試使用第一個 pokemonList.pokemon 中的值來重命名 sprites 文件夾中的文件。
在 Javascript id 中做類似的事情
for(i=0;i<pokemonList.length;i++){loop through both using i here}
但我不確定如何在 python 中做到這一點。 先感謝您。
口袋妖怪列表:
[{'Milcery'}, {'Alcremie'}, {'Alcremie'}, {'Alcremie'}, {'Alcremie'}, {'Alcremie'}, {'Alcremie'}, {'Alcremie'}, {'Alcremie'}, {'Alcremie'}, {'Falinks'}, {'Pincurchin'}, {'Snom'}, {'Frosmoth'}, {'Stonjourner'}, {'Eiscue'}, {'Eiscue'}, {'Indeedee'}, {'Indeedee'}, {'Morpeko'}, {'Morpeko'}, {'Cufant'}, {'Copperajah'}, {'Dracozolt'}, {'Arctozolt'}, {'Dracovish'}, {'Arctovish'}, {'Duraludon'}, {'Dreepy'}, {'Drakloak'}, {'Dragapult'}, {'Zacian'}, {'Zacian'}, {'Zamazenta'}, {'Zamazenta'}, {'Eternatus'}, {'Eternatus'}]
您也可以使用索引:
for i in range(len(pokemonList)):
# do whatever you want with the i index
通過用方括號索引列表並使用 len 函數來獲取列表的長度,可以在 python 中做同樣的事情:
for i in range (0, len(pokemonList)):
print(pokemonList[i])
在python中列表索引從0開始。
如果“口袋妖怪”和“文件”的范圍相同,您可以將循環設置為
for pokemon,files in zip(pokemonList, os.walk("./sprites/")):
print(pokemon, files)
# do other stuff
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.