如何在一行中编写并行循环迭代(列表长度不等)

2024-06-01 01:50:50 发布

您现在位置:Python中文网/ 问答频道 /正文

li1 = [['a','b','c'], ['c','d','e']]
li2 = [['c','a','b'], ['c','e','d']]
c = 1
for i in range(len(l11)):
    if (sorted[li1[i]]!=sorted(li2[i]):
        c = 0
if(c): k = True
else: k = False

怎么把这个写在一行里?另外,如何使用zip()来实现这一点? 如果li2 = [['a','c','b']]呢?使用zip将返回True,但它将返回False。你知道吗


Tags: infalsetrueforlenifrangezip
1条回答
网友
1楼 · 发布于 2024-06-01 01:50:50

您可以使用^{}

>>> zip(li1, li2)
<zip object at 0x0000000000723248>
>>> list(zip(li1, li2))
[(['a', 'b', 'c'], ['c', 'a', 'b']), (['c', 'd', 'e'], ['c', 'e', 'd'])]

^{}

>>> all([True, True, True])
True
>>> all([True, False, True])
False

k = all(sorted(x) == sorted(y) for x, y in zip(li1, li2))

相关问题 更多 >