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

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

如何一起使用兩個(gè)列表?

如何一起使用兩個(gè)列表?

HUX布斯 2024-01-16 15:44:50
我有 2 個(gè)清單;材料及其相應(yīng)的數(shù)量。例如,列表是:words=["banana","apple","orange","bike","car"]和numbers=[1,5,2,5,1]。通過這些列表,我如何確定哪一個(gè)列表中的項(xiàng)目數(shù)量最少,哪一個(gè)列表中的項(xiàng)目數(shù)量最多。我不知道從哪里開始謝謝!
查看完整描述

5 回答

?
小唯快跑啊

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

from operator import itemgetter


words = ["banana", "apple", "orange", "bike", "car"]

numbers = [1, 5, 2, 5, 1]


min_item = min(zip(words, numbers), key=itemgetter(1))

max_item = max(zip(words, numbers), key=itemgetter(1))


print("The min item was {} with a count of {}".format(*min_item))

print("The max item was {} with a count of {}".format(*max_item))

輸出:


The min item was banana with a count of 1

The max item was apple with a count of 5

>>> 


查看完整回答
反對 回復(fù) 2024-01-16
?
DIEA

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

用于zip匹配相應(yīng)的單詞和數(shù)字:


>>> words = ["banana", "apple", "orange", "bike", "car"]

>>> numbers = [1, 5, 2, 5, 1]

>>> list(zip(words, numbers))

[('banana', 1), ('apple', 5), ('orange', 2), ('bike', 5), ('car', 1)]

如果將zip它們(number, word)配對,則可以直接在這些對上使用min和max來獲得最小/最大組合:


>>> min(zip(numbers, words))

(1, 'banana')

>>> max(zip(numbers, words))

(5, 'bike')

dict或者從對中創(chuàng)建一個(gè)(word, number)并在其上使用min和max。這只會給你單詞,但你可以從字典中獲取相應(yīng)的數(shù)字:


>>> d = dict(zip(words, numbers))

>>> d

{'apple': 5, 'banana': 1, 'bike': 5, 'car': 1, 'orange': 2}

>>> min(d, key=d.get)

'banana'

>>> max(d, key=d.get)

'apple'


查看完整回答
反對 回復(fù) 2024-01-16
?
白衣染霜花

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

保持簡單:如果您只想要一件最少和一件最多的物品


words=["banana","apple","orange","bike","car"] 

numbers=[1,5,2,5,1]


# get least and most amounts using max and min

least_amount = min(numbers)

most_amount=max(numbers)


# get fruit with least amount using index

index_of_least_amount = numbers.index(least_amount)

index_of_most_amount = numbers.index(most_amount)


# fruit with least amount

print(words[index_of_least_amount])


# fruit with most amount

print(words[index_of_most_amount])


查看完整回答
反對 回復(fù) 2024-01-16
?
慕森卡

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

您應(yīng)該使用字典或元組列表來執(zhí)行以下操作:


words=["banana","apple","orange","bike","car"]

numbers=[1,5,2,5,1]

dicti = {}


for i in range(len(words)):

    dicti[words[i]] = numbers[i]


print(dicti["banana"]) #1

結(jié)果字典


{'banana': 1, 'apple': 5, 'orange': 2, 'bike': 5, 'car': 1}

這是如何使用元組列表獲取其中的最大值和最小值的方法


words=["banana","apple","orange","bike","car"]

numbers=[1,5,2,5,1]


numMax = max(numbers)

numMin = min(numbers)

print([x for x,y in zip(words, numbers) if y == numMax ]) #maxes

print([x for x,y in zip(words, numbers) if y == numMin ]) #mins


查看完整回答
反對 回復(fù) 2024-01-16
?
青春有我

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

words = ["banana","apple","orange","bike","car"]

numbers = [1, 5, 2, 5, 1]


# Make a dict, is easier

adict = {k:v for k,v in zip(words, numbers)}


# Get maximums and minimums

min_value = min(adict.values()) 

max_value = max(adict.values())


# Here are the results, both material and values. You have also those which are tied

min_materials = {k,v for k,v in adict if v == min_value}

max_materials = {k,v for k,v in adict if v == max_value}


查看完整回答
反對 回復(fù) 2024-01-16
  • 5 回答
  • 0 關(guān)注
  • 310 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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