gpt4 book ai didi

Python 适合给定输入的数据类型

转载 作者:太空宇宙 更新时间:2023-11-04 02:40:25 25 4
gpt4 key购买 nike

我有这样的数据结构,

Fruit Apple Mango Orange
Price 4 5 3
Units 6 10 4

哪种存储方式最好? 字典还是pandas

在那之后需要操作这些数据..任何建议都是可取的

最佳答案

我希望这个解决方案能满足您的需求:

from collections import Counter
fruits = {'Apple': [4, 6],
'Mango': [5, 10],
'Orange': [3, 4]
}

def changes(number, coins_available, coins_current):
if sum(coins_current) == number:
yield coins_current
elif sum(coins_current) > number:
pass
elif coins_available == []:
pass
else:
for c in changes(number, coins_available[:], coins_current + [coins_available[0]]):
yield c
for c in changes(number, coins_available[1:], coins_current):
yield c

n = 30
coins = [i[0] for i in fruits.values()]
mapper = {i[0]: i[1] for i in fruits.values()}

solutions = [sol for sol in changes(n, coins, [])]
amounts = [list(map(lambda x: mapper[x], s)) for s in solutions]
reworked_amounts = [sum(i) for i in amounts]
final = {i: j for i, j in zip(reworked_amounts, solutions)}
lookup = max(final.keys())
fruit_map = {v[0]: k for k, v in fruits.items()}
the_solution = map(lambda x: fruit_map[x], final[lookup])
printout = Counter(the_solution)
for item in printout:
print('{}: {}'.format(item, printout[item]))

关于Python 适合给定输入的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46707022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com