In: Computer Science
def change_type(info: List[list]) -> None:
""" Modify info to a float if and only if it represents a number
that is not
a whole number(a float), and convert info to an int if and only if
it represents a
whole number, keep everythingelse as a string.
>>> i = [['apple', '888', 'School', '111.1']]
>>> change_type(i)
>>> i
[['apple', 888, 'School', '111.1']]
Please help! Write as little code as possible!
Please only use if statements, nested loop and lists to solve this question.
def change_type(info):
newList = []
# iterating list through sub lists
for subList in info:
# initializing empty list
temp = []
# iterating though every element in sub list
for element in subList:
# checking if number is numeric or not
# if numeric converted into integer and appended
# else numeric is converted to float and appended
if element.isnumeric():
if int(element) >= 0:
temp.append(int(element))
else:
temp.append(float(element))
else:
temp.append(element)
# appending sublist to newList
newList.append(temp)
# returning newlist
return newList
Code
Output