【问题标题】:Python nested for loopsPython 嵌套 for 循环
【发布时间】:2015-11-22 15:30:07
【问题描述】:
car_list=[['BMW',4,False,200],['Mercedes',3,False,250],['Renault',2,False,150],['Audi',3,False,180]] #car list

new_list=[] 
def add_new_car(horsepower,brand,car_list):
    for lists in car_list:
        for i in lists:
            if brand==i :
                lists[1]=lists[1]+1
                print(car_list)

            else: #from there i dont want to execute program when brand is mercedes. if brand is different from list i want to execute . but program executes.:$$
                if brand != i:
                    new_list.append(brand)
                    new_list.append(1)
                    new_list.append(True)
                    new_list.append(horsepower)
                    car_list.append(new_list)
                    print(car_list)
                    break
        break

brand='Mercedes' #when brand is mercedes program executes else if part and creates new list and add this lst to car list. but i cant add it to existed mercedes list as> mercedes,3,false,250 
horsepower=170

add_new_car(horsepower,brand,car_list)

我的代码没有按我希望的那样工作。当品牌是梅赛德斯时,我无法执行此操作,它会执行 else if 部分并创建新列表。但我想将其添加到现有的梅赛德斯列表中。并且新列表将是

[['BMW',4,False,200],['Mercedes',4,False,250],['Renault',2,False,150],['Audi',3,False,180]]

不是

[['BMW', 4, False, 200], ['Mercedes', 3, False, 250], ['Renault', 2, False, 150], ['Audi', 3, False, 180], ['Mercedes', 1, True, 170]]

我能为它做些什么?

【问题讨论】:

  • 不清楚你想要的输出是什么。该程序是如何达到您发布的输出的?为什么BMW 值更改为3?请发布该代码所需的最终输出。另外,品牌总是Mercedes 吗?当我通过'BMW' 时会发生什么?
  • 对不起,我弄错了.. bmw list 与初始相同.. 此代码的最终输出 [['BMW', 4, False, 200], ['Mercedes', 3, False , 250], ['Renault', 2, False, 150], ['Audi', 3, False, 180], ['Opel', 1, True, 170]] 当品牌是 Opel.. 但当品牌是 mercedes 它是 [['BMW', 4, False, 200], ['Mercedes', 3, False, 250], ['Renault', 2, False, 150], ['Audi', 3, False , 180], ['Mercedes', 1, True, 170]].. 但是它必须是 [['BMW',4,False,200],['Mercedes',4,False,250],['Renault ',2,False,150],['奥迪',3,False,180]]

标签: python list loops for-loop nested


【解决方案1】:

此代码适用于您 İlayda。您不需要添加内部 for 循环并将 else 放在 for 循环后保证如果没有匹配项则添加新项目。

car_list = [['BMW', 4, False, 200], ['Mercedes', 3, False, 250], ['Renault', 2, False, 150], ['Audi', 3, False, 180]]

def add_new_car(horsepower, brand, car_list):
    for lists in car_list:
        if lists[0] == brand:
            lists[1] += 1
            break
    else:
        new_list = []
        new_list.append(brand)
        new_list.append(1)
        new_list.append(True)
        new_list.append(horsepower)
        car_list.append(new_list)

brand = 'Mercedes'
horsepower = 170

add_new_car(horsepower, brand, car_list)
print car_list

【讨论】:

    【解决方案2】:

    您不需要遍历内部列表。那个循环就是原因,你要为每个输入使用else if 部分。只需使用lists[0] 索引值检查brand 值。

    我想下面的函数应该足够了。要将brand 添加到列表中(如果它尚不存在),您可以使用for 循环的else 部分,该循环在没有break 的情况下完成时执行。

    def add_new_car(horsepower,brand,car_list):
        for lists in car_list:
            if lists[0] == brand:
                lists[1] = lists[1]+1
                break
        else:
            cart_list.append([brand, 1, True, horsepower])
    

    【讨论】:

    • 哦,是的..这真的很有意义..谢谢Rohit :)
    猜你喜欢
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 2017-06-05
    • 2019-04-15
    • 2020-10-09
    • 2017-08-27
    • 2017-11-11
    相关资源
    最近更新 更多