简体   繁体   English

用带有 python 列表的 for 语句替换 while 循环

[英]Replacing a while loop with a for statement with python list

I have to create a list of cars, a function to display the cars but replace the fourth car with a phrase, and call the function, for an assignment.我必须创建一个汽车列表 function 来显示汽车,但用一个短语替换第四辆汽车,然后调用 function 进行分配。 One of the parts of the grading scheme is to use a for statement to iterate, not a while loop.评分方案的一部分是使用 for 语句进行迭代,而不是使用 while 循环。 It also says that it can be done in under 10 lines of code.它还说它可以用不到 10 行代码来完成。

I have created a while loop that does prints the proper list, but I cannot manage to change it to a for statement successfully.我创建了一个 while 循环来打印正确的列表,但我无法成功地将其更改为 for 语句。

def my_Assign3_challenge(cars):
  i=0
  while i<len(cars):
    print(cars[i])
    i=i+1
    if i==3:
      print("I would never buy that!")
      i=i+1
cars=["toyota","mitsubishi","dodge","ford","mini"]
my_Assign3_challenge(cars)

This for statement just prints the list, without the change:这个 for 语句只打印列表,没有改变:

def my_Assign3_challenge(cars):
  for x in cars:
    if x!= 3:
      print(x)
    else:
      print("I would never buy that!")
cars=["toyota","mitsubishi","dodge","ford","mini"]
my_Assign3_challenge(cars)

I also managed a couple times to have just the first list item repeat indefinitely, I can't find the code that did that now, though.我还设法让第一个列表项无限期地重复,但我现在找不到这样做的代码。

I would greatly appreciate if anyone could take a look at this and tell me where I went wrong.如果有人能看看这个并告诉我哪里出错了,我将不胜感激。 I'm sure it's just a simple mistake, as I am very new to coding, but I would love your help!我敢肯定这只是一个简单的错误,因为我对编码很陌生,但我很想得到你的帮助!

Your while loop iterates through the list indices...您的while循环遍历列表索引...

while i<len(cars):
  print(cars[i])

... but you changed your referencing with the for loop: you now reference the elements themselves, the brand names: ...但是您使用for循环更改了引用:您现在引用了元素本身,品牌名称:

for x in cars:

but you still treat x as if it were an integer index.但是您仍然将x视为 integer 索引。 Since x is now a sequence of brand names, it will never equal 3 .由于x现在是一个品牌名称序列,它永远不会等于3 You need to make up your mind which way you're going to refer to the list elements, and make your second set of code use that way consistently.您需要下定决心以哪种方式引用列表元素,并使您的第二组代码始终如一地使用该方式。

The for loop in the function: function 中的 for 循环:

def my_Assign3_challenge(cars):
  for x in cars:
    if x!= 3:
      print(x)
    else:
      print("I would never buy that!")
cars=["toyota","mitsubishi","dodge","ford","mini"]
my_Assign3_challenge(cars)

Outputs输出

toyota
mitsubishi
dodge
ford
mini

You need to get the index using .index()您需要使用.index()获取索引

The code would be:代码将是:

def my_Assign3_challenge(cars):
  for x in cars:
    if cars.index(x) != 3:
      print(x)
    else:
      print("I would never buy that!")
cars=["toyota","mitsubishi","dodge","ford","mini"]
my_Assign3_challenge(cars)

I can do it in 3 lines:我可以用 3 行来完成:

challenge = lambda x : [v if i !=3 else "I would never buy that!" for i, v in enumerate(x)]
cars=["toyota","mitsubishi","dodge","ford","mini"]
challenge(cars)

output: output:

Out[54]: ['toyota', 'mitsubishi', 'dodge', 'I would never buy that!', 'mini']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM