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).
Related
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 1 year ago.
Improve this question
I want to strip 0 from a given string.
The string contains either 1 or 0. I want to strip the zeroes if they appear at the ends.
I know i can do this using if condition, but i want to know if there is any function made to do this efficiently than using if-else.
Example-
String = 0100010101010
Output = 10001010101
Also, i don't think using regex is any more efficient, complexity wise.
Try this:
s = "0100010101010"
print(s.lstrip("0").rstrip("0"))
'10001010101'
This should work for the string s:
s = s.strip("0")
Make sure s is a string and not a number.
Can you try this , it will work
s = str(s).strip("0")
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 5 years ago.
Improve this question
If I know that I need to put for example 5 numbers in a list, how can i put it in with just one line of input?
Sorry for beginner question, but i am new to this and I wasn't able to find an answer. Thanks in advance.
To me it is unclear what you mean exactly, but I think you mean this:
myList = [int1,int2,int3,...]
Integers can be added into the list directly this way. I hope this helped!
Note that this goes for any type of value, not only for integers
In order to read user input of multiple numbers, you can do the following:
myList = [int(i) for i in input().split()]
If on Python2, you should use this instead:
myList = [int(i) for i in raw_input().split()]
You can use Pythons built range() function. Which will return a range of numbers, on which you would then apply the Python list() function to.
For instance if you want a list between 1 and 100 you can do.
list1 = list(range(1,101))
This which would return all integers between 1 and 100.
In your case say you wanted 5 numbers between 0-5
five_numbers = list(range(1,6))
This would return [1,2,3,4,5]
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 reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have following 2D array
name_list = [['Orange', '5'],['Mango','6'],['Banana','3']]
I want to get each fruit name alone with its count and print it using a python code. So how do I read above array to extract the data (inside for loop)
I need print out as
Name:Orange<br/>
Count:5<br/>
Name:Mango<br/>
Count:6<br/>
Name:Banana<br/>
Count:3<br/>
You can unpack your list like this:
for name, amount in name_list:
print("Name:{}".format(name))
print("Count:{}".format(amount))
Try this:
name_list = [['Orange', '5'],['Mango','6'],['Banana','3']]
for item in name_list:
print("Name: {}".format(item[0]))
print("Count: {}".format(item[1]))
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
How can I count how many different numbers there are in one long number?
For example: this number 1122334455 has 5 different numbers.
How can I do that in Python?
You can do that as:
print len(set(str(s)))
The str() casts the int as a string
The set() takes the unique elements of the string and creates a set of it
The len() returns the length of the set
Examples
>>> print len(set(str(s)))
5
s = 1324082304
>>> print len(set(str(s)))
6
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
How to check if part of text is in tuple?
For example:
my_data = ((1234L,), (23456L,), (3333L,))
And we need to find if 123 or 1234 is in tuple.
I didn't worked lot with tuples before.
In array we use:
if variable in array
But its not working for tuples like my_data
PS. 1st answer solved problem.
def findIt(data, num):
num = str(num)
return any(num in str(i) for item in data for i in item)
data = ((1234L,), (23456L,), (3333L,))
print findIt(data, 123)
Output
True