Notice that all of the data in the inner lists are represented as strings. You are to write the function convert_data, which should make modifications to the inner lists according to the following rules: If and only if a string represents a whole number (ex: '3' or '3.0'), convert the string to an int. If and only if a string represents a number that is not a whole number (ex: '3.14'), convert the string to a float. Otherwise, leave the string as a str. HINT: The provided helper function is_number may be used (attached image). QUESTION: Complete the docstring below. def convert_data(data: List[list]) -> None: """Convert each string in data to an int if and only if it represents a whole number, and a float if and only if it represents a number that is not a whole number. >>> d = [['abc', '123', '45.6', 'car', 'Bike']] >>> convert_data(d) >>> d [['abc', 123, 45.6, 'car', 'Bike']]
Notice that all of the data in the inner lists are represented as strings. You are to write the function convert_data, which should make modifications to the inner lists according to the following rules:
- If and only if a string represents a whole number (ex: '3' or '3.0'), convert the string to an int.
- If and only if a string represents a number that is not a whole number (ex: '3.14'), convert the string to a float.
- Otherwise, leave the string as a str.
HINT: The provided helper function is_number may be used (attached image).
QUESTION: Complete the docstring below.
def convert_data(data: List[list]) -> None:
"""Convert each string in data to an int if and only if it represents a
whole number, and a float if and only if it represents a number that is not
a whole number.>>> d = [['abc', '123', '45.6', 'car', 'Bike']]
>>> convert_data(d)
>>> d
[['abc', 123, 45.6, 'car', 'Bike']]
>>> d = [['ab2'], ['-123'], ['BIKES', '3.2'], ['3.0', '+4', '-5.0']]
>>> convert_data(d)
>>> d
[['ab2'], [-123], ['BIKES', 3.2], [3, 4, -5]]
"""
Step by step
Solved in 4 steps with 2 images