Find all numbers in a string in Python 3 [duplicate]

2024/7/7 6:26:07

Newbie here, been searching the net for hours for an answer.

string = "44-23+44*4522" # string could be longer

How do I make it a list, so the output is:

[44, 23, 44, 4522]
Answer

Using the regular expressions as suggested by AChampion, you can do the following.

string = "44-23+44*4522"
import re
result = re.findall(r'\d+',string)

The r'' signifies raw text, the '\d' find a decimal character and the + signifies 1 or more occurrences. If you expect floating points in your string that you don't want to be separated, you might what to bracket with a period '.'.

re.findall(r'[\d\.]+',string)
https://en.xdnf.cn/q/120011.html

Related Q&A

call dictionary from one function to another

How can I call a dictionary created in one function to another?I have tried using How do I access a dictionary from a function to be used in another function? but it doesnt work for me.I have created…

How to change values in numpy array

import numpy as np a=np.array([[4,2,6],[3,6,5]]) b=np.array([3,5])I want to update the numbers in "a" which are bigger than the numbers in "b" to np.nan. If they are smaller or equa…

Why use the object oriented approach in matplotlib for visualizing data? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Set a status for a discord bot

I am trying to get my bot to have the status, m!help. I see that a lot of other bots have their help command in their status so I wanted to do that too. await bot.change_presence(activity=discord.Game(…

Can you permanently change python code by input?

Im still learning python and am currently developing an API (artificial personal assistant e.g. Siri or Cortana). I was wondering if there was a way to update code by input. For example, if I had a lis…

Count the number of files with a special suffix in a directory using python

It is possible to count the number of all files in a directory by:import ospath = /mnt/BIGDATA/num_files = len([f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))])As mentioned in How…

How to not hardcode function in this example

The following links contain 2 csv files that the function should pass through grades_1e_2a grades_2e_4aHowever my function is only able to pass the 2nd linked file, as it is hardcoded to range(4,8). ou…

How to group array based on the same values

Please, confused with array in python. I want to group array based on the same values.Code:enter image description hereid_disease = [penyakit_tepung,hawar_daun] for id_disease in id_disease:qres = acac…

latest video by youtube api

In order to learn this api I am trying to create a bot.one of the things this bot does is to first comment when a channel uploads a video.On some channels it works however on some channels it doesnt wo…

Function to switch between two frames in tkinter

Im looking through the code at passing-functions-parameters-tkinter-using-lambda, and needed a tad more functionality inside his class PageOne(tk.Frame). Instead of using lambda commands below (as he …