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 红包祝福语简短10字
18 周六早晨祝福语简短