Python程序检查字符串列表中的所有元素是否都是数字
当需要检查字符串列表中的所有元素是否都是数字时,使用“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”方法完成的。
此操作的结果分配给一个变量。
根据结果的布尔值,在控制台上显示相关消息。
热门推荐
10 小侄过生日祝福语简短
11 小店营业的祝福语简短
12 担任老师的祝福语简短
13 饮料吧台祝福语简短大气
14 公司新春搞笑祝福语简短
15 高情商敬酒祝福语简短
16 婚礼祝福语新郎妹妹简短
17 教师答辩通过祝福语简短
18 生女孩红包简短祝福语