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

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

如何使用循環(huán)在Python中保存嵌套列表的元素并刪除列表

如何使用循環(huán)在Python中保存嵌套列表的元素并刪除列表

翻翻過去那場雪 2021-05-07 18:10:21
我正在嘗試創(chuàng)建一個列表列表(嵌套列表),分別將元素的數(shù)量和用戶列表的數(shù)量分別定為b,a。但是,如何將temp_list保存到list_of_lists中。由于我是在將temp_list附加到list_of_list之后刪除的,因此以后列表中的元素也將被刪除。a, b= map(int,input().split())i = 0list_of_lists = []while i < b:    temp_list = []    temp_list.append(map(float, input().split()))    print(temp_list, i)    list_of_list.append(temp_list)    del temp_list[:]    i += 1print(list_of_lists)
查看完整描述

2 回答

?
神不在的星期二

TA貢獻1963條經(jīng)驗 獲得超6個贊

幾個問題:

  • 您不應該del temp_list[:]刪除剛剛添加的對象。

  • 您的循環(huán)會更好for。

  • 您的變量稱為list_of_listsnot list_of_list,因此list_of_list.append()應拋出一個NameError

  • map在Py3中,它會返回一個迭代器,因此您需要將其轉換為列表,可以使用,temp_list.extend(map(...))但可以直接創(chuàng)建它。注意:的首次使用map(...)已解壓縮到各個變量中,因此可以按預期工作。

更新的代碼:

a, b = map(int, input().split())

list_of_lists = []

for i in range(b):

    temp_list = list(map(float, input().split()))

    print(temp_list, i)

    list_of_lists.append(temp_list)


查看完整回答
反對 回復 2021-05-25
?
收到一只叮咚

TA貢獻1821條經(jīng)驗 獲得超5個贊

在您的代碼中,您每次都刪除臨時列表


del temp_list[:]

代替


a, b = map(int, input().split())

您可以像這樣簡單地使用它


a, b = map(int, input()) 

并輸入3,4之類的輸入python將自動將其作為元組獲取,并將其分別分配給變量a,b


a, b = map(int, input()) #3,4

list_of_lists = []

for i in range(b):

    temp_list = list(map(float, input())) 

    print(temp_list, i)

    list_of_lists.append(temp_list)

print (list_of_lists)


查看完整回答
反對 回復 2021-05-25
  • 2 回答
  • 0 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

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