首页 > 解决方案 > 如何根据类型分隔列表值 - Python

问题描述

从包含整数、字符串和浮点数的列表中,创建三个列表以分别存储它们。

请帮助我如何根据类型分隔列表值。

标签: pythonpython-3.xlist

解决方案


您可以使用类型。

test = [3, "hello", 2.4]

int_array = []
string_array = []
float_array = []

for value in test:
    check = type(value)
    if check == int:
        int_array.append(value)
    elif check == str:
        string_array.append(value)
    elif check == float:
        float_array.append(value)

推荐阅读