Python suitable Datatype for given input

时间:2017-10-12 10:14:19

标签: python pandas dictionary

I am having data structure something like this,

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

which is best way to store this? dictionary or pandas?

after that need to manipulate these data.. any suggestion would appreciable

2 个答案:

答案 0 :(得分:2)

I hope this solution will fit your needs:

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]))

答案 1 :(得分:0)

I prefer pandas as they are easy to perform some analytics on it and indexing is pretty easy.