-1

So I scraped a list of heights from a website, and I am trying to find an average of these heights in ft and in. My problems is that the str.replace() is not working here and I am not sure why. (It doesn't give me any error messages) Any suggestions on how to fix this?

> print(maleswimLIST)
> ['6\'5"', '5\'10"', '5\'9"', '6\'2"', '6\'5"', '5\'10"', '5\'8"', '5\'8"', '5\'10"', '5\'9"', '5\'7"', '6\'2"', '6\'0"', '6\'0"', '5\'11"', '6\'3"', '5\'10"', '5\'10"', '5\'5"', '6\'1"', '5\'9"', '5\'7"', '6\'3"', '6\'5"', '5\'10"', '5\'9"', '6\'2"', '6\'5"', '5\'10"', '5\'8"', '5\'8"', '5\'10"', '5\'9"', '5\'7"', '6\'2"', '6\'0"', '6\'0"', '5\'11"', '6\'3"', '5\'10"', '5\'10"', '5\'5"', '6\'1"', '5\'9"', '5\'7"', '6\'3"']
> for x in maleswimLIST:
    x.replace("\'",".")
    print(x)

> 6'5"
5'10"
5'9"
6'2"
6'5"
5'10"
5'8"
5'8"
5'10"
5'9"
5'7"
6'2"
6'0"
6'0"
5'11"
6'3"
5'10"
5'10"
5'5"
6'1"
5'9"
5'7"
6'3"
6'5"
5'10"
5'9"
6'2"
6'5"
5'10"
5'8"
5'8"
5'10"
5'9"
5'7"
6'2"
6'0"
6'0"
5'11"
6'3"
5'10"
5'10"
5'5"
6'1"
5'9"
5'7"
6'3"
Masklinn
  • 34,759
  • 3
  • 38
  • 57
조혜연
  • 5
  • 1

4 Answers4

1

My problems is that the str.replace() is not working here and I am not sure why. (It doesn't give me any error messages

str.replace doesn't work in-place, it returns a new string with the replacement applied. It also doesn't really care how many replacements it performs (whether 0 ot 1000), it will only raise errors if the input is nonsensical e.g. if you provide integers.

Here you would normally create a brand new list containing the transformed / converted values (using either a procedural for loop or a list comprehension) rather than try and update the values in-place.

Although note that your replacements here don't really make sense: imperial units are not decimal, there are not 10 inches to a foot but 12, so 5ft9 is 5.75 ft not 5.9ft.

Masklinn
  • 34,759
  • 3
  • 38
  • 57
0

You haven't changed the value of x. str.replace() does not change the variable in place, you need to reassign the return value to x so that it takes effect when you call print(x) thereafter.

maleswimLIST = [x.replace("\'",".") for x in maleswimLIST]
ApplePie
  • 8,814
  • 5
  • 39
  • 60
0

The replace function you call on the string x doesn't change the value in place, you need to set it again:

result = []
for x in maleswimLIST:
    result.append(x.replace("'", ".")
print(result)

Or as a list comprehension which is more pythonic:

result = [x.replace("'", ".") for x in maleswimLIST]

BTW: there is no need to escape the apostrophe ' in your replace function.

Michael Anckaert
  • 853
  • 8
  • 12
0

Problem Explanation

The replace method returns the new value, and the original value stays intact (strings are immutable)

Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

Code Fix

Using list comprehension, iterating over the list, and collecting the replaced value:

male_swim_list =  ['6\'5"', '5\'10"', '5\'9"', '6\'2"', '6\'5"', '5\'10"', '5\'8"', '5\'8"', '5\'10"', '5\'9"', '5\'7"', '6\'2"', '6\'0"', '6\'0"', '5\'11"', '6\'3"', '5\'10"', '5\'10"', '5\'5"', '6\'1"', '5\'9"', '5\'7"', '6\'3"', '6\'5"', '5\'10"', '5\'9"', '6\'2"', '6\'5"', '5\'10"', '5\'8"', '5\'8"', '5\'10"', '5\'9"', '5\'7"', '6\'2"', '6\'0"', '6\'0"', '5\'11"', '6\'3"', '5\'10"', '5\'10"', '5\'5"', '6\'1"', '5\'9"', '5\'7"', '6\'3"']

male_swim_list = [x.replace("\'", ".") for x in male_swim_list]

print(male_swim_list)

Output:

['6.5"', '5.10"', '5.9"', '6.2"', '6.5"', '5.10"', '5.8"', '5.8"', '5.10"', '5.9"', '5.7"', '6.2"', '6.0"', '6.0"', '5.11"', '6.3"', '5.10"', '5.10"', '5.5"', '6.1"', '5.9"', '5.7"', '6.3"', '6.5"', '5.10"', '5.9"', '6.2"', '6.5"', '5.10"', '5.8"', '5.8"', '5.10"', '5.9"', '5.7"', '6.2"', '6.0"', '6.0"', '5.11"', '6.3"', '5.10"', '5.10"', '5.5"', '6.1"', '5.9"', '5.7"', '6.3"']

Some Advice

At least since PEP 8 it's a convention to name variables with lower case with underscores. It doesn't change the behavior of the code, yet it is more readable for your future teammates.

Aviv Yaniv
  • 6,188
  • 3
  • 7
  • 22