Home > Net >  Statistics.median converting lists containing float numbers into integers
Statistics.median converting lists containing float numbers into integers

Time:10-15

Here is a list, containing different integers, and floats, strings. I want to find the median numbers using statistics.median() but I cannot convert the list into integers.

#DOCTYPE Python 
import statistics
import numpy as np
data = ['3900000.00', '140000000.00', '150000000.00', '3000000.00', '189250000.00', '280000000.00', '280000000.00', '200000000.00', '2250000.00', '85000000.00', '2600000.00', '230000000.00', '680000000.00', '158000000.00', '230000000.00', '300000000.00', '200000000.00', '295000000.00', '250000000.00', '155000000.00', '143850000.00', '182000000.00', '3900000.00', '2200000.00', '91475000.00', '190000000.00', '2100000.00', '325000000.00', '118534000.00', '230000000.00', '3500000.00', '83688000.00', '320000000.00', '565000000.00', '162000000.00', '170000000.00', '265000000.00', '250000000.00', '120000000.00', '230000000.00', '88000000.00', '85000000.00', '209000000.00', '83000000.00', '161300000.00', '215000000.00', '120000000.00', '265000000.00', '167000000.00', '280000000.00', '70000000.00', '410000000.00', '215000000.00', '118000000.00', '179800000.00', '211000000.00', '131750000.00', '2400000.00', '319800000.00', '2400000.00', '140000000.00', '230000000.00', '250000000.00', '2100000.00', '137000000.00', '260000000.00', '178100000.00', '565000000.00', '160000000.00', '173000000.00', '240000000.00', '157000000.00', '176000000.00', '130000000.00', '118000000.00', '131964000.00', '85000000.00', '102268800.00', '92000000.00', '140000000.00', '148000000.00', '3200000.00', '160000000.00', '385000000.00', '140000000.00', '105000000.00', '2700000.00', '88000000.00', '129000000.00', '547950000.00', '250000000.00', '230000000.00', '138000000.00', '225000000.00', '230000000.00', '230000000.00', '230000000.00', '210000000.00', '189000000.00', '140000000.00', '328000000.00', '140000000.00', '220000000.00', '319800000.00', '85000000.00', '100000000.00', '170000000.00', '210000000.00', '2600000.00', '195000000.00', '165000000.00', '160000000.00', '195000000.00', '3800000.00', '250000000.00', '260000000.00', '230000000.00', '85000000.00', '2500000.00', '165000000.00', '86000000.00', '3900000.00', '210000000.00', '230000000.00', '434000000.00', '1600000.00', '1750000.00', '310000000.00', '165000000.00', '290000000.00', '85000000.00', '5000000.00', '380000000.00', '208000000.00', '325000000.00', '110000000.00', '40000000.00', '260000000.00', '207000000.00', '215000000.00', '450000000.00', '1600000.00', '150000000.00', '280000000.00', '220000000.00', '195975000.00', '190000000.00', '190000000.00', '297000000.00', '104100000.00', '110000000.00', '380000000.00', '270000000.00', '100000000.00', '88000000.00', '2400000.00', '2100000.00', '170000000.00', '116700000.00', '88000000.00', '155000000.00', '2400000.00', '2300000.00', '2600000.00', '120000000.00', '4000000.00', '235000000.00', '418000000.00', '125000000.00', '255000000.00', '290000000.00', '200000000.00', '233000000.00', '390000000.00', '190000000.00', '1280000000.00', '160050000.00', '285000000.00', '175000000.00', '137000000.00', '205000000.00', '126000000.00', '150000000.00', '800000000.00', '2000000.00', '160000000.00', '205000000.00', '137000000.00']

datas = ["5", "6" , "11", "1", "21", "5", "5", "5", "5", "1.00", "3.00", "5.00"]
# print([int(datas) for datas in datas])

print(int(str(datas)))

I have tried several methods, but I am looking for the most straight easy method. I have been getting ValueError: invalid literal for int() with base 10: '' as an error whenever i wanted to use int() method, then i tried to use str() method where float() cant be used on strings again. So i am stuck.

CodePudding user response:

Replace:

print(int(str(datas)))

With:

print(list(map(float,datas)))

Or if you want integers not float then:

print([int(float(data)) for data in datas])

CodePudding user response:

Calculating the median value from a list of numbers is trivial.

Neither of the lists in OP's question contain ints or floats - they are all strings.

The list elements should be normalised to float because some values such as '3900000.00' can't be converted [directly] to int.

Thus, if all elements in a list can be converted to float then you could implement your own median function as follows:

def median(_list):
    _list = sorted(map(float, _list))
    m = (ll := len(_list)) // 2
    return _list[m] if ll % 2 else (_list[m]   _list[m-1]) / 2

For the data list this will return 166000000.0 and for datas 5.0

Obviously the return value could just be passed to int() if required.

If you want to use the statistics module (which will almost certainly be faster) then:

from statistics import median

print(median(map(float, data)))
  • Related