for循环是否在Python中清空列表?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了for循环是否在Python中清空列表?相关的知识,希望对你有一定的参考价值。
如何在第二和第三部分中获取列表中的项目作为输出? (每次我要打印它们时,它们只是从列表中的项目中打印字母。)
foods = ["pizza", "burger", "popcorn", "cannoli", "noodles"]
print("The foods I like are:
")
for foods in foods[:3]:
print(foods.title())
#2nd
print("The foods my friend likes are:
")
for foods in foods[1:4]:
print(foods.title())
#3rd
print("The food SHE likes are:
")
for foods in foods[:]:
print(foods.title())
答案
您在每次迭代中都覆盖foods
。请尝试以下方法:
foods = ["pizza", "burger", "popcorn", "cannoli", "noodles"]
#2nd
print("The foods my friend likes are:
")
for f in foods[1:4]:
print(f.title())
#3rd
print("The food SHE likes are:
")
for f in foods[:]:
print(f.title())
以上是关于for循环是否在Python中清空列表?的主要内容,如果未能解决你的问题,请参考以下文章
python中的while循环与for循环怎么样那个比较好用?
如何使用引导程序和 for 循环在 django 中创建电影片段?