finding a float form a string without regex python [duplicate] - python

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 5 years ago.
I am trying to take an input from a "raw_input" function and make it into 3 floats and then sum them up.
user_input = "1.23+2.25+3.25"
is it possible to take the 3 numbers and add them to a list of floats that look like this or something similar?
float_lst = [1.23,2.25,3.25]

Yes.
float_lst = [float(i) for i in user_input.split("+")]

If I only go by your requirement, not the list, you can eval. Trivial code example below
a = raw_input()
print eval(a)

You can use the split function and then cast the elements to float.
user_input = "1.23+2.25+3.25"
lst = user_input.split("+")
lst = [float(i) for i in lst]
Now you have a list of float so you can do
result = sum(lst)
And you will have the result

Related

python how to add all elements of a list together [duplicate]

This question already has answers here:
How do I parse a string to a float or int?
(32 answers)
Sum a list of numbers in Python
(26 answers)
Apply function to each element of a list
(4 answers)
Closed 6 months ago.
if I had a list
results = ['1','4','73','92']
how do I get that list to call on itself and add all elements together?
the elements have to be strings
map str to int and sum it
results = ['1','4','73','92']
sum(map(int, results))
You can use list comprehension to convert the list of strings to a list of ints, then use the sum function to add all of the integers up.
results = ['1','4','73','92']
sum([int(x) for x in results]) # 170
One way to do this is using a list comprehension + sum operator:
sum([float(num) for num in results])
Note that it's safer to use float() instead of int() since your elements may include decimals.
This can be achieved using a for loop.
First, create a variable for the total:
total = 0
Next, loop through all elements in the list:
for result in results:
Then, check if the string is a number:
if result.isnumeric():
Finally, add the result to the total:
total += int(result)
This results in the final code of:
total = 0
for result in results:
if result.isnumeric():
total += int(result)

is there a pythonic way to convert an input list of integer(which would be a char) into a list of integer? [duplicate]

This question already has answers here:
Convert all strings in a list to integers
(11 answers)
Closed 3 years ago.
I'm trying to take a single line of intergers and using a for loop to convert the elements into integers, Is there a more pyhtonic way of doing so??
a = input().strip().split()
l = []
for i in a:
l.append(int(i))
You can do map directly on split:
l = list(map(int, input().strip().split()))

Convert List to integer in Python [duplicate]

This question already has answers here:
Convert list of ints to one number?
(19 answers)
Closed 4 years ago.
In Python, I want to convert list into int.
so if i have input like this:
a=[1,2,3,4]
i want this output:
1234
so How it is possible?
You can use the join function in conjunction with a generator, like this:
a=[1,2,3,4]
int(''.join(str(i) for i in a))
Output:
1234
With recursion:
a=[1,2,3,4]
def f(l):
if not l: return 0
return l[-1] + f(l[:-1]) * 10
print(f(a))
This outputs:
1234
You can use generator comprehension in the following way:
result = int(''.join((str(i) for i in a)))
This turns every item of the list to a string, joins the list together and then turns the whole thing back to an integer

How to unstring a list/tuple without eval [duplicate]

This question already has answers here:
Convert string to nested structures like list
(2 answers)
Closed 7 years ago.
I'm trying to use input to get the value for a tuple. However, since input sets the value as a string, I'm trying to unstring it as well. I've found that eval works for this purpose, but that it should be distrusted. While this will not be a problem as long as I use the code privately, if I were to publicly release it I want to use the best code possible.
So, is there another way of unstringing a tuple in Python 3?
Here's what I'm currently doing:
>>> a = input("What is the value? ")
What is the value? (3,4)
>>> a
'(3,4)'
>>> eval(a)
(3, 4)
Use the safe version of eval, ast.literal_eval which is designed exactly for what you are trying to achieve:
from ast import literal_eval
tup = literal_eval(a)
I'd do it like this:
>>> inp = input()
'(3,4)'
>>> tuple(map(int, inp.strip()[1:-1].split(',')))
(3, 4)
where strip will make sure leading or trailing blanks won't ruin your day.
Simple, you don't ask the user to input a tuple. Instead you do this:
x = input("Enter x value: ")
y = input("Enter y value: ")
data = (
int(x),
int(y)
)
print(
repr(data)
)
--output:--
(10, 3)

How to approach this loop? [duplicate]

This question already has answers here:
How to find elements that are common to all lists in a nested list?
(4 answers)
Closed 8 years ago.
t = input()
stringlist = []
setlist = []
for _ in range(t):
stringlist.append(raw_input())
print stringlist
for h in stringlist:
setlist.append((set(h)))
print setlist
print len((setlist[0] & setlist[1] & setlist[2]))
It's a simple program to find the number of common letters between the words that given as input. Note: This program only works for exactly 3 inputs. Can someone point me towards how I can generalize the last line of this code to allow for as many inputs as is supplied? I would appreciate it if you could just point me towards the answer and not actually give the answer.
So far Ive thought about using a join() to merge the input strings with the seperator being '&' and then put an eval() on the resulting string.
Just put all the input in a list then use map and set.intersection
l = ["foo", "boo", "foobar"]
common = set.intersection(*map(set,l))
print common
set(['o'])
Full code:
t = int(raw_input())
string_list = [raw_input() for _ in range(t)]
common = set.intersection(*map(set,string_list))
print(len(common))
Or cast the raw_input as a set if you don't need the list of words elsewhere:
string_list = [set(raw_input()) for _ in range(t)]
common = set.intersection(*string_list)

Categories

Resources