第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

我怎樣才能從字典中進(jìn)行匯總交易?

我怎樣才能從字典中進(jìn)行匯總交易?

一只斗牛犬 2022-12-27 10:18:34
我有這樣的代碼:bought = {'Banana':2, 'Avocado':3, 'Cherries':1}price_of = {'Apple': 6,'Avocado': 5,'Banana': 3,'Blackberries': 10,'Blueberries': 12,'Cherries': 7,'Pineapple': 7}bought = sorted(bought) def print_summary(items,fprice):   items = bought   for item, n in fprice.items():      print(n, item, ':', n * items[item])      print('total', sum(n * items[item])print_summary(bought,price_of)但我得到這樣的輸出:在此處輸入圖像描述我想讓輸出是這樣的:3 Avocado : 152 Banana : 61 Cherries : 7total : 28
查看完整描述

4 回答

?
慕沐林林

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊

我弄得有點(diǎn)亂,但是可以用


bought = {'Banana':2, 'Avocado':3, 'Cherries':1}

price_of = {'Apple': 6,

'Avocado': 5,

'Banana': 3,

'Blackberries': 10,

'Blueberries': 12,

'Cherries': 7,

'Pineapple': 7}


#Creating a new sorted dictionary named b

a = sorted(bought)

b = {}

for fruit in a:

    b[fruit] = bought.get(fruit)

#Making bought be b

bought = b


def summary(items, price):

    total = 0

    for i in items:

        print(f" {items.get(i)} {i} : {items.get(i)*price.get(i)}")

        total = total + items.get(i)*price.get(i)

    print(f" Total {total}")


summary(bought, price_of)

希望能幫助到你


查看完整回答
反對(duì) 回復(fù) 2022-12-27
?
倚天杖

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

bought = {'Banana':2, 'Avocado':3, 'Cherries':1}

price_of = {'Apple': 6,

'Avocado': 5,

'Banana': 3,

'Blackberries': 10,

'Blueberries': 12,

'Cherries': 7,

'Pineapple': 7}


# bought = sorted(bought)

def print_summary(items,fprice):

#    items = counter_item(chart)

#    for item, n in fprice.items():

#       print(n, item, ':', n * items[item])

#       print('total', sum(n * items[item]))

    total = 0

    for i, k in enumerate(sorted(items, key= lambda x: items[x], reverse=True )):

        print(items[k], k, ':', items[k] * fprice[k])

        total += items[k] * fprice[k]

    print("total : {}".format(total))


print_summary (bought,price_of)

3 Avocado : 15

2 Banana : 6

1 Cherries : 7

total : 28


查看完整回答
反對(duì) 回復(fù) 2022-12-27
?
阿波羅的戰(zhàn)車

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊

在這里你想根據(jù)鍵值以相反的順序?qū)δ愕淖值溥M(jìn)行排序,所以你必須在你的代碼中添加它。這里主要關(guān)注的是排序,所以我以簡單的方式完成了值的乘法。


bought = {'Banana':2, 'Avocado':3, 'Cherries':1}

price_of = {'Apple': 6,

'Avocado': 5,

'Banana': 3,

'Blackberries': 10,

'Blueberries': 12,

'Cherries': 7,

'Pineapple': 7}


bought['Banana'] *= price_of['Banana']

bought['Avocado'] *= price_of['Avocado']

bought['Cherries'] *= price_of['Cherries']


lst = list()

for k,v in bought.items():

    newtup = (v,k)

    lst.append(newtup)


lst = sorted(lst, reverse = True)

n=3

total = 0

for k,v in lst:

    print(n,' ',v,' : ',k)

    total = total + k

    n = n-1

print('total :', total) 

此代碼的輸出是:


3   Avocado  :  15

2   Cherries  :  7

1   Banana  :  6

total : 28


查看完整回答
反對(duì) 回復(fù) 2022-12-27
?
MMTTMM

TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個(gè)贊

for fruit, quantity in sorted(bought.items()):

    print(f"{quantity} {fruit} : {quantity * price_of[fruit]}")

print(f"total : {sum(quantity * price_of[fruit] for fruit, quantity in bought.items())}")



查看完整回答
反對(duì) 回復(fù) 2022-12-27
  • 4 回答
  • 0 關(guān)注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)