首页 > 解决方案 > 仅更新嵌套循环中的整数

问题描述

swim=[['Cycle Shoes+Goggles', 1.25], 
      ['Cycle shoes+Sunglasses', 1], 
      ['Run shoes+Goggles', 1.33]]

swim_s = 50
    
def update1(lst1):
    for i in lst1:
        i[1]*=swim_s
    
update1(swim)

目前,该程序只会将第一个整数乘以 50,但我将如何对列表中的每个整数执行此操作(完整列表将有超过 10 个整数)

标签: python

解决方案


Check if it's an instance of int using isinstance:

def update(lst):
    for ele in lst1:
        for i in range(len(ele)):
            if isinstance(ele[i], int):
                ele[i] *= 50

推荐阅读