[英]Python list contains only digits integer or string
I need to check if a list contains only digits of int or str.我需要检查一个列表是否只包含 int 或 str 的数字。
ex:前任:
[3, 34, "45", 7, "3"]
would return true会返回真
and ["3" "hello", 9]
和["3" "hello", 9]
would return false会返回假
I know that the .isdigit()
function cannot be used with a list containing int我知道.isdigit()
函数不能与包含 int 的列表一起使用
Simply try this one-linear pythonic solution:只需尝试这个单线性 pythonic 解决方案:
>>> all(map(lambda x: str(x).isdigit(), [3, 34, "45", 7, "3"]))
True
>>> all(map(lambda x: str(x).isdigit(), ["3" "hello", 9]))
False
your_list = [3, 34, "45", 7, "3"]
def foo(e):
try:
int(e)
return True
except ValueError:
return False
all(map(foo, your_list))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.