简体   繁体   English

Python 列表仅包含数字整数或字符串

[英]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.

相关问题 连接字符串数字列表以创建整数Python 3 - Joining a list of string digits to create an integer Python 3 条件,如果字符串仅包含变量和整数Python - Condition if string contains only variable and integer Python 如何检查一个字符串只包含数字和/在python中? - How to I check that a string contains only digits and / in python? 检查字符串是否包含 integer python - check if string contains an integer python 将长整数的数字与 Python 3 中的数字列表匹配 - Matching digits of a long integer to a list of numbers in Python 3 如何在Python中识别列表的任何字符串是否包含数字 - how to identify that anystring of list contains digits or not in python Python-使用递归将整数数字化? - Python - String of Digits to Integer using Recursion? 我怎么能告诉一个字符串是否只包含使用正则表达式的python中的数字和空格 - how can i tell if a string contains ONLY digits and spaces in python using regex Python 3.6 如何检查一个列表是否包含一个带字母的字符串并且还包含一个整数? - Python 3.6 how to check to see if a list contains a string with letters and also contains an integer? 创建一个 function 以仅从 python 中的列表中获取 integer 或字符串值 - create a function to get only integer or string value from a list in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM