Python) to align in feet, inches

Asked 2 years ago, Updated 2 years ago, 93 views

Hello, I'm a freshman who just started programming!

It's just that I'm sorting people's personal information by height. It's okay if the key is written in cm, but it's written in ft and in. ft doesn't matter, but the alignment at in becomes weird. 6ft 1in // 5ft 4in // 5ft 10in

If you sort this out, it should be 6ft 1in // 5ft 10in // 5ft 4in. 6ft 1in // 5ft 4in // 5ft 10in This is how it aligns.

I thought about converting in to cm, but I don't know how to implement it.

Masters, help me!

def process_roster_files(path_to_directory):
filenames = glob('lab03/rosters/*.txt')
summ = []
name = ""
height = ""
for item in filenames:
    myFile = open(item, 'r')
    key = myFile.readlines()
    for line in key:
        if 'Name' in line:            
            name = line.lstrip('Name:\t').rstrip('\n')

        if 'Height' in line:
            height = line.lstrip('Height:\t').rstrip('\n')
            summ.append([name, height])


summ.sort(reverse=False)
output_file = open('hello.csv', 'w')
height_list_file = 'hello.csv'
for i in range(0,6):
    output_file.write(summ[i][0]+', '+summ[i][1] + '\n')

output_file.close()
return height_list_file

This is the code I wrote.

python sorting

2022-09-21 16:10

2 Answers

Create a function that generates an alignment key and use it.

import re

def length_to_num(val):
    """ Converts the string entered as Xft Yin in ft """
    # Extract the number before ft and the number before in using the regular expression
    m = re.match(r"(\d+)ft\s*(\d+)in",val)
    # m.group(0) is the full regular expression matched string
    # m.group(1) is a string matched to the first ()
    # m.group(2) is a string matched to the second ()
    # 1 ft = 12 in, reflecting this
    return int(m.group(1)) + float(m.group(2))/12

Try sorting using the functions above. Here is an example of a test:

import re

def length_to_num(val):
    """ Converts the string entered as Xft Yin in ft """
    # Extract the number before ft and the number before in using the regular expression
    m = re.match(r"(\d+)ft\s*(\d+)in",val)
    # m.group(0) is the full regular expression matched string
    # m.group(1) is a string matched to the first ()
    # m.group(2) is a string matched to the second ()
    # 1 ft = 12 in, reflecting this
    return int(m.group(1)) + float(m.group(2))/12

li = ["3ft 6in","3ft 9in","3ft 10in","3ft 1in","5ft 10in"]
li.sort(key=length_to_num, reverse=False)
# When sorting, passes a function to the key factor that changes the item to the key to sort.
print(li[0])
print(li[1])


2022-09-21 16:10

You can also implement length_to_num() in the previous minutes without using a regular expression. It is a code that takes into account if the data file has decimal points, only ft, or if the ft or in units are incorrectly written.

def str_to_num(arg):
    #return int(''.join(n for n in arg if n.isdigit()))
    return float(''.join(n for n in arg if n in '0123456789.'))

def get_only_nums(str):
    return [str_to_num(x) for x in str.split()]

def length_to_num(str):
    nums = get_only_nums(str)
    # # print(nums)
    if len(nums) == 1:
        return nums[0] * 12
    else:
        return nums[0] * 12 + nums[1]

print(length_to_num("   6ft    5.3in    "))
print(length_to_num(" 6ft"))


2022-09-21 16:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.