我的 line.split 函数没有按预期工作,我不确定为什么



我试图制作一个多行列表,分成四行,并将四行中的每一行都放入自己的列表中,但每当我尝试时,我都会得到num2的错误是索引错误列表超出范围。我不太确定我的问题是什么。我希望有人能指出来。

下面是我的代码:
item = [
"Ham and Egg Sandwich $(15.75,10.0,44.0)",
"Bacon and Cheese Plate $(9.5,3.0,50.0)",
'Tuna Salad $(12.3,4.0,20.0)',
'Beef Soup $(9.0,2.0,30.0)',
'Spicy Beef Barbeque $(20.0,18.0,20.0)',
'Pork Barbeque $(18.0,12.0,35.0)',
'Oven Chicken Barbeque $(15.0,9.0,50.0)',
'Pulled Beef Barbeque Burger $(25.0,20.0,35.0)',
'House Salad $(5.0,2.5,46.0)',
'Pellegrino $(5.4,3.0,60.0)',
'White Wine $(7.5,5.0,40.0)',
'Red Wine $(11.0,4.0,71.0)'
]
for line in item:
itemInfo = line.split("$")
itemName = itemInfo[0].strip()
resplit = itemInfo[1].strip()
for line in resplit:
split = line.split(",")
num1 = split[0].strip()
num2 = split[1].strip()

请参阅原始代码的注释。

item = [
"Ham and Egg Sandwich $(15.75,10.0,44.0)",
"Bacon and Cheese Plate $(9.5,3.0,50.0)",
'Tuna Salad $(12.3,4.0,20.0)',
'Beef Soup $(9.0,2.0,30.0)',
'Spicy Beef Barbeque $(20.0,18.0,20.0)',
'Pork Barbeque $(18.0,12.0,35.0)',
'Oven Chicken Barbeque $(15.0,9.0,50.0)',
'Pulled Beef Barbeque Burger $(25.0,20.0,35.0)',
'House Salad $(5.0,2.5,46.0)',
'Pellegrino $(5.4,3.0,60.0)',
'White Wine $(7.5,5.0,40.0)',
'Red Wine $(11.0,4.0,71.0)'
]
for line in item:
# line = an entry from the list
# Example: "Ham and Egg Sandwich $(15.75,10.0,44.0)"
itemInfo = line.split("$")
# itemInfo  = list
# Example ['Ham and Egg Sandwich ', '(15.75,10.0,44.0)']
itemName = itemInfo[0].strip()
# itemName = str Example: 'Ham and Egg Sandwich'
resplit = itemInfo[1].strip()
# resplit = str Example: '(15.75,10.0,44.0)'
for line in resplit: # So line = str Example: '(' one character.
split = line.split(",")
num1 = split[0].strip() # Is one character '('
num2 = split[1].strip() # Thus index error, no more characters.

下面的代码完成了我所理解的目标。

item = [
"Ham and Egg Sandwich $(15.75,10.0,44.0)",
"Bacon and Cheese Plate $(9.5,3.0,50.0)",
'Tuna Salad $(12.3,4.0,20.0)',
'Beef Soup $(9.0,2.0,30.0)',
'Spicy Beef Barbeque $(20.0,18.0,20.0)',
'Pork Barbeque $(18.0,12.0,35.0)',
'Oven Chicken Barbeque $(15.0,9.0,50.0)',
'Pulled Beef Barbeque Burger $(25.0,20.0,35.0)',
'House Salad $(5.0,2.5,46.0)',
'Pellegrino $(5.4,3.0,60.0)',
'White Wine $(7.5,5.0,40.0)',
'Red Wine $(11.0,4.0,71.0)'
]
for line in item:
# line = an entry from the list
# Example: "Ham and Egg Sandwich $(15.75,10.0,44.0)"
itemInfo = line.split("$")
# itemInfo  = list
# Example ['Ham and Egg Sandwich ', '(15.75,10.0,44.0)']
itemName = itemInfo[0].strip()
# itemName = str Example: 'Ham and Egg Sandwich'
# itemInfo[1] = str Example: Example: '(15.75,10.0,44.0)'
numbers = itemInfo[1][1:-1].split(',')  # Drop parentheses and split
num1 = numbers[0]
num2 = numbers[1]
num3 = numbers[2]
print(f'{itemName=}, {num1=},{num2=},{num3=}')

输出
itemName='Ham and Egg Sandwich', num1='15.75',num2='10.0',num3='44.0'
itemName='Bacon and Cheese Plate', num1='9.5',num2='3.0',num3='50.0'
itemName='Tuna Salad', num1='12.3',num2='4.0',num3='20.0'
itemName='Beef Soup', num1='9.0',num2='2.0',num3='30.0'
itemName='Spicy Beef Barbeque', num1='20.0',num2='18.0',num3='20.0'
itemName='Pork Barbeque', num1='18.0',num2='12.0',num3='35.0'
itemName='Oven Chicken Barbeque', num1='15.0',num2='9.0',num3='50.0'
itemName='Pulled Beef Barbeque Burger', num1='25.0',num2='20.0',num3='35.0'
itemName='House Salad', num1='5.0',num2='2.5',num3='46.0'
itemName='Pellegrino', num1='5.4',num2='3.0',num3='60.0'
itemName='White Wine', num1='7.5',num2='5.0',num3='40.0'
itemName='Red Wine', num1='11.0',num2='4.0',num3='71.0'

相关内容

  • 没有找到相关文章

最新更新