tags:

views:

72

answers:

2

I have list of

34.00B
65.89B
346M

I need

34.
65.89
.344

So, how do i remove last character, is if B or M, divide M's by 1000.

+6  A: 

I think you just want something like this:

divisors = {'B': 1, 'M': 1000}
def fn(number):
    if number[-1] in divisors:
        return str(float(number[:-1]) / divisors[number[-1]])
    return number

map(fn, ['34.00B', '65.89B', '346M'])

I converted the return value back to a string since your question was a little unclear

Michael Mrozek
How would keep float? return (float(number[:-1]) / divisors[number[-1]])?
how use this function? I cant seem print "number", TIA
I put divisors, "under" fn(number): I got output, thanks
+2  A: 

Not sure if I understood the question clearly, the following code removes the last character and returns a float of the value (dividing by 1000 if the last character was 'M').

lst=[
    "34.00B",
    "65.89B",
    "346M"
]

lst=map(lambda x: float(x[:-1]) if x[-1]=='B' else float(x[:-1])/1000, lst)
print lst
MAK
Sometimes one-liners aren't worth it...
Ned Batchelder
@Ned Batchelder: I just wrote what came to mind (you gotta admit, it is very simple). Didn't think it would be hard to follow. Not trying to be smart or anything.
MAK
I do the same thing with Python; if I don't have to think about the solution and I'm just typing it in automatically, it tends to end up as one long line
Michael Mrozek