I have a dynamically generated string like:
'\n\n\n0\n1\n\n\n\n\n\n'
or
'\r\n\r\n\r\n0\r\n\r\n1\r\n\r\n'
or
'\r\n\r\n\r\n1/2\r\n\r\n1/2\r\n\r\n'
I wonder what is the best way to extract only the number 1, 0 or 1/2 with python 3
What I am doing now is use \r\n or \n to split the string and check each element in the list - I don't like my own way to process, there should be a better and elegant way to do that.
Thanks
Split on whitespace to retrieve words. Then turn each word into a number, a fraction. Then convert to floating point, in case you find that more convenient.
(No need to wrap it with list(), if you're happy with a generator.)
from fractions import Fraction
def get_nums(s):
"""Extracts fractional numbers from input string s."""
return list(map(float, (map(Fraction, s.split()))))
It all can be done as a single one-liner using list-comprehension:
numbers=[float(i) for i in myString.split()]
The split method of the string class will take care of the excess whitespace for you.
Related
Not sure if I clearly describe my question, my original string is:
"1.you are beautiful 2.hello world 3.Thanks!"
I want the output to be:
"1.you are beautiful"
"2.hello world"
"3.Thanks!"
or
"1.you are beautiful
2.hello world
3.Thanks!"
I used split() when the sentence contains comma or period. However, I am thinking if I can write a function by using the number. For example, if type(item)==int then add a line break
Thank you for your help!
Here you go. This will only work for single digit numbers and will work with or without the period.
a='1.you are beautiful 2.hello world 3.Thanks!'
out=''
for ch in a:
if ch.isdigit():
out+='\n'+ch
else:
out+=ch
print(out)
If you want to detect any length number (e.g. 11) you would have to use enumerate(a) and "look ahead" to find the period '.' with another for loop and then add the new line or look into use the re package and Regular Expressions.
I have a string: "0x1.9999999afpap-4". I hope to have a regular expression to extract 1.9999999afpa from this string. I am looking for a regular expression solution to extract everything from a string except the "0x" and "p-4". At meanwhile, I hope this solution could be applied to other strings with random letters and length, such as extract"1.999999999pp" from "0x1.999999999ppp-4"
Thanks.
use a non-capturing group (note: you have to escape the dot to be accurate):
test ="0x1.9999999afp-4"
import re
new=re.search("1\.\w*(?=[$p])", test)
print (new.group())
now output is
"1.9999999af"
Hey guys so I tried looking at previous questions but they dont answer it like my teacher wants it to be answered. Basically i need to get a string from a user input and see if it has:
at least one of [!,#,#,$,%,^,&,*,(,)] (non-letter and nonnumeric
character)
o Create a list for these special characters
I have no idea how to make a def to do this. Please help!
You should probably look into Regular expressions. Regular expressions allow you to do many string operations in a concise way. Specifically, you'll want to use re.findall() in order to find all special characters in your string and return them. You can check if the returned list has length 0 to check if there were any special characters at all.
With regards to building the regular expression to find special characters itself... I'm sure you can figure that part out ;)
Please try the below
import re
inputstring = raw_input("Enter String: ")
print inputstring
print "Valid" if re.match("^[a-zA-Z0-9_]*$", inputstring) else "Invalid"
I have a rectangular Coordinate in Matlab that looks like the following:
0.0240 - 0.1680i
I'd like to split the double into it real and imaginary parts, those parts being 0.0240 and -0.1680 (Don't need the i here)
I've converted the double into a string using the following:
I=0.0240 - 0.1680*i
I_1=num2str(I)
Im not sure how to proceed here to get what i want. strsplit() just gives back the string in the form it already is. Id like to somehow split it to give me the two numbers separately. I'm not too experienced with data manipulation in Matlab so any help is appreciated.
num2str converts number to string. It is not for separating real and imaginary parts.
You can use:
I=0.0240 - 0.1680*i;
real_part=real (I)
imaginary_part=imag(I)
I have a huge string. I need to extract a substring from that that huge string. The conditions are the string starts with either "TECHNICAL" or "JUSTIFY" or "ALIGN" and ends with a number( any number from 1 to 10) followed by period and then followed by space. so for example, I have
string x = "This is a test, again I am testing TECHNICAL: I need to extract this substring starting with testing. 8. This is test again and again and again and again.";
so I need this
TECHNICAL: I need to extract this substring starting with testing.
I was wondering if someone has elegant solution for that.
I was trying to use the regular expression, but I guess I could not figure out the right expresion.
any help will be appreciated.
Thanks in advance.
Try this: #"((?:TECHNICAL|JUSTIFY|ALIGN).*?)(?:[1-9]|10)\. "