Question

[Python] I have the following function that removes all non-numerical characters from a string: d...

[Python]

I have the following function that removes all non-numerical characters from a string:

def remove_non_numeric(s):
  
seq_type= type(s)
return seq_type().join(filter(seq_type.isdigit, s))

And I need help implementing it into this function:

def list_only_numbers( a_list ) :

# Create a new empty list.

# Using a loop (or a list comprehension)

# 1) call remove_non_numeric with a list element

# 2) if the return value is not the empty string, convert

# the string to either int or float (if it contains a decimal

# point) and append the value to the new list.

# 3) if the return value is the empty string, do not

# append anything to the new list.

# Return the new list.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def list_only_numbers (a_list):
#Create a new list
l = []
n = 0

#USing a loop
for i in a_list:

#Call remove_non_numeric
s = remove_non_numeric (a_list[i])

#If the return value is not the empty string
if (s != ''):
#if there is a decimal in the string then convert it to float
if ('.' in s):
n = float(s)
#else convert it to integer
else:
n = int(s)
l.append(n)
return l
  

def list_only_numbers (a list) #create a new list #04ing a loop for i in a list: #Call remove non numeric s - remove non nume

Add a comment
Know the answer?
Add Answer to:
[Python] I have the following function that removes all non-numerical characters from a string: d...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • *************PERL************ The clamp function... Removes all whitespace characters from the beginning and end of a string....

    *************PERL************ The clamp function... Removes all whitespace characters from the beginning and end of a string. Removes a newline character from the end of a string Removes the last character in a string; regardless of what it is. Limits an integer to a maximum or minimum value

  • HW7.26. Return a CSV string from a list of numbers The function below takes a single...

    HW7.26. Return a CSV string from a list of numbers The function below takes a single parameter, a list of numbers called number_list. Complete the function to return a string of the provided numbers as a series of comma separate values (CSV). For example, if the function was provided the argument [22, 33, 44], the function should return '22,33,44'. Hint: in order to use the join function you need to first convert the numbers into strings, which you can do...

  • I need help for this assignment. Thank you!! # 5555555555555555555555555555555555555555555555555 # Returns the longest word (which has six or more # characters and contains the letter, e) from # the...

    I need help for this assignment. Thank you!! # 5555555555555555555555555555555555555555555555555 # Returns the longest word (which has six or more # characters and contains the letter, e) from # the parameter list - 4 marks Os Define the get_longest_e_word() function which is passed a list of strings as a parameter. The function returns the word in the list which has the most characters (i.e., the longest word) BUT only words which have 6 or more characters and contain the letter...

  • For this task you're going to write a function that joins strings from a list. The function calle...

    I need help with this python programming assignment please double check the indentations For this task you're going to write a function that joins strings from a list. The function called join ) has one formal parameter and one optional parameter. Recall that an optional parameter is like end or sep in the print( function. You don't have to use it because there's a default value. For the print () function, e.g., \'n' (newline) is the default value for end....

  • Write a Python program (remove_first_char.py) that removes the first character from each item in a list...

    Write a Python program (remove_first_char.py) that removes the first character from each item in a list of words. Sometimes, we come across an issue in which we require to delete the first character from each string, that we might have added by mistake and we need to extend this to the whole list. Having shorthands (like this program) to perform this particular job is always a plus. Your program should contain two functions: (1) remove_first(list): This function takes in a...

  • 3. Write Python statements that will do the following: a) Input a string from the user....

    3. Write Python statements that will do the following: a) Input a string from the user. *** Print meaningful messages along with your output.* b) Print the length of the String. Example input: The sky is blue. Correct output: The length of the string is 16 Incorrect output: 16 c) Print the first character of the string. d) Print the last character of the string. 4. Save the program. Double-check the left edge for syntax errors or warning symbols before...

  • For Python-3 I need help with First creating a text file named "items.txt" that has the...

    For Python-3 I need help with First creating a text file named "items.txt" that has the following data in this order: Potatoes Tomatoes Carrots. Write a python program that 1. Puts this as the first line... import os 2. Creates an empty list 3. Defines main() function that performs the tasks listed below when called. 4. Put these two lines at the top of the main function... if os.path.exists("costlist.txt"): os.remove("costlist.txt") Note: This removes the "costlist.txt" file, if it exists. 5....

  • Python question. i have to start from an empty linked list, using the method addNodeEnd() to...

    Python question. i have to start from an empty linked list, using the method addNodeEnd() to add the nodes containing the values (3*i+5)%17, where i is from 0 to 10. Then print the values of all the nodes in this linked list to the screen. This is the code that i created right here and i need help checking if i made any mistakes thanks! The code is below: class Node: def __init__(self, data): self.data = data self.next = None...

  • In Python How do I make the following function below iterate for 20 years. (1996 to...

    In Python How do I make the following function below iterate for 20 years. (1996 to 2016) def getComplaints(year, make, model): url0 = #url url = url0.format(year,make,model) s = requests.get(url).text # this is a CSV string df = pandas.read_csv(StringIO(s)) # use pandas to parse the CSV complaints = df.to_dict('records') # convert to list of dicts return complaints I want to change this function so it is able to search from the year 1996 all the way to 2016 and then...

  • How to do this code? HW13.11. Create an HTML list from a Python list The function...

    How to do this code? HW13.11. Create an HTML list from a Python list The function below takes one parameter: a list, that contains only strings. Complete the function to create a unordered HTML list, as a string, and returns it. An empty list should return an empty HTML list with no list items. Do not add any extraneous whitespace to the HTML. For example, if given the list ["one", "two"], the function should return "<ul><li>one</li><li>two</li> </ul>". student.py IT TI...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT