当需要检查字符串列表中的所有元素是否都是数字时,使用“all”运算符。
下面是相同的演示
my_list = ["434", "823", "98", "74", '9870'] print("名单是:") print(my_list) my_result = all(ele.isdigit() for ele in my_list) if(my_result == True): print("All the elements in the list are numeric") else: print("All the elements in the list are not numeric")输出结果
名单是: ['434', '823', '98', '74', '9870'] All the elements in the list are numeric
定义了一个整数列表并显示在控制台上。
'all' 运算符用于检查每个元素是否为数字。
这是使用“isdigit”方法完成的。
此操作的结果分配给一个变量。
根据结果的布尔值,在控制台上显示相关消息。