conversion of ISOformat in python and find minimum value

Viewed 31
l1 = ["india","pak","usa","bhutan"]

l2= ["ISO10_00_000","ISO5_00_000","ISO1_00_000","ISO3_00_000"]

l1 is country and l2 is population of countries after converting it into dictionary India has population is ISO10_00_000,pak has ISO5_00_000 and so on.... so we want minimum population country in the output. how we can do this. "ISO10_00_000" what do we mean this and how we convert this in numbers? thank you in advance.....

3 Answers

There are lots of different ways to achieve this. List comprehension should can do the trick.

nums = [int(''.join(i[3:].split("_"))) for i in l2]
country = l1[nums.index(min(nums))]

which is essentially the same as doing this:

nums = []
for n in l2:
    num = ''.join(n[3:].split('_'))  # removes first 3 letters and removes the _
    num = int(num)  # converts to integer
    nums.append(num)  # adds it to the list nums
min_index = nums.index(min(nums))    # mind the minimum index
country = l1[min_index]             # get the same index in l1 

output

'usa'
>>> 
>>> l2=["ISO10_00_000","ISO5_00_000","ISO1_00_000","ISO3_00_000"]
>>> 
>>> l2
['ISO10_00_000', 'ISO5_00_000', 'ISO1_00_000', 'ISO3_00_000']
>>> 
>>> 
>>> l2 = [int(v[3:].replace('_','')) for v in l2]
>>> 
>>> 
>>> l2
[1000000, 500000, 100000, 300000]
l1 = ["india","pak","usa","bhutan"]

l2= ["ISO10_00_000","ISO5_00_000","ISO1_00_000","ISO3_00_000"]

To get a sorted list:

sorted(list(zip(l1,l2)), key=lambda x:int(''.join(n for n in x[-1] if n.isdigit())))

To get the smallest country

sorted(list(zip(l1,l2)), key=lambda x:int(''.join(n for n in x[-1] if n.isdigit())))[0][0]
Related