我是说直接把字典里的内容变成字符串
然后解析这个字符串为字典
然后才是代码中的计算,我是让你修改代码
将输出字符串解析为字典的九种方法:
方法一:使用正则表达式
import re
parsed_item_prices = {}
current_item = None
for line in output_string.split('\n'):
if line.startswith("第"):
current_item = re.search(r':(.+)', line).group(1)
parsed_item_prices[current_item] = {}
elif not line.startswith("-") and current_item:
parts = line.split(":")
other_item = parts[0].strip()
ratio = float(parts[1])
parsed_item_prices[current_item][other_item] = ratio
方法二:使用字符串操作和切片
parsed_item_prices = {}
current_item = None
lines = output_string.split('\n')
for i in range(len(lines)):
line = lines[i]
if line.startswith("第"):
current_item = line.split(":")[1].strip()
parsed_item_prices[current_item] = {}
elif not line.startswith("-"):
parts = line.split(":")
other_item = parts[0].strip()
ratio = float(parts[1])
parsed_item_prices[current_item][other_item] = ratio
方法三:使用字符串操作和索引
parsed_item_prices = {}
current_item = None
lines = output_string.split('\n')
for i in range(len(lines)):
line = lines[i]
if line.startswith("第"):
current_item = line[line.find(":")+1:].strip()
parsed_item_prices[current_item] = {}
elif not line.startswith("-"):
parts = line.split(":")
other_item = parts[0].strip()
ratio = float(parts[1])
parsed_item_prices[current_item][other_item] = ratio
方法四:使用字符串操作和分割
parsed_item_prices = {}
current_item = None
for line in output_string.split('\n'):
if line.startswith("第"):
current_item = line.split(":")[1].strip()
parsed_item_prices[current_item] = {}
elif not line.startswith("-"):
parts = line.split(":")
other_item = parts[0].strip()
ratio = float(parts[1])
parsed_item_prices[current_item][other_item] = ratio
方法五:使用字符串操作和判断
parsed_item_prices = {}
current_item = None
lines = output_string.split('\n')
for line in lines:
if line.startswith("第"):
current_item = line.split(":")[1].strip()
parsed_item_prices[current_item] = {}
elif not line.startswith("-"):
parts = line.split(":")
if len(parts) == 2:
other_item = parts[0].strip()
ratio = float(parts[1])
parsed_item_prices[current_item][other_item] = ratio
方法六:使用字符串操作和列表推导
parsed_item_prices = {}
current_item = None
lines = output_string.split('\n')
for line in lines:
if line.startswith("第"):
current_item = line.split(":")[1].strip()
parsed_item_prices[current_item] = {}
elif not line.startswith("-"):
parts = [part.strip() for part in line.split(":")]
if len(parts) == 2:
parsed_item_prices[current_item][parts[0]] = float(parts[1])
方法七:使用字符串操作和字典get方法
parsed_item_prices = {}
current_item = None
lines = output_string.split('\n')
for line in lines:
if line.startswith("第"):
current_item = line.split(":")[1].strip()
parsed_item_prices[current_item] = {}
elif not line.startswith("-"):
parts = line.split(":")
other_item = parts[0].strip()
ratio = float(parts[1]) if len(parts) == 2 else None
if ratio is not None:
parsed_item_prices[current_item][other_item] = ratio
方法八:使用字符串操作和异常处理
parsed_item_prices = {}
current_item = None
lines = output_string.split('\n')
for line in lines:
try:
if line.startswith("第"):
current_item = line.split(":")[1].strip()
parsed_item_prices[current_item] = {}
elif not line.startswith("-"):
parts = line.split(":")
other_item = parts[0].strip()
ratio = float(parts[1])
parsed_item_prices[current_item][other_item] = ratio
except IndexError:
continue
方法九:使用列表操作和迭代器
parsed_item_prices = {}
current_item = None
lines = iter(output_string.split('\n'))
for line in lines:
if line.startswith("第"):
current_item = line.split(":")[1].strip()
parsed_item_prices[current_item] = {}
elif not line.startswith("-"):
parts = line.split(":")
other_item = parts[0].strip()
ratio = float(parts[1])
parsed_item_prices[current_item][other_item] = ratio
以上是九种不同的方法,以满足不同的偏好和需求。
标签:prices,item,current,字符串,parts,九种,line,parsed,字典 From: https://blog.csdn.net/weixin_73675558/article/details/139320704