Home > Blockchain >  Python : Dictionaries for conversions of numbers with M and K to int in a new list
Python : Dictionaries for conversions of numbers with M and K to int in a new list

Time:04-19

Thanks for dropping in this post.

I'm flummoxed with how to pass list members that includes string with K and M.

I need to convert those strings with K and M into integers while those with purely string is kept as it is.

So for example I have a list:

sales = ['No data', '300K', '5M', '3B']

into

updated_sales = ['No data', 300000, 5000000, 3000000000]

I've tried this code:

import decimal
from decimal import Decimal

suffix = {"M": 1000000,
          "B": 1000000000,
          "K": 1000
              }
def updsales(saleslist):
  updated_sales=[]
  for sale in saleslist:
    if sale[-1] in suffix:
      sales_value,magnitude = sale[:-1], sale[-1]
      return updated_sales.append(Decimal(sales_value) * 10 ** suffix[magnitude])
    else:
      return updated_sales.append('No Data')

sales = ['No data', '300K', '5M', '3B']
updsales(sales)

How do I fix this?

CodePudding user response:

Your code was almost correct:

suffix = {"M": 1000000,
          "B": 1000000000,
          "K": 1000
              }
def updsales(saleslist):
    updated_sales=[]
    for sale in saleslist:
        if sale[-1] in suffix:
            sales_value,magnitude = sale[:-1], sale[-1]
            updated_sales.append(float(sales_value) *  suffix[magnitude])
        else:
            updated_sales.append('No Data')
    return updated_sales

sales = ['No data', '300K', '5M', '3B']
print(updsales(sales))

You needed 2 changes:

  1. Don't return the appends, return the fully updated list after the for loop
  2. You were doing 10 ** ... when the suffix list already contains the complete multiplier.

I changed Decimal with float, and you could even just use int in this case.

CodePudding user response:

Code with modifications:

def updsales(saleslist):
    updated_sales=[]
    for sale in saleslist:
        if sale[-1] in suffix:
            sales_value,magnitude = sale[:-1], sale[-1]
            updated_sales.append(int(sales_value) *  suffix[magnitude])
        else:
            updated_sales.append('No Data')
    return updated_sales

updsales(['No data', '300K', '5M', '3B'])

Output:

['No Data', 300000, 5000000, 3000000000]

What you were doing wrong?

  1. Returning the append list instead of the entire list
  2. Using 10 ** even though you'd stored the required zeros in suffix.

However, if you are interested. Here's a simple implementation using .replace():

def updsales(saleslist):
    for i, val in enumerate(saleslist): # This will get the index and the value of the item in list
        if 'M' in val:
            saleslist[i] = int(val.replace('M', '000000')) # Replacing M with 000000 and converting to int
        elif 'B' in val:
            saleslist[i] = int(val.replace('B', '000000000'))# Replacing B with 000000000 and converting to int
        elif 'K' in val:
            saleslist[i] = int(val.replace('K', '000'))# Replacing K with 000 and converting to int
    return saleslist

    updsales(['No data', '300K', '5M', '3B'])

Output:

['No data', 300000, 5000000, 3000000000]
  • Related