Question

In: Computer Science

Design a Python script that accepts as input a user-provided list and transforms it into a...

Design a Python script that accepts as input a user-provided list and transforms it into a different list in preparation for data analysis, the transformed list replaces each numeric element in the original list with its base-10 order of magnitude and replaces string elements with blanks.

Example:

This script accepts as input a user-provided list expected to contain non-zero numbers and strings. It then prints a transformed list replacing numbers with their order of magnitude, and strings as blanks.

Type list here, separated by commas, but without any brackets or parentheses: 45,0,"",,hfe8r34, 12.0,0.3744,3489,d3

Transformed list is [1, '', '', '', '', 1, 0, 3, '']

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

import math

#method to transform a value into base-10 order of magnitude if it is numeric
#else return an empty string

def transform(value):
    #using try except block, converting value to float and finding order_magnitude using
    #log10 function
   
try:
        order_magnitude=math.log10(float(value))
        #if no exception occurred, returning integer value of order_magnitude
       
return int(order_magnitude)
    except:
        #exception occurred, returning empty string
       
return ''


#asking and reading input
print('Type list here, separated by commas, but without any brackets or parentheses:')
str_array=input().split(',') #reads a line of text, splits by ',' to create a list
transformed_list=[] #empty list to store transformed elements
#ooping through each element in str_array

for i in str_array:
    #tranforming i and assigning to value
   
value=transform(i)
    #appending to transformed_list
   
transformed_list.append(value)
#printing transformed_list
print('Transformed list is',transformed_list)

#OUTPUT

Type list here, separated by commas, but without any brackets or parentheses:

45,0,"",,hfe8r34, 12.0,0.3744,3489,d3

Transformed list is [1, '', '', '', '', 1, 0, 3, '']


Related Solutions

PYTHON Write a program that accepts a range of input from the user and checks whether...
PYTHON Write a program that accepts a range of input from the user and checks whether the input data is sorted or not. If the data series is already sorted your program should print “True” or should print “False” otherwise. You should not use any sort function for this program. Input: How many numbers you want to input: 3 # user input 3 Input the number: 5 Input the number: 2 Input the number: 7 Output: False
In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input,...
In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input, and returns the highest sum of three neighboring elements in it. Write a main method that initializes the following five lists, gets the ThreeSum result for all of them using the above function, and prints the result to the screen. Example of the output: List 1: [4,5,4,5] , Three sum = 14 List 2: [7] , Three sum = 7 List 3: [ ]...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
⦁   Write a Bash script that prompts for user input and reads a string of text...
⦁   Write a Bash script that prompts for user input and reads a string of text from the user. If the user enters a no null (no empty) string, the script should prompt the user to re-enter again the string; otherwise should display the string entered “. ⦁   Given an array of the following four integers (3, 5, 13, 14); write a Bash script to display the second and all elements in the array.    ⦁   Write a short Bas...
Task 2.5: Write a script that will ask the user for to input a file name...
Task 2.5: Write a script that will ask the user for to input a file name and then create the file and echo to the screen that the file name inputted had been created 1. Open a new file script creafile.sh using vi editor # vi creafile.sh 2. Type the following lines #!/bin/bash echo ‘enter a file name: ‘ read FILENAME touch $FILENAME echo “$FILENAME has been created” 3. Add the execute permission 4. Run the script #./creafile.sh 5. Enter...
Write a Python script that takes an input image and output's the name of the dominant...
Write a Python script that takes an input image and output's the name of the dominant color in that image(i.e. red, green, blue).  
create a Python script that prompts the user for a title, description, and filename of your...
create a Python script that prompts the user for a title, description, and filename of your Python program and add the following to the bottom of the existing homepage: Add the post title with an emphasis Add the post description beneath the post title Create a hyperlink to the Python file using the filename input Create another hyperlink to the page you will create in the Web Showcase assignment
2) Write a C++ program that accepts a sentence as an input from the user. Do...
2) Write a C++ program that accepts a sentence as an input from the user. Do the following with the sentence. Please use C++ style string for this question. 1) Count the number of letters in the input 2) Change all lower case letters of the sentence to the corresponding upper case
Write a Flask app in which a Python script prompts the user to enter a string...
Write a Flask app in which a Python script prompts the user to enter a string and then that string, as entered, gets displayed on an otherwise blank HTML file, called output.html. Show the Python/Flask code and the html code of the output.html file, one below the other as part of your answer.
Post a Python program that accepts at least three values as input, performs some computation and...
Post a Python program that accepts at least three values as input, performs some computation and displays at least two values as the result. The program must include an if statement that performs different computations based on the value of one of the inputs. Include comments in your program that describe what the program does. Also post a screen shot of executing your program on at least two test cases.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT