2013-10-27 5 views
-1
get_quantities({'t1': ['Vegetarian stew', 'Poutine', 'Vegetarian stew'], 't3': ['Steak pie', 'Poutine', 'Vegetarian stew'], 't4': ['Steak pie', 'Steak pie']}) 

이것은 내 사전입니다. t은 표를 나타냅니다.식품의 이름이 키와 그 수량으로 표시된 새 사전을 반환하십시오.

내가 새 사전을 반환해야

는 :

{'Vegetarian stew': 3, 'Poutine': 2, 'Steak pie': 3} like this. 

어떻게이 코드를 작성할 수 있습니다

?

+1

약간의 노력을 보여주십시오. 사전에 대한 자습서를 검색하고 직접 해보십시오. 시도한 코드에 문제가 있으면 StackOverflow를 방문하여 도움을 받으십시오. – Christian

답변

0
from collections import Counter 

def get_quantities(tables): 
    counter = Counter() 
    for table in tables.iterValues(): 
     counter.update(table) 
    return counter 

이렇게하면 Counter이 사전 형 개체입니다. 예를 들어

,

quantities = get_quantities({'t1': ['Vegetarian stew', 'Poutine', 'Vegetarian stew'], 't3': ['Steak pie', 'Poutine', 'Vegetarian stew'], 't4': ['Steak pie', 'Steak pie']}) 
print quantities['Vegetarian stew'] 

3를 인쇄합니다.

관련 문제