Python——检查元组中是否包含任何列表元素
当需要检查元组中是否包含任何列表元素时,需使用布尔值和简单迭代。
下面是该算法的演示:
示例
my_tuple = (14, 35, 27, 99, 23, 89,11)
print("The tuple is :")
print(my_tuple)
my_list = [16, 27, 88, 99]
print("The list is :")
print(my_list)
my_result = False
for element in my_list:
if element in my_tuple :
my_result = True
break
print("The result is :")
if(my_result == True):
print("The element is present in the tuple")
else:
print("The element isn't present in the tuple")输出
The tuple is : (14, 35, 27, 99, 23, 89, 11) The list is : [16, 27, 88, 99] The result is : The element is present in the tuple
说明
定义一个整数元组,并在控制台上显示。
定义一个整数列表,并在控制台上显示。
最初将布尔值指定为“False”。
迭代列表,如果元组中存在该元素,则将布尔值重新初始化为“True”。
控制跳出循环。
根据布尔变量的值,在控制台上显示输出。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP