如何更改嵌套列表中值的变量类型

How to change the variable type for values inside nested lists

我想尽可能更改字符串的变量类型。 ('6'会转成6,'7.8'会转成7.8,'true'会转成True,'null'或空字符串会转成None)

到目前为止,这是我的代码:

def change_variable_type(variables: List[List[str]]) -> None:
   for sublist in variables: #Access elements inside nested list
      for element in sublist:
         if element.isnumeric(): #Checks if it's a number and converts to a float
            element = float(element)

            if element == int(element): #Checks if it can be an int, and converts if it can
               element = int(element)

         elif element.lower() == 'false': #Checks if it can be False, and converts if it can
            element = False

         elif element.lower() == 'true': #Checks if it can be True, and converts if it can
            element = True

         elif element == 'null' or element == '': #Checks if it can be None, and converts if it can
            element = None

variables_to_convert = [['abc', '123', '123.4', 'true', 'False', '']]
change_variable_type(variables_to_convert)

编码后是运行,variables_to_convert应该是[['abc', 123, 123.4, True, False, None]],不过,我只是再次获取原始值。我是不是漏掉了连初学者都能看出来的东西,或者这是逻辑错误?

PS 对于认为我的代码混乱或无组织的人,我深表歉意。这只是我必须做的一个小功能,所以我没有花太多时间让它看起来不错。提前致谢!

您可以将变量重新分配回子列表:

def change_type(element):
     if element.isnumeric(): #Checks if it's a number and converts to a float
        element = float(element)

        if element == int(element): #Checks if it can be an int, and converts if it can
           element = int(element)
        return element

     elif element.lower() == 'false': #Checks if it can be False, and converts if it can
        return False

     elif element.lower() == 'true': #Checks if it can be True, and converts if it can
        return True

     elif element == 'null' or element == '': #Checks if it can be None, and converts if it can
        return None
     return element
    
def change_variable_type(variables):
    for sublist in variables:
        for index, element in enumerate(sublist):
            sublist[index] = change_type(element)

variables_to_convert = [['abc', '123', '123.4', 'true', 'False', '']]
change_variable_type(variables_to_convert)
print(variables_to_convert)

按要求输出

在循环中,变量不直接引用列表,因此要更改任何值,您需要通过索引引用原始列表,为此您可以使用 enumerate

from typing import List


def change_variable_type(variables: List[List[str]]) -> None:
    for sublist in variables:
        for index, element in enumerate(sublist):
            if element:
                if element.isnumeric():
                    sublist[index] = int(element)
                elif element.replace('.', '').isnumeric():
                    sublist[index] = float(element)
                elif element.lower() == 'true':
                    sublist[index] = True
                elif element.lower() == 'false':
                    sublist[index] = False
            else:
                sublist[index] = None
    return variables


variables_to_convert = [['abc', '123', '123.4', 'true', 'False', '']]
change_variable_type(variables_to_convert)
print(variables_to_convert)

输出:

[['abc', 123, 123.4, True, False, None]]