Python - 测试元组列表是否有单个元素
给定一个元组列表,检查它是否只由一个元素组成,多次使用。
Input : test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
Output : True
Explanation : All elements are equal to 3.
Input : test_list = [(3, 3, 3), (3, 3), (3, 4, 3), (3, 3)]
Output : False
Explanation : All elements are not equal to any particular element.
方法#1:使用循环
在此,我们检查所有元素并将它们与元组列表中初始元组的初始元素进行比较,如果有任何元素不同,则将结果标记为关闭。
Python3
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# Using loop
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
# checking for similar elements in list
res = True
for sub in test_list:
flag = True
for ele in sub:
# checking for element to be equal to initial element
if ele != test_list[0][0]:
flag = False
break
if not flag:
res = False
break
# printing result
print("Are all elements equal : " + str(res))
Python3
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# Using all() + list comprehension
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
# checking for single element using list comprehension
res = all([all(ele == test_list[0][0] for ele in sub) for sub in test_list])
# printing result
print("Are all elements equal : " + str(res))
输出:
The original list is : [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
Are all elements equal : True
方法 #2:使用all() +列表推导式
在这里,我们使用 all() 执行检查所有元素是否相同的任务,列表理解用于执行迭代元组列表中所有元组的任务。
蟒蛇3
# Python3 code to demonstrate working of
# Test if tuple list has Single element
# Using all() + list comprehension
# initializing list
test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
# printing original list
print("The original list is : " + str(test_list))
# checking for single element using list comprehension
res = all([all(ele == test_list[0][0] for ele in sub) for sub in test_list])
# printing result
print("Are all elements equal : " + str(res))
输出:
The original list is : [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)]
Are all elements equal : True