Convert string list of heights into centimeters

Viewed 50

How can I iterate through a string list of heights and convert the values into centimeters? The heights are formatted like so: ['5\' 11"', '6\' 1"', '6\' 3"']

I understand that I will have to slice the values for feet and inches and cast them as integers, but I don't understand how to slice the values out of the list. For example, if I used height[0] I would return the string 5' 11". How can I simply return the 5 and 11 and iterate through the whole list?

5 Answers

One option if the strings always have both feet and inches in the same order:

l = ['5\' 11"', '6\' 1"', '6\' 3"']

out = [int(ft[:-1])*30.48+int(inch[:-1])*2.54
       for s in l for ft, inch in [s.split()]]

output [180.34, 185.42, 190.5]

For each element in the list you can omit the last charachter and split on ' to get feet and inches. Then map int to both and convert to centimeters

heights = ["5' 11\"", "6' 1\"", "6' 3\""]


def feet_inches_to_cm(feet, inches):
    return feet * 30.48 + inches * 2.54


for height in heights:
    feet, inches = map(int, height[:-1].split("' "))
    print(feet_inches_to_cm(feet, inches))

If you want a list instead of printing in a loop:

heights = ["5' 11\"", "6' 1\"", "6' 3\""]


def feet_inches_to_cm(feet, inches):
    return feet * 30.48 + inches * 2.54

centimeters = [feet_inches_to_cm(*map(int, height[-1].split("' "))) for height in heights]

Hey interesting problem :), How about that?

def imperial_str_to_tuple(imp: str) -> tuple[int,int]:
    clean_str = imp.replace("'", "")  # 6' 1'' -> "6 1"
    number_list = map(int, clean_str.split(" "))  # -> 6,1
    return tuple(number_list)

input_list = ["5' 11''", "6' 1''", "6' 3''"]
splitted = [
    imperial_str_to_tuple(x)
    for x in input_list
]
# should be now: [(5, 11), (6,1), (6,3)]

# keep in mind to create your imperial2metric function
result = [imperial2metric(t) for t in splitted]

You could use a regular expression to isolate the integers in each string as follows:

import re

data = ['5\' 11"', '6\' 1"', '6\' 3"']

for v in data:
    f, i = map(int, re.findall('(\d+)', v))
    cm = (f * 12 + i) * 2.54
    print(f'{v} = {cm:.2f}cm')

Output:

5' 11" = 180.34cm
6' 1" = 185.42cm
6' 3" = 190.50cm

Note:

This technique allows for the possibility that the strings may or may not contain any whitespace. It does however rely on there being exactly two distinct numbers in the string

Break down your problem!

Write a function that translates one value as you want. Here is an example using regular expression (but you can also look at the other answers):

import re

def imp2met(imp):
    ''' Translates a string like 5' 6" into a float given the same measure in cm '''
    m = re.match(r'(\d+)\'\s*(\d+)"', imp)
    if m:
        return (12*int(m.group(1))+int(m.group(2)))*2.54
    else:
        return None

Now you can use either map or list comprehension to translate your list:

i = ['5\' 11"', '6\' 1"', '6\' 3"']
l = list(map(imp2met, i)) # Apply imp2met to each element in i
l = [imp2met(x) for x in i] # Same
Related