简体   繁体   English

无法添加到空白列表

[英]Unable to add to blank list

I am working on a program that can be used to add a given value say a car dealership with the cars it owns:我正在开发一个程序,该程序可用于增加给定价值,例如汽车经销商及其拥有的汽车:

for example例如

car_storage = []

def add_cars(dealership, car):
        for items in car_storage:
            for values in items:
                if dealership in items:

                   #Adds car whether it exists or not in list
                   items.append(car)
                   return
                    
                #If the dealership does not exist it is created and added to the list
                else:

                    items.append([dealership, car])
                    return

add_cars("Manchester", "Mini")
add_cars("London", "Toyota")
add_cars("London", "BMW")
add_cars("London", "BMW")

#Desired output
[["Nottingham", "Audi"],["Manchester","Mini"],["London", "Lorry", "BMW", "BMW"]]

However, my code never adds to the list, where am I going wrong?但是,我的代码从未添加到列表中,我哪里出错了?

The issue with your code as @juanpa.arrivillaga said is that the before you add the first item the list car_dealership is empty so nothing in your function executes. @juanpa.arrivillaga 所说的代码的问题是,在添加第一项之前,列表 car_dealership 是空的,因此 function 中的任何内容都不会执行。

Your approach however is almost correct, Python supports else keyword with for loops.但是,您的方法几乎是正确的, Python 支持 else 关键字和 for 循环。 What it means is, if the for loop ran without breaking (no break statements) the else statement is called.这意味着,如果 for 循环运行时没有中断(没有 break 语句),则调用 else 语句。 For example例如

for i in range(1, 4):
    if i==2:
        break
else: # Not executed as there is a break
      # if we remove the break the else will run
    print("No Break")

Now similarly in your code.现在在您的代码中类似。 you can use the same concept.您可以使用相同的概念。

def add_cars(dealership, car):
        for items in car_storage:
            for values in items:
                if dealership in items:

                   #Adds car whether it exists or not in list
                   items.append(car)
                   return
                    
        #If the dealership does not exist it is created and added to the list
        else:
            car_storage.append([dealership, car])

Which is very similar to your original code except the indentation level on the else part.除了else部分的缩进级别之外,这与您的原始代码非常相似。

Now in this case since you're returning and not breaking you can even get away with removing the else entirely since the function will never reach there现在在这种情况下,由于您正在返回而不是破坏,您甚至可以完全移除 else ,因为 function 永远不会到达那里

def add_cars(dealership, car):
        for items in car_storage:
            for values in items:
                if dealership in items:

                   #Adds car whether it exists or not in list
                   items.append(car)
                   return
                    
        #If the dealership does not exist it is created and added to the list
        car_storage.append([dealership, car])

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

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