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

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

如何在另一個(gè)函數(shù)中使用一個(gè)函數(shù)的數(shù)據(jù)?

如何在另一個(gè)函數(shù)中使用一個(gè)函數(shù)的數(shù)據(jù)?

Helenr 2024-01-16 10:35:03
我是初學(xué)者,有一個(gè)與將函數(shù)數(shù)據(jù)與其他函數(shù)一起使用相關(guān)的問(wèn)題。假設(shè)一個(gè)函數(shù)my_list生成一個(gè)列表和一個(gè)子列表。我想知道如何訪問(wèn)該函數(shù)生成的列表的子列表中的項(xiàng)目,并使用這些數(shù)字在另一個(gè)函數(shù)中計(jì)算它們的總和。我嘗試過(guò)類似的事情def compute_sum(myNewlist): #myNewList is the list generated by the other function#     for i in myNewList:         addup += i 但我確實(shí)對(duì) python 還不夠熟悉,無(wú)法思考如何做到這一點(diǎn)。我想我要問(wèn)的是如何將子列表中的元素調(diào)用到另一個(gè)函數(shù)?編輯 - 只需將代碼放在這里,以便回復(fù)者有更多的理解!fyle = input('Enter the file name you want to process: ')def read_data(fyle):    with open(fyle) as file:                for line in fyle:            lne = [line.strip().split() for line in open(fyle).readlines()]            newlist = [[elem[1], elem[0], elem[2]] for elem in lne]                         print(newlist)        read_data(fyle)def compute_sum(newlist):          ???編輯 2 - 列表也看起來(lái)像mylist = [[Smith, Bob, 18], [Jorgen, Peter, 14]]- 為此,我希望提取并添加數(shù)字,而不是字符串
查看完整描述

2 回答

?
牛魔王的故事

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

這是代碼重寫的示例,我認(rèn)為它演示了您所要求的內(nèi)容:


fyle = input('Enter the file name you want to process: ')


def read_data(fyle):

    lne = [line.strip().split() for line in open(fyle).readlines()]

    newlist = [[elem[1], elem[0], elem[2]] for elem in lne]

    return newlist


def compute_sum(newlist):

    s = sum([int(x[0]) for x in newlist])

    return s



list = read_data(fyle)

sum = compute_sum(list)

print(sum)

數(shù)據(jù)文件/tmp/data.txt:


line1_item1 10 line1_item3

line2_item1 20 line2_item3

line3_item1 30 line3_item3

結(jié)果:


Enter the file name you want to process: /tmp/data.txt

60


查看完整回答
反對(duì) 回復(fù) 2024-01-16
?
陪伴而非守候

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

下面的代碼應(yīng)該可以計(jì)算總和。正如您在上面的帖子中提到的,我考慮過(guò) mylist = [[Smith, Bob, 18], [Jorgen, Peter, 14]]。


def compute_sum(new_list):

     sum=0

     for item in new_list:

          sum=sum+int(item[2])

     #print(sum)

     return sum


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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