Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a file like this
(E:0.13228,((D:0.08440,A:0.14071):0.29270,(H:0.30329,(B:0.06928,
(F:0.00236,G:0.00010):0.00010):0.44531):0.06201):0.57269,C:0.19183);
I need to generate it as follows:
(E:0.13228,((D:0.08440,A:0.14071)0.29270:0.29270,(H:0.30329,(B:0.06928,
(F:0.00236,G:0.00010)0.00010:0.00010)0.44531:0.44531)0.06201:0.06201)0.57269:0.57269,C:0.19183);
What you should use in this cases are the regular expressions. Check Python re
match_pattern = r'\):(\d(\.\d+)?)'
output_pattern = r')\1:\1'
input_str = """(E:0.13228,((D:0.08440,A:0.14071):0.29270,(H:0.30329,(B:0.06928,
(F:0.00236,G:0.00010):0.00010):0.44531):0.06201):0.57269,C:0.19183);"""
output_str = re.sub(match_pattern, output_pattern, input_str)
print(output_str)
And the result is:
(E:0.13228,((D:0.08440,A:0.14071)0.29270:0.29270,(H:0.30329,(B:0.06928,
(F:0.00236,G:0.00010)0.00010:0.00010)0.44531:0.44531)0.06201:0.06201)0.57269:0.57269,C:0.19183);
I guess you could go with some string manipulation like so if you're not using regular expressions
data = "(E:0.13228,((D:0.08440,A:0.14071):0.29270,(H:0.30329,(B:0.06928,
(F:0.00236,G:0.00010):0.00010):0.44531):0.06201):0.57269,C:0.19183);"
dataParts = data.split("):")
correctedData = dataParts[0]
for dataPart in dataParts[1:]:
number = dataPart[:7]
correctedData = "){}:".format(number).join([correctedData, dataPart])
but that's not clean...
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
# no periods, spaces or puctuation marks
punctuation_not_wanted = [".","!", " " "/"]
for punctuation_not_wanted in s:
if punctuation_not_wanted in s:
return false
You can compare them as sets. If they have same symbols, their intersection (&) will have this same symbols and will converts to True for if statement.
if set(punctuation_not_wanted) & set(s):
return False
all([(c not in s) for c in punctuation_not_wanted])
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to remove the amp; from all the data inside the list
['?daypartId=1&catId=12', '?daypartId=1&catId=1', '?daypartId=1&catId=2', '?daypartId=1&catId=11', '?daypartId=1&catId=10', '?daypartId=1&catId=6', '?daypartId=1&catId=4', '?daypartId=1&catId=14', '?daypartId=1&catId=5', '?daypartId=1&catId=3', '?daypartId=1&catId=8']
desired output without amp;
['?daypartId=1&catId=12', '?daypartId=1&catId=1', '?daypartId=1&catId=2', '?daypartId=1&catId=11',...]
Using re.sub():
import re
data = ['?daypartId=1&catId=12', '?daypartId=1&catId=1', '?daypartId=1&catId=2', '?daypartId=1&catId=11', '?daypartId=1&catId=10', '?daypartId=1&catId=6', '?daypartId=1&catId=4', '?daypartId=1&catId=14', '?daypartId=1&catId=5', '?daypartId=1&catId=3', '?daypartId=1&catId=8']
data = [re.sub(r'amp;', '', item) for item in data]
A simple list comprehension is best using replace
my_list=['?daypartId=1&catId=12', '?daypartId=1&catId=1', '?daypartId=1&catId=2', '?daypartId=1&catId=11', '?daypartId=1&catId=10', '?daypartId=1&catId=6', '?daypartId=1&catId=4', '?daypartId=1&catId=14', '?daypartId=1&catId=5', '?daypartId=1&catId=3', '?daypartId=1&catId=8']
[i.replace('&','') for i in my_list]
using regex
import re
[re.sub('&','',i) for i in my_list]
Output
['?daypartId=1;catId=12',
'?daypartId=1;catId=1',
'?daypartId=1;catId=2',
'?daypartId=1;catId=11',
'?daypartId=1;catId=10',
'?daypartId=1;catId=6',
'?daypartId=1;catId=4',
'?daypartId=1;catId=14',
'?daypartId=1;catId=5',
'?daypartId=1;catId=3',
'?daypartId=1;catId=8']
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a list of acc nos:
list1 = ['1234','3456','2345','5543','1344','5679','6433','3243','0089']
Output I need is a string:
print(output): '1234','3456','2345','5543','1344','5679','6433','3243','0089'
You can join all the values with ',' and then adding a ' before and after the string like this:
"'{0}'".format("','".join(list1))
>>> list1 = ['1234','3456','2345','5543','1344','5679','6433','3243','0089']
>>> print(','.join(["'{0}'".format(s) for s in list1]))
'1234','3456','2345','5543','1344','5679','6433','3243','0089'
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Text file or string:
SomeText1/SomeText2/SomeText3/SomeText4/SomeText5
#What I am looking for:
split_func(3, "/")
>>> SomeText3
Try:
s = "SomeText1/SomeText2/SomeText3/SomeText4/SomeText5"
# s.split("/") returns a list of strings, split at the "/"
# I.e. ["SomeText1", "SomeText2", "SomeText3", "SomeText4", "SomeText5"]
# Then take the second element (remembering that the count starts at 0
result = s.split("/")[2]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How would I create a script it in python late the values from the recursive sequence:
$x_1 = \sqrt{2}$, $x_{n+1} = \sqrt{2x_n}$ http://www.sciweavers.org/upload/Tex2Img_1392861864/render.png
X = [sqrt(2)]
for i in range(1,10):
X.append(sqrt(2*X[i-1]))
Here is a slow solution. Assuming n is >=1
import math
def recursive(n):
if n = 1:
math.sqrt(2)
return math.sqrt(2*recursive(n-1))