Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python 3中查找字符串中的所有数字_Python_String_Int_Cal - Fatal编程技术网

在Python 3中查找字符串中的所有数字

在Python 3中查找字符串中的所有数字,python,string,int,cal,Python,String,Int,Cal,这里的新手,已经在网上搜索了几个小时的答案 string = "44-23+44*4522" # string could be longer 如何使其成为列表,因此输出为: [44, 23, 44, 4522] 使用AChampion建议的正则表达式,可以执行以下操作 string = "44-23+44*4522" import re result = re.findall(r'\d+',string) r“”表示原始文本,'\d'表示小数字符,+表示1次或多次出现。如果您希望字符串中

这里的新手,已经在网上搜索了几个小时的答案

string = "44-23+44*4522" # string could be longer
如何使其成为列表,因此输出为:

[44, 23, 44, 4522]

使用AChampion建议的正则表达式,可以执行以下操作

string = "44-23+44*4522"
import re
result = re.findall(r'\d+',string)
r“”表示原始文本,'\d'表示小数字符,+表示1次或多次出现。如果您希望字符串中的浮点不希望被分隔,则可以使用句点“.”括起来

re.findall(r'[\d\.]+',string)

在这里,您可以看到您的合成功能,并对其进行了解释和详细说明。
由于您是新手,这是一种非常简单的方法,因此易于理解。

def find_numbers(string):
    list = []
    actual = ""
    # For each character of the string
    for i in range(len(string)):
        # If is number
        if "0" <= string[i] <= "9":
            # Add number to actual list entry
            actual += string[i]
        # If not number and the list entry wasn't empty
        elif actual != "":
            list.append(actual);
            actual = "";
    # Check last entry
    if actual != "":
        list.append(actual);
    return list
def find_编号(字符串):
列表=[]
实际值=“”
#对于字符串的每个字符
对于范围内的i(len(string)):
#如果是数字

如果“0”,请查看
re
模块和
split
,您可以在其中对运算符进行拆分。我真的不相信您已经搜索网络数小时了。我向你保证,在谷歌上输入你确切的问题标题会给我答案。已使用至少2个小时搜索此内容。谢谢你的快速回复!唯一需要添加的是如果OP需要一个int列表
result=[int(i)for i in re.findall(r'\d+',string)]
result=[int(i)for i in re.split('[+-*/],string)]
这被认为很简单吗?