检查嵌套列表是否对称

huangapple go评论70阅读模式
英文:

check the nested list for symmetry

问题

检查嵌套列表是否对称的方法是遍历列表并比较每个元素与其镜像(翻转)是否相同。这是一个示例代码:

def is_symmetric(lst):
    def is_mirror(lst1, lst2):
        if isinstance(lst1, list) and isinstance(lst2, list) and len(lst1) == len(lst2):
            for i in range(len(lst1)):
                if not is_mirror(lst1[i], lst2[-(i + 1)]):
                    return False
            return True
        else:
            return lst1 == lst2

    return is_mirror(lst, lst[::-1])

# 测试示例
symmetric_lists = [
    [[[0], [2]],
     [[0], [2]],
     [[0], [2]]],

    [[0, 1, 2],
     [3, 4, 5]],

    [[[[2], [3]]]]
]

asymmetric_lists = [
    [[[0], [2]],
     [[0], [2, 2]],
     [[0], [2]]],

    [[[2, [3]]]]
]

for lst in symmetric_lists:
    print(is_symmetric(lst))  # 应该输出 True

for lst in asymmetric_lists:
    print(is_symmetric(lst))  # 应该输出 False

这段代码定义了一个is_symmetric函数,用于检查嵌套列表是否对称。然后,它测试了一些对称和非对称的示例列表。

英文:

Tell me how to check the nested list for symmetry. For example:

[[[0], [2]],
 [[0], [2]],  
 [[0], [2]]]   # symmetry

[[[0], [2]],
 [[0], [2, 2] ],  
 [[0], [2]]]   # not symmetry

[[0,1,2],
 [3,4,5]]      # symmetry

[[[2, [3]]]]   # not symmetry

[[[[2], [3]]]] # symmetry

答案1

得分: 2

以下是已翻译好的内容:

递归函数可能是解决这个问题的最简单方法之一

def isSymmetric(A):
    if not isinstance(A, list): return True  # 非列表被视为对称
    iA = (len(a) if isinstance(a, list) else -1 for a in A)  # 长度
    return all(l0 == ln for l0 in iA for ln in iA) and all(map(isSymmetric, A))

- 在给定级别所有长度必须相等即第一个长度`l0`与所有其他`ln`相同)。
- 当一个项目不是列表时它被视为对称用于递归目的
- 当一个项目是列表时除了具有所有相等长度的项目外它的所有项目也必须是对称的

输出

s = [[[0], [2]],
    [[0], [2]],  
    [[0], [2]]]   
print(isSymmetric(s))  # True

s = [[[0], [2]],
    [[0], [2, 2]],  
    [[0], [2]]]  
print(isSymmetric(s))  # False

s = [[0,1,2],
    [3,4,5]]      
print(isSymmetric(s))  # True

s = [[[2, [3]]]]  
print(isSymmetric(s))  # False

s = [[[[2], [3]]]] 
print(isSymmetric(s))  # True

*请注意这不会检查cousins之间的对称性例如如果第三级的某些子列表大小不相同但属于不同的父列表”,则不会检测到结构不对称。(您可能希望在这种情况下提供您期望的结果示例*

请注意,我已经删除了不需要翻译的部分。

英文:

A recursive function is probably the simplest way to approach this:

def isSymmetric(A):
    if not isinstance(A,list): return True  # non-lists are symmetrical
    iA = (len(a) if isinstance(a,list) else -1 for a in A) # lengths
    return all(l0==ln for l0 in iA for ln in iA) and all(map(isSymmetric,A))
  • at a given level, all lengths must be equal (i.e. first length l0 is same as all other ln).
  • when an item is not a list, it is considered symmetrical (for the purpose of recursion)
  • when an item is a list, in addition to having all equal length items, all of its items must also be symmetrical

output:

s = [[[0], [2]],
    [[0], [2]],  
    [[0], [2]]]   
print(isSymmetric(s)) # True

s = [[[0], [2]],
    [[0], [2, 2] ],  
    [[0], [2]]]  
print(isSymmetric(s)) # False

s = [[0,1,2],
    [3,4,5]]      
print(isSymmetric(s)) # True

s = [[[2, [3]]]]  
print(isSymmetric(s)) # False

s = [[[[2], [3]]]] 
print(isSymmetric(s)) # True

Note that this does not check for symmetry across "cousins". For example, if some sublists of the third level are not the same size but belong to different "parent list" the structure would not be detected as assymetrical. (you may want to provide an example of your expected result in such cases)

huangapple
  • 本文由 发表于 2023年5月11日 00:53:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定