在Python中迭代列表的问题 [英] Issues iterating through lists in Python

查看:61
本文介绍了在Python中迭代列表的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决一个简单的问题:



 userInput = input( 按空格分隔的列表元素:
nums = userInput.split()
for x in list(nums):
y = int(x)* 3
z = list(str(y))
3 in z:
z.remove( 3
else
print (z)



使用上面的代码;从逻辑上讲,应该从列表中删除所有出现的3。然而,这并不是所希望的。有人可以解释为什么&如何修复?



我想将用户输入的各个元素乘以3,然后从列表中删除所有3个实例并打印输出。



我也尝试过True并使用i + = 1计数和东西,但似乎无法让这个工作或我的头围绕它。感谢帮助。



输入 - 所需输出 - 当前输出

1 2 3 - 6 9 - 9
0 - 0 - 0
2 6 11 - 6 18 - []



我发现我应该使用的是列表理解。所以我做了一些研究,并且能够让它们发挥作用,但仍然没有达到预期的效果:



 p =输入( 按空格分隔的列表元素:
nums = p.split()
mp = [num num in nums * 3 ]
print (mp)
x 列表中的class =code-keyword>(nums):
y = int(x)* 3
z = str( y)
print (z)
c = [z]
filtered = [chr for chr in c if c!= 3]
print (已过滤)
for
n in list(z):
negate = [string for string in n if n!= 3]
print (否定)



输出保持不变。我不明白为什么以及如何解决这个问题?



为什么扫描列表中的字符串并将其从列表中删除是如此困难?! ? :(



真实地感谢帮助,因为我感觉像是面向桌面的手掌哭泣哈哈:P



我尝试过:



Stackoverflow,youtube,python文档。

解决方案

 userInput = input( 按空格分隔的列表元素:
nums = userInput.split()
for x in list (nums):
y = int(x)* 3
if y! = 3
print (y,end = ' '
print





或使用清单理解

 userInput = input( 按空格分隔的列表元素:
nums = userInput.split()
products = [int(x)* 3 for x nums if x!= 1]
print (产品)


I'm trying to solve what should be an easy problem:

userInput = input("list element seperated by space: ")
nums = userInput.split()
for x in list(nums):
    y = int(x) * 3
    z = list(str(y))
while "3" in z:
    z.remove("3")
else:
    print(z)


With the above code; logically, all occurrences of "3" should be removed from the list. This is not functioning as desired however. Could someone explain why & how to fix?

I want to multiply individual elements of user input by 3 and then remove all instances of 3 from the list and print off the output.

I've also tried while True and using i += 1 counts and stuff but just can't seem to get this working or my head round it. Appreciate the help.

input   - desired output - currently outputting

1 2 3   - 6 9            - 9
0       - 0              - 0
2 6 11  - 6 18           - []


I found out that what I should be using is list comprehension. So I did some research on that and have been able to get them to function, but still not as desired:

p = input("list element seperated by space: ")
nums = p.split()
mp = [num for num in nums * 3]
print(mp)
for x in list(nums):
    y = int(x) * 3
    z = str(y)
    print(z)
    c = [z]
filtered = [chr for chr in c if c != "3"]
print(filtered)
for n in list(z):
    negate = [string for string in n if n != "3"]
    print(negate)


The output remains the same. I don't understand why and how I can get around this?

Why is it so hard to scan a list for a string and remove it from the list?!? :(

Truthfully appreciate the help as I feel like desk-facing-palming-crying haha :P

What I have tried:

Stackoverflow, youtube, python documentation.

解决方案

userInput = input("list element seperated by space: ")
nums = userInput.split()
for x in list(nums):
    y = int(x) * 3
    if y != 3:
        print(y, end=' ')
print("")



or using a list comprehension

userInput = input("list element seperated by space: ")
nums = userInput.split()
products = [int(x) * 3 for x in nums if x != "1"]
print(products)


这篇关于在Python中迭代列表的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆