将语音识别器生成的一行字符串中的字符串和整数添加到 2 个单独的列表
Add string and integer from one line string generate by speech recognizer to 2 separate list
我有一些来自语音识别器的字符串,其值是随机的,但形式相同 (x,y...n)
MyOrder = 'orange 2 grape 3 apple 4 mango 5 banana 1'
如何像MyOrder一样以正确的顺序将字符串和整数添加到单独的列表中?或在一行中沿着这些字符串过滤 x、y(字符串、整数或反之亦然),例如:
fruit_cart = ['orange', 'grape', 'apple', 'mango', 'banana']
fruit_quantity = [2,3,4,5,1]
我尝试使用条件 if 语句添加列表字符串,但顺序与 MyOrder 字符串不同。
import numpy as np
import re
import speech_recognition as sr
fruit_cart = []
fruit_quantity = []
fruit_price = []
gross_price = []
recognizer = sr.Recognizer()
with sr.Microphone() as source2:
recognizer.adjust_for_ambient_noise(source2, duration=0.9)
audio2 = recognizer.listen(source2)
MyOrder = recognizer.recognize_google(audio2)
MyOrder = MyOrder.lower()
print("You said: " +MyOrder)
if "apple" in(MyOrder) :
fruit_cart.append("apple")
fruit_price.append(5)
if "banana" in(MyOrder) :
fruit_cart.append("banana")
fruit_price.append(7)
if "grape" in (MyOrder):
fruit_cart.append("grape")
fruit_price.append(9)
if "mango" in (MyOrder):
fruit_cart.append("mango")
fruit_price.append(6)
if "orange" in (MyOrder):
fruit_cart.append("orange")
fruit_price.append(8)
q = [int(x) for x in re.findall('(\d+)', MyOrder)]
fruit_quantity.extend(q)
total_fruit = len(fruit_cart)
print(fruit_cart)
print(fruit_quantity)
print(fruit_price)
print(total_fruit)
gross_price = np.multiply(fruit_quantity,fruit_price)
total_price = 0
for b in range(total_fruit):
print("%i \t %s\t %s \t\t%i\t\t %i" % (b + 1, fruit_cart[b], fruit_price[b], fruit_quantity[b], gross_price[b]))
total_price = total_price + gross_price[b]
tax = total_price * 10/100
final_price = total_price + tax
顺便说一下,我是 python 的新手
您的代码中的问题是它的工作顺序与 ifs 语句相同。因此,如果苹果出现在 MyOrder
中,它们将始终排在第一位,然后是香蕉、葡萄等。您想按照在 MyOrder
中出现的顺序填写列表。
我会将字符串转换为列表并迭代列表而不是使用 if 语句。对于价格,我会定义一个包含每种水果价格的字典。以下是我的做法:
prices = {"apple": 5, "banana": 7, "grape": 9, "mango": 6, "orange": 8}
with sr.Microphone() as source2:
recognizer.adjust_for_ambient_noise(source2, duration=0.9)
audio2 = recognizer.listen(source2)
MyOrder = recognizer.recognize_google(audio2)
MyOrder = MyOrder.lower()
print("You said: ", MyOrder)
word_list = MyOrder.split(" ")
fruit_cart = word_list[::2]
fruit_quantity = [int(n) for n in word_list[1::2]]
fruit_price = [prices[fruit] for fruit in fruit_cart]
total_fruit = len(fruit_cart)
gross_price = np.multiply(fruit_quantity,fruit_price)
total_price = 0
for b in range(total_fruit):
print("%i \t %s\t Rp %s \t\t%i\t\t Rp %i" % (b + 1, fruit_cart[b], fruit_price[b], fruit_quantity[b], gross_price[b]))
total_price = total_price + gross_price[b]
tax = total_price * 10/100
final_price = total_price + tax
我有一些来自语音识别器的字符串,其值是随机的,但形式相同 (x,y...n)
MyOrder = 'orange 2 grape 3 apple 4 mango 5 banana 1'
如何像MyOrder一样以正确的顺序将字符串和整数添加到单独的列表中?或在一行中沿着这些字符串过滤 x、y(字符串、整数或反之亦然),例如:
fruit_cart = ['orange', 'grape', 'apple', 'mango', 'banana']
fruit_quantity = [2,3,4,5,1]
我尝试使用条件 if 语句添加列表字符串,但顺序与 MyOrder 字符串不同。
import numpy as np
import re
import speech_recognition as sr
fruit_cart = []
fruit_quantity = []
fruit_price = []
gross_price = []
recognizer = sr.Recognizer()
with sr.Microphone() as source2:
recognizer.adjust_for_ambient_noise(source2, duration=0.9)
audio2 = recognizer.listen(source2)
MyOrder = recognizer.recognize_google(audio2)
MyOrder = MyOrder.lower()
print("You said: " +MyOrder)
if "apple" in(MyOrder) :
fruit_cart.append("apple")
fruit_price.append(5)
if "banana" in(MyOrder) :
fruit_cart.append("banana")
fruit_price.append(7)
if "grape" in (MyOrder):
fruit_cart.append("grape")
fruit_price.append(9)
if "mango" in (MyOrder):
fruit_cart.append("mango")
fruit_price.append(6)
if "orange" in (MyOrder):
fruit_cart.append("orange")
fruit_price.append(8)
q = [int(x) for x in re.findall('(\d+)', MyOrder)]
fruit_quantity.extend(q)
total_fruit = len(fruit_cart)
print(fruit_cart)
print(fruit_quantity)
print(fruit_price)
print(total_fruit)
gross_price = np.multiply(fruit_quantity,fruit_price)
total_price = 0
for b in range(total_fruit):
print("%i \t %s\t %s \t\t%i\t\t %i" % (b + 1, fruit_cart[b], fruit_price[b], fruit_quantity[b], gross_price[b]))
total_price = total_price + gross_price[b]
tax = total_price * 10/100
final_price = total_price + tax
顺便说一下,我是 python 的新手
您的代码中的问题是它的工作顺序与 ifs 语句相同。因此,如果苹果出现在 MyOrder
中,它们将始终排在第一位,然后是香蕉、葡萄等。您想按照在 MyOrder
中出现的顺序填写列表。
我会将字符串转换为列表并迭代列表而不是使用 if 语句。对于价格,我会定义一个包含每种水果价格的字典。以下是我的做法:
prices = {"apple": 5, "banana": 7, "grape": 9, "mango": 6, "orange": 8}
with sr.Microphone() as source2:
recognizer.adjust_for_ambient_noise(source2, duration=0.9)
audio2 = recognizer.listen(source2)
MyOrder = recognizer.recognize_google(audio2)
MyOrder = MyOrder.lower()
print("You said: ", MyOrder)
word_list = MyOrder.split(" ")
fruit_cart = word_list[::2]
fruit_quantity = [int(n) for n in word_list[1::2]]
fruit_price = [prices[fruit] for fruit in fruit_cart]
total_fruit = len(fruit_cart)
gross_price = np.multiply(fruit_quantity,fruit_price)
total_price = 0
for b in range(total_fruit):
print("%i \t %s\t Rp %s \t\t%i\t\t Rp %i" % (b + 1, fruit_cart[b], fruit_price[b], fruit_quantity[b], gross_price[b]))
total_price = total_price + gross_price[b]
tax = total_price * 10/100
final_price = total_price + tax