Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
The result of one of my web scrapes produces the following:
price = ['$1049.98']
which is type list, and I am trying to convert this to float.
Take the first and only element in the list, strip off the $ sign and convert it to a float:
parsed_price = float(price[0].lstrip('$'))
import re
price = ['$1049.98']
get_values = [float(re.search(r'(\d+\.\d+)+',cost).group()) for cost in price]
print(get_values)
>>>[1049.98]
Here is my simple solution. Hope that helps.
price = ['$1049.98']
result = [float(i[1:]) for i in price][0]
print(result)
You can change the index(I mean, replace 0 with other numbers) or loop though the list(result).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm working with big multidimensional arrays. I want to know after x loops the values S,S2,E,E2
I want to find all the solutions of a loop.
I have a code that iterates with a greedy. My output is a list of numers. I want to save the arrays that generate those numbers. My code:
while True:
if f2<f:
f,S,S2=f2,E,E2 # f is a value and S and E are arrays
......
print f2-f
else:
break
If i run this code I have a list of f2-f but I need also to save the f,S,S2,f2,E,E2 created in each passage of the while.. Thank you
results = []
while True:
if f2<f:
f,S,S2=f2,E,E2 # f is a value and S and E are arrays
......
results.append((f,S,S2,E,E2))
print f2-f
print "intermediate result:", results[-1]
else:
break
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
Can someone please explain how this works? Like, how does it count?
for a in range(2,5):
for b in range(1,2):
print (a+b,end=" ")
print("---",end=" ")
the output is: 3 --- 4 --- 5 ---
It just adds 2, 3, 4 with 1 and prints the result.
In Python-3.x, print() is a function and the argument end in it means
string appended after the last value, default a newline
Values returned by range(x, y) are [x, y-1]
If you don't know the usage of a function, you could open ipython and type something like the following:
help(print)
help(range)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a json array in this format in PYTHON. How to make it to a 2d numpy array?
[{"z":1,"y":0.10000000149011612,"x":0.10000000149011612},{"z":0.6666666865348816,"y":0.10000000149011612,"x":0.20000000298023224},{"z":0.5,"y":0.10000000149011612,"x":0.30000001192092896},{"z":0.6666666865348816,"y":0.10000000149011612,"x":0.4000000059604645},{"z":0.3333333432674408,"y":0.10000000149011612,"x":0.5},{"z":0.1666666716337204,"y":0.10000000149011612,"x":0.6000000238418579},{"z":0.8333333134651184,"y":0.10000000149011612,"x":0.699999988079071},{"z":0.5,"y":0.10000000149011612,"x":0.800000011920929},{"z":0.5,"y":0.25999999046325684,"x":0.10000000149011612},{"z":0.5,"y":0.25999999046325684,"x":0.20000000298023224},{"z":1,"y":0.25999999046325684,"x":0.30000001192092896},{"z":0.1666666716337204,"y":0.25999999046325684,"x":0.4000000059604645},{"z":0.1666666716337204,"y":0.25999999046325684,"x":0.5},{"z":0.5,"y":0.25999999046325684,"x":0.6000000238418579},{"z":0.8333333134651184,"y":0.25999999046325684,"x":0.699999988079071},{"z":0.6666666865348816,"y":0.25999999046325684,"x":0.800000011920929},{"z":0.5,"y":0.41999998688697815,"x":0.10000000149011612},{"z":0.6666666865348816,"y":0.41999998688697815,"x":0.20000000298023224},{"z":0.1666666716337204,"y":0.41999998688697815,"x":0.30000001192092896},{"z":0.6666666865348816,"y":0.41999998688697815,"x":0.4000000059604645},{"z":0.5,"y":0.41999998688697815,"x":0.5},{"z":0.8333333134651184,"y":0.41999998688697815,"x":0.6000000238418579},{"z":0.8333333134651184,"y":0.41999998688697815,"x":0.699999988079071},{"z":0.5,"y":0.41999998688697815,"x":0.800000011920929},{"z":0.6666666865348816,"y":0.5799999833106995,"x":0.10000000149011612},{"z":0.6666666865348816,"y":0.5799999833106995,"x":0.20000000298023224},{"z":0.1666666716337204,"y":0.5799999833106995,"x":0.30000001192092896},{"z":0.1666666716337204,"y":0.5799999833106995,"x":0.4000000059604645},{"z":0.8333333134651184,"y":0.5799999833106995,"x":0.5},{"z":0.5,"y":0.5799999833106995,"x":0.6000000238418579},{"z":0.6666666865348816,"y":0.5799999833106995,"x":0.699999988079071},{"z":0.1666666716337204,"y":0.5799999833106995,"x":0.800000011920929},{"z":0.3333333432674408,"y":0.7400000095367432,"x":0.10000000149011612},{"z":1,"y":0.7400000095367432,"x":0.20000000298023224},{"z":0.1666666716337204,"y":0.7400000095367432,"x":0.30000001192092896},{"z":0.3333333432674408,"y":0.7400000095367432,"x":0.4000000059604645},{"z":1,"y":0.7400000095367432,"x":0.5},{"z":0.1666666716337204,"y":0.7400000095367432,"x":0.6000000238418579},{"z":0.1666666716337204,"y":0.7400000095367432,"x":0.699999988079071},{"z":0.1666666716337204,"y":0.7400000095367432,"x":0.800000011920929}]
The array look like: ([[1,2,3],[4,5,6]]) Every json object is a []
As I understand, json_array is a list of dictionaries
m = np.zeros((len(json_array), 3))
for i, elem in enumerate(json_array):
m[i,0] = elem['x']
m[i,1] = elem['y']
m[i,2] = elem['z']
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If I had a list
['xxxx', 'oooo', 'xxxx', 'oooo'......etc]
which looks like
(xxxx)
(oooo)
(xxxx)
(oooo)
and the list could be as long as the user inputs,
how would I make a new list sorted by each column which would look like:
['xoxo', 'xoxo', 'xoxo', 'xoxo']
which would be
(xoxo)
(xoxo)
(xoxo)
(xoxo)
myList=['xxxx', 'oooo', 'xxxx', 'oooo']
print [''.join(element) for element in zip(*myList)]
Output
['xoxo', 'xoxo', 'xoxo', 'xoxo']
What you are looking for, is called, transposing an array. That can be achieved with zip function.