friends = ['Masum','Pavel','Sohag']
print(friends[1]) # this one gives me the result 'Pavel'
for friends in friends:
print('Happy new yers,', friends)
print(friends[1]) # Why this one give me the result o
0
A
回答
0
因爲你使用名字朋友的名單和字符串,所以你的變量朋友從['Masum','Pavel','Sohag']更改爲「Sohag」。
要糾正這只是更改了到: 的朋友的朋友
0
嘗試friend in friends
。你有點覆蓋friends
與同名的迭代器。
0
不要使用相同的變量名的列表迭代:
friends = ['Masum','Pavel','Sohag']
for friend in friends:
print('Happy new yers,', friend)
# At this point friend will be the last one while friends will still be the list you assigned
1
當你寫:
for friends in friends:
你重新分配標籤friends
在這個項目陣列。 循環完成後,該數組沒有任何名稱,因此丟失。但是,標籤friends
將存儲該數組的最後一個值。 例如(->
手段「指向」)
Before iteration: friends -> array
Ist iteration: friends -> 'Masum'
2nd iteration: friends -> 'Pavel'
3rd iteration and after loop completion: friends -> 'Sohag'
注意,只有一個變量現在具有值‘索哈傑’。其他每個變量/數組都會丟失。
相關問題
- 1. Python:爲什麼我得到不同的排序結果
- 2. 爲什麼我得到相同的結果不同的列
- 3. 爲什麼使用withMutations會得到不同的結果?
- 4. 爲什麼python請求獲得與Ajax不同的結果
- 5. 爲什麼list()與對象分別顯示不同的結果?
- 6. 爲什麼NULL會得到不同的結果,無?
- 7. 爲什麼我在這裏得到不同的結果?
- 8. 爲什麼string.matches和pattern.matches會得到不同的結果?
- 9. 爲什麼得到不同的結果strsplit功能
- 10. 爲什麼我得到.NET4不同的結果相比,.NET 3.5
- 11. 爲什麼我得到不同的結果
- 12. 爲什麼不同的測試順序會得到不同的結果?
- 13. 爲什麼不同的變量名稱會得到不同的結果(python2.7)?
- 14. 爲什麼我在同一個設計中得到兩個不同的結果?
- 15. 爲什麼我會得到lmList vs lmList的不同總結結果[[x]]
- 16. 爲什麼我用相同的HtmlDecode()函數得到不同的結果?
- 17. 爲什麼使用相同的數據會得到不同的結果?
- 18. 爲什麼我得到這個結果?
- 19. 爲什麼我會得到空結果?
- 20. 爲什麼lookupsymbols api的結果不同?
- 21. 爲什麼我會得到相同的結果?
- 22. 爲什麼我從jsonp得到相同的報價結果
- 23. 爲什麼我通過jquery + ajax得到相同的結果?
- 24. 爲什麼我會得到同一個字符串的多個結果,以及它們爲什麼不同
- 25. 爲什麼jQuery.height()在Chrome上獲得不同的結果?
- 26. 爲什麼webkit通過capybara獲得與CasperJS不同的結果?
- 27. 爲什麼我得到的結果$結果
- 28. 爲什麼re.findall()給我的結果不同於Python中的re.finditer()?
- 29. 爲什麼re2的結果與Python中的re模塊不同?
- 30. 爲什麼這兩個python函數返回不同的結果?
因爲你已經使用循環變量'friends'遮蓋了列表'friends' - 你正在獲得''Sohag'[0]'。嘗試'在朋友的朋友:''而不是。 – jonrsharpe