[問題] list問題

看板Python作者 (gto)時間4年前 (2019/11/11 09:18), 編輯推噓6(608)
留言14則, 7人參與, 4年前最新討論串2/2 (看更多)
請教大家list問題 若a = [1,2,3,4] 我現在要把第3個數和第4個數相減 並print出來 該如何做???? 我會把值取出來 但相減會出現錯誤訊息 我該如何做呢? 我的程式碼片斷是... act_num=history_1[len(history_1)-1:len(history_1)] act_num_2=history_1[len(history_1)-6:len(history_1)-5] print('act:',map(float,act_num_2)-map(float,act_num)) 結果跑出 Traceback (most recent call last): File "C:\Users\Administrator\Desktop\str\time_test.py", line 101, in <module> print('act:',map(float,act_num_2)-map(float,act_num)) TypeError: unsupported operand type(s) for -: 'list' and 'list' 這是什麼原故呢??? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.253.101.13 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1573435133.A.796.html

11/11 09:37, 4年前 , 1F
-1, -6, -5 意義不明
11/11 09:37, 1F

11/11 09:50, 4年前 , 2F
為什麼要寫這麼複雜?
11/11 09:50, 2F

11/11 09:57, 4年前 , 3F
因為我要取這個list裡最後倒數幾個特定的數...
11/11 09:57, 3F

11/11 09:58, 4年前 , 4F
你先做slice再做map得到的還是list 而list不支援相減
11/11 09:58, 4F

11/11 09:58, 4年前 , 5F
就這樣
11/11 09:58, 5F

11/11 10:01, 4年前 , 6F
T大,那有辦法把list裡東西取出來,再加減嗎?
11/11 10:01, 6F

11/11 10:46, 4年前 , 7F
act_num = history[-1] act_num_2 = history[-6]
11/11 10:46, 7F

11/11 10:46, 4年前 , 8F
numpy array支援elelmentwise的加減
11/11 10:46, 8F

11/11 10:51, 4年前 , 9F
只看描述似乎就是
11/11 10:51, 9F

11/11 10:51, 4年前 , 10F
print(a[3] -a[2]) 嗎?
11/11 10:51, 10F

11/11 11:01, 4年前 , 11F
謝謝各位大大熱情支援,小弟不是專科的所以問的很ooxx
11/11 11:01, 11F

11/11 11:02, 4年前 , 12F
我試上面大大的方法及說明,讓我試出我要功能了...
11/11 11:02, 12F

11/11 11:02, 4年前 , 13F
小弟謝謝各位熱情鄉民!!!!!!!
11/11 11:02, 13F

11/13 16:57, 4年前 , 14F
換成np.array(your_list)就可以做你說的向量計算
11/13 16:57, 14F
文章代碼(AID): #1ToBRzUM (Python)
討論串 (同標題文章)
文章代碼(AID): #1ToBRzUM (Python)