小编yop*_*gyo的帖子

如何使用 while 循环消除空字符串?(Python)

list1 = ["apple", "pear", "", "strawberry", "orange", "grapes", "", "watermelon"]
list2 = []
x = 0
while x < len(list1):
    if len(list1[x]) > 0:
        list2.append(list1[x])
print(list2)
Run Code Online (Sandbox Code Playgroud)

我尝试运行这段代码,但似乎不起作用。我应该如何不使用列表理解或其他方法来修改此代码?

python string list while-loop

2
推荐指数
1
解决办法
121
查看次数

标签 统计

list ×1

python ×1

string ×1

while-loop ×1