Python 检查列表是否包含混合类型

Python 检查列表是否包含混合类型,python,Python,我尝试用Python编写一个函数,该函数将检查列表是否包含混合对象类型(例如,包含列表和字符串的列表)。我可以检查列表一次,但无法检查子列表。 如果列表仅包含列表或仅包含字符串,则函数应返回True。 例如: [[“aa”、“bb”、“cc”]、[“aa”]、[]]-将返回True [[“aa”,“bb”],[[“aa”,“bb”],“aa”]-返回False(有一个包含列表和字符串的列表) 检查一次列表: def same_type(lst): # an empty list woul

我尝试用Python编写一个函数,该函数将检查列表是否包含混合对象类型(例如,包含列表和字符串的列表)。我可以检查列表一次,但无法检查子列表。
如果列表仅包含列表或仅包含字符串,则函数应返回True。

例如:

[[“aa”、“bb”、“cc”]、[“aa”]、[]]
-将返回
True

[[“aa”,“bb”],[[“aa”,“bb”],“aa”]
-返回
False
(有一个包含列表和字符串的列表)

检查一次列表:

def same_type(lst):
    # an empty list would return True
    if len(lst) == 0:
        return True
    elif len(lst) == 1:
        return True
    else:
        for l in lst :
            # check if other elements are the same as first
            if type(lst[0]) != type(l):
                return False

        return True
编辑:
最后我做了这件事:
1) 通过递归查找给定列表中的所有列表(子列表),并将它们放在一个大列表中。

2) 迭代那个大列表,检查是否有一个混合数据类型的列表,并使用my
same\u type
函数。

您可以首先通过如下函数展平列表:

def flatten(lst):
    outp = []
    for elem in lst:
        if isinstance(elem, list):
            outp += flatten(elem)
        else:
            outp.append(elem)
    return outp

但是,请记住,关心对象的类型被认为是非音速的,只要它执行您希望它执行的操作。(查找duck typing。)

您可以尝试以下递归函数,该函数检查列表
lst
中的所有元素是否为
typ
类型或满足相同属性的列表

def all_type_eq(lst, typ):
    # an empty list would return True
    if len(lst) == 0:
        return True
    elif len(lst) == 1:
        return True
    else:
        for l in lst :
            # check if other elements are the same as first
            if type(l) == list:
                if not all_type_eq(l, typ):
                    return False
            elif typ != type(l):
                return False
        return True

我假设列表被视为一个特例,它们的内容需要递归地检查;其他迭代器(如元组,甚至字符串)不是这样处理的

首先,递归查找第一个非列表元素的函数(如果没有,则引发ValueError):

然后是一个递归函数,用于检查列表中的所有项是否与第一个项具有相同的类型:

def all_same_type(L):
    try:
        first_item = first_find_element(L)
    except ValueError:
        # Nothing in there, so all the same
        return True

    return all_of_type(L, type(first_item))

def all_of_type(L, first_item_type):
    for item in L:
        if type(item) == list:
            # Recurse
            if not all_of_type(item, first_item_type):
                return False
        elif type(item) != first_item_type:
            return False
    # We saw all and didn't return False, so return True.
    return True

然后,您似乎需要做的是在检查之前将列表展平:

def flatten(lst):
    for e in lst:
        if type(e) == lst:
            for ee in flatten(e):
                yield ee
        else:
            yield e
然后检查是否存在类型不匹配:

def mixed_types(lst):
    t0 = None

    for e in flatten(lst):
        if t0 is None:
            t0 = type(e)
        elif type(e) != t0:
            return False

    return True

也许你可以先把列表展平这有点尴尬,因为列表和字符串本身是不同类型的。因此,您需要列表的特殊情况,如果一个值是一个列表,那么递归地检查它的内容?不清楚您要的是什么(
[[[“aa”,“bb”],**[“aa”,“bb”],“aa”]**]
不是有效的表达式)。你提出的解决方案不是你想要的吗?你的列表是由列表组成的。是否要检查子列表中的项目是否都是相同类型的?@skyking My html(粗体)已编辑为python。不,它不会工作,它只会检查最外层的列表[[list1],[list2]]并给出True,但是list2包含混合类型[[list],str]]
def mixed_types(lst):
    t0 = None

    for e in flatten(lst):
        if t0 is None:
            t0 = type(e)
        elif type(e) != t0:
            return False

    return True