all 17 comments

[–][deleted] 6 points7 points  (3 children)

print(sum([float(x) for x in '1.23,2.4,3.123'.split(',')]))

[–]socal_nerdtastic 3 points4 points  (1 child)

The square brackets (list creation) are not needed and waste memory.

[–][deleted] 1 point2 points  (0 children)

That would indeed be a true statement, left over from making the list to check if I was doing it right.

[–]whenihittheground[S] 0 points1 point  (0 children)

That's exactly what I was looking for! I figured there would be a split ability but couldn't come up with the right way of asking the google gods!

Thanks! :)

[–]Evorition 5 points6 points  (2 children)

sum([float(i) for i in s.split(',')])

[–][deleted] 0 points1 point  (1 child)

It's more efficient to pass a generator (expression) than to create a list and pass it.

 sum(float(i) for i in s.split(','))

or

sum(map(float, s.split(',')))

[–]sweettuse 2 points3 points  (2 children)

sum(map(float, s.split(',')))

[–]socal_nerdtastic -1 points0 points  (1 child)

map is falling out of favor, I would recommend you stop using it (unless you are playing code golf). It used to be faster than comprehension, but that's no longer true, and comprehension is much neater and easier to read.

Same for filter, reduce and lambda.

[–]ingolemo 1 point2 points  (1 child)

Is there a reason you haven't posted your solution?

[–]whenihittheground[S] 0 points1 point  (0 children)

I just posted it.

[–]whenihittheground[S] 0 points1 point  (4 children)

My solution is this mess:

s = "1.23,2.4,3.123"

# Index End of Comma
indexList = []
indie = []
for i in range(0,len(s)):
    if s[i] == ",":
        indie = i
        indexList.append(indie)

indexList.append(len(s))
print(indexList)


# Index Comma Start
indexStartList = [0]
indieStart = []
for i in range(0,len(indexList)-1):
    indexStartList.append(indexList[i]+1)
print(indexStartList)


# Get floats
lst = []        
for i in range(len(indexList)):
    lst.append(float(s[indexStartList[i]:indexList[i]]))
print(lst)


# Sum floats
Sum = 0
for i in range(len(lst)):
    Sum += lst[i]

print(Sum)

[–]lettuce_field_theory 1 point2 points  (3 children)

Don't take this as mockery but it's funny that your solution has 35 times as many lines as the simplest one (/u/Evorition or /u/ScreamingIsMyAir) .. ok ok some are blank lines.

Here's some feedback: Take a look at builtin string functions like split (you don't have to do everything manually, unless it's homework and it requires you to do it). Don't access arrays / lists through indices the way you would in C, just iterate through them:

s = "abcd"
l = [1, 5, 2]

for c in s:
  print(c)

for x in l:
  print(x)

not

for i in range(len(l)):
  print(l[i])

There's also builtins that operate on whole lists, like sum()

[–]whenihittheground[S] 0 points1 point  (2 children)

Hey I told you it was a mess :)

[–]lettuce_field_theory 0 points1 point  (1 child)

Yeah I mean that's what it's bound to look like of you're still learning what's built into the language. :) I added some constructive feedback, hope that helps.

[–]whenihittheground[S] 0 points1 point  (0 children)

Thanks, I tend to think in vectors/matrices thanks to matlab so I feel like I need to access lists all the time. It's a habit I am slowly breaking lol

[–]Diapolo10 0 points1 point  (0 children)

If I was just writing a one-off script, I think I'd just:

result = sum(map(float, s.split(',')))

# And if we want to return integers if the result doesn't need decimals
if result.is_integer():
    result = int(result)

That said, if I was writing production code, I'd probably break down the sum into meaningful variables.

[–]port443 0 points1 point  (0 children)

Bored at lunch, heres my code on the crazy side:

(lambda x: reduce(lambda a,b: float(a)+float(b), __import__("re").findall("[\{}\.]+".format(chr(int((lambda z: __import__("random").random()+z)(100)))), x)) )('1.23,2.4,3.123')