Python Forum
Need help amalgamating two scripts
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help amalgamating two scripts
#7
You have a list with values ["-500", "+280", "+962", "-2088"] and whant to sum them?
In [1]: l = "balance: -£500 yes no perhaps?"

In [2]: print("{}".format(l.split()[1].replace("£", "")))
-500

In [3]: balance = ["-500", "+280", "+962", "-2088"]

In [4]: b = [int(v.strip('+')) for v in balance]

In [5]: b
Out[5]: [-500, 280, 962, -2088]

In [6]: sum(b)
Out[6]: -1346
str.strip(",.!@?") removes desired symbols from both sides of a string. Default are the blank symbols: \n, \r and space
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
Need help amalgamating two scripts - by brocq_18 - Mar-07-2017, 02:24 PM
RE: Need help amalgamating two scripts - by Larz60+ - Mar-07-2017, 02:37 PM
RE: Need help amalgamating two scripts - by wavic - Mar-07-2017, 02:45 PM
RE: Need help amalgamating two scripts - by wavic - Mar-07-2017, 04:46 PM
RE: Need help amalgamating two scripts - by Ofnuts - Mar-20-2017, 02:02 PM
RE: Need help amalgamating two scripts - by wavic - Mar-20-2017, 12:00 PM
RE: Need help amalgamating two scripts - by Ofnuts - Mar-21-2017, 12:51 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020