英文:
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 otherln
). - 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论