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 出阁宴祝福语姐姐简短