简体   繁体   中英

python remove last character and id

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM