Question

In: Computer Science

def change_type(info: List[list]) -> None: """ Modify info to a float if and only if it...

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.

Solutions

Expert Solution

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


Related Solutions

from partition import partition def quicksort(a: list, l: int, u: int) -> None: '''Sort the given...
from partition import partition def quicksort(a: list, l: int, u: int) -> None: '''Sort the given list a in non-descending order. Precondition: 0 <= l and u < len(a)''' if l < u: mid = (l + u) // 2 three = [a[l], a[mid], a[u]] three.sort() if three[1] == a[l]: pivot_loc = l elif three[1] == a[u]: pivot_loc = u else: pivot_loc = mid a[u], a[pivot_loc] = a[pivot_loc], a[u] pivot = a[u] i = partition(a, l, u - 1, pivot)...
class Board: def __init__(self): self.pieces = [[None for i in range(8)] for j in range(8)] def...
class Board: def __init__(self): self.pieces = [[None for i in range(8)] for j in range(8)] def __str__(self): s = "" for j in range(7, -1, -1): #for each row for i in range(8): # for each column if self.pieces[j][i] is None: # if self.pieces[j][i] is None s += "." # populate the row with '.' val for each column else: s += self.pieces [j][i].get_symbol() s += "\n" #after each row add a new line return s # return after iterating...
''' File: pyPatientLL.py Author: JD ''' class Node: #ADT        def __init__(self, p = None):             ...
''' File: pyPatientLL.py Author: JD ''' class Node: #ADT        def __init__(self, p = None):              self.name = ""              self.ss = self.age = int(0)              self.smoker = self.HBP = self.HFD = self.points = int(0)              self.link = None              #if list not empty              if p != None:                     p.link = self        ptrFront = ptrEnd = None choice = int(0) def menu():        print( "\n\tLL Health Clinic\n\n")        print( "1. New patient\n")        print( "2. View patient by...
Python Q1. def silly(stuff, more_stuff=None): data = [] for i, thing in enumerate(stuff): if thing >=...
Python Q1. def silly(stuff, more_stuff=None): data = [] for i, thing in enumerate(stuff): if thing >= 0 or i < 2: data.append(thing + i) print(len(stuff)) if more_stuff is not None: data += more_stuff print(data) Write a Python statement that calls silly and results in the output (from within silly): 4 [5, 3, 1, 5, -3] stuff = [5, 2, 0, 2] more_stuff = [-3] silly(stuff, more_stuff) Q2. Function silly2 is defined as: def silly2(nums, indices, extra=None): print(len(nums), len(indices)) new_indices =...
Python class LinkedNode: # DO NOT MODIFY THIS CLASS # __slots__ = 'value', 'next' def __init__(self,...
Python class LinkedNode: # DO NOT MODIFY THIS CLASS # __slots__ = 'value', 'next' def __init__(self, value, next=None): """ DO NOT EDIT Initialize a node :param value: value of the node :param next: pointer to the next node in the LinkedList, default is None """ self.value = value # element at the node self.next = next # reference to next node in the LinkedList def __repr__(self): """ DO NOT EDIT String representation of a node :return: string of value """...
Python file def calci(): grade = float(input("Enter your grade:")) while (grade <= 4): if (grade >=...
Python file def calci(): grade = float(input("Enter your grade:")) while (grade <= 4): if (grade >= 0.00 and grade <= 0.67): print("F") break elif (grade >= 0.67 and grade <= 0.99): print("D-") break elif (grade >= 1.00 and grade <= 1.32): print("D") break elif (grade >= 1.33 and grade <= 1.66): print("D+") break elif (grade >= 1.67 and grade <= 1.99): print("C-") break elif (grade >= 2.00 and grade <= 2.32): print("C") break elif (grade >= 2.33 and grade <=...
C++ Given vector<float> vec; Using a ranged for loop, modify each value in vector to cube...
C++ Given vector<float> vec; Using a ranged for loop, modify each value in vector to cube and subtract 12.5
def main():     print('This program determines whether you have hypertension.')     systolic_pressure = float(input('Enter your systolic...
def main():     print('This program determines whether you have hypertension.')     systolic_pressure = float(input('Enter your systolic pressure: '))     diastolic_pressure = float(input('Enter your diastolic pressure: '))     # insert statement here to call the hypertension_tester function def hypertension_tester(dp, sp):     if sp >= 140 or dp >= 90:         print('You have hypertension')     else:         print('You do not have hypertension') main() A person has hypertension if systolic pressure is 140 or above or diastolic pressure is 90 or above. Which of the...
Modify this linked list code to work with string. Insert the following items into the list...
Modify this linked list code to work with string. Insert the following items into the list and display the list. The items are: Pepsi, Coke, DrPepper, Sprite, Fanta. Insert them in that order. Display the list. Then delete DrPepper and redisplay the list. Then insert 7-UP and redisplay the list. Then append Water and redisplay the list. c++ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ #include <iostream> using namespace std; class ListNode { public:     int value;     ListNode *next;     ListNode(int nodeValue) {       value...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return the length of the longest chain of 1's that start from the beginning. You MUST use a while loop for this! We will check. >>> longest_chain([1, 1, 0]) 2 >>> longest_chain([0, 1, 1]) 0 >>> longest_chain([1, 0, 1]) 1 """ i = 0 a = [] while i < len(submatrix) and submatrix[i] != 0: a.append(submatrix[i]) i += 1 return sum(a) def largest_rectangle_at_position(matrix: List[List[int]], x:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT