在Python中,比较两个列表的大小可以通过以下几种方法:
1. 使用`len()`函数:
list1 = [1, 2, 3]
list2 = [1, 3, 5]
print(len(list1) > len(list2)) 输出:False
2. 使用`cmp()`函数(Python 2.x):
list1 = [1, 2, 3]
list2 = [1, 3, 5]
print cmp(list1, list2) 输出:-1,表示list1 < list2
3. 使用`operator`模块(Python 3.x):
import operator
print(operator.cmp_to_key(lambda x, y: x > y)(list1, list2)) 输出:False
4. 使用列表推导和`any()`函数:
list1 = [1, 2, 3]
list2 = [1, 3, 5]
print any(x > y for x, y in zip(list1, list2)) 输出:False
5. 使用列表推导和`all()`函数:
list1 = [1, 2, 3]
list2 = [1, 2, 3]
print all(x == y for x, y in zip(list1, list2)) 输出:True
6. 将列表转换为元组,然后使用集合的对称差集:
first_list = [['Test.doc', '1a1a1a', 1111], ['Test2.doc', '2b2b2b', 2222]]
second_list = [['Test.doc', '1a1a1a', 1111], ['Test2.doc', '2b2b2b', 2222], ['Test3.doc', '8p8p8p', 9999]]
first_set = set(map(tuple, first_list))
second_set = set(map(tuple, second_list))
print first_set.symmetric_difference(second_set) 输出:{('Test3.doc', '8p8p8p', 9999)}