You are given a list of strings consisting of grocery items, with prices in parentheses. Return a list of prices in float format. Use RegExp to solve.
Question
You are given a list of strings consisting of grocery items, with prices in parentheses. Return a list of prices in float format. Use RegExp to solve.
Solution
Sure, here is a Python solution using regular expressions (RegExp):
import re
def extract_prices(grocery_list):
prices = []
for item in grocery_list:
match = re.search(r'\((.*?)\)', item)
if match:
price = match.group(1)
prices.append(float(price))
return prices
Here's how it works:
- We import the
remodule, which allows us to use regular expressions. - We define a function
extract_pricesthat takes a list of grocery items as input. - We create an empty list
pricesto store the prices we extract. - We iterate over each item in the grocery list.
- We use the
re.searchfunction to find a match for the regular expressionr'\((.*?)\)'. This expression matches any text enclosed in parentheses. - If a match is found, we use the
groupmethod to extract the text inside the parentheses. This is the price, but it's still a string. - We convert the price to a float and append it to the
priceslist. - After we've processed all the items in the grocery list, we return the
priceslist.
Similar Questions
A data analyst discovers that their database has recognized product price data as text strings. What SQL function can the analyst use to convert the text strings to floats? 1 pointCASTSUBSTRTRIMLENGTH
In a retail store software , get the price of each stock as input with its price and print in the following format input:dinnerset10000mattress24000Invertor30000 output: dinnerset10,000mattress24,000Invertor30,000
Use regular expressions to extract data from strings
Objective:To work with OperatorsScenario:A new stationary shop has been opened in the city. The owner asks his accountant to take the list of items sold in the store. The list should contain the details of the items and their costs. Help the accountant to generate the prince list by writing a Python program. Generate list with just 4 products - A4sheets, pen, pencil and eraser and get the price of the items from the user. Please refer the sample input and output statements for more clarifications.Guidelines:The amount must be displayed with 2 decimal places. On entering the product price to be a negative number, the program should display the message " Invalid input" and stop the program. Sample Input 1 :Cost of A4sheet:40Cost of pen:20Cost of pencil:10Cost of eraser:5Sample Output 1 :Items DetailsA4sheet:40.00Pen:20.00Pencil:10.00Eraser:5.00Sample Input 2 :Cost of A4sheet:40Cost of pen:20Cost of pencil:10Cost of eraser:-5Sample Output 2 :Invalid inputSample Input 3 :Cost of A4sheet:86Cost of pen:10Cost of pencil:-34Sample Output 3 :Invalid input
Problem StatementMaria loves shopping and is curious to know the total cost of her favorite items. She intends to purchase t items, each priced at p rupees, with a progressive increase of 5 rupees for every subsequent item.Write a program that takes the price of one item (p) and the number of items (t) as input, calculates the total cost using the formula (t/2) * (2p + (t-1) * 5), and displays the result.Input format :The first line of input consists of a float value p, representing the price of an item.The second line of input consists of an integer t, representing the number of items.Output format :The output displays "Rs. " followed by a float value, representing the total cost in rupees, rounded off to two decimal places.Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:10.0 ≤ p ≤ 1000.01 ≤ t ≤ 100Sample test cases :Input 1 :10.03Output 1 :Rs. 45.00Input 2 :720.056Output 2 :Rs. 48020.00
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.