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

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

在 for 循環(huán)中將字典添加到列表中,每次運行的最后一個 dict.key 的值未正確添加

在 for 循環(huán)中將字典添加到列表中,每次運行的最后一個 dict.key 的值未正確添加

Qyouu 2023-08-15 16:59:31
我通過創(chuàng)建 for 循環(huán)隨機(jī)刪除數(shù)據(jù)數(shù)組的行并重新計算指標(biāo)(總和)。在 for 循環(huán)的每次迭代中,我都會打亂數(shù)據(jù)并刪除第一行值。然后我對剩余行中的值求和。對于每次迭代或運行,我想跟蹤運行編號、剩余點的總和以及被刪除的行。我通過為每次運行創(chuàng)建結(jié)果字典,然后將這些結(jié)果附加到列表中來實現(xiàn)此目的。但是,當(dāng)我打印出結(jié)果字典列表時,運行編號和總和是正確的,但每個結(jié)果字典中已刪除行的值似乎始終是上次運行中刪除的行,而不是其特定的值跑步。import randomimport numpy as npPoints = np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4],[5,5,5,5],[6,6,6,6]])index = 1all_results = []N = 5for i in range(N):    np.random.shuffle(Points)    removed_row = Points[:index]    print(f'Removed row from run {i}: {removed_row}')    remaining_rows = Points[index:]    sum = np.sum(remaining_rows)    run_results = {'Run':i,"Sum of remaining points": sum ,"Removed row": removed_row}    all_results.append(run_results)print(all_results)#outputRemoved row from run 0: [[2 2 2 2]]Removed row from run 1: [[2 2 2 2]]Removed row from run 2: [[4 4 4 4]]Removed row from run 3: [[5 5 5 5]]Removed row from run 4: [[4 4 4 4]][{'Run': 0, 'Sum of remaining points': 76, 'Removed row': array([[4, 4, 4, 4]])}, {'Run': 1, 'Sum of remaining points': 76, 'Removed row': array([[4, 4, 4, 4]])}, {'Run': 2, 'Sum of remaining points': 68, 'Removed row': array([[4, 4, 4, 4]])}, {'Run': 3, 'Sum of remaining points': 64, 'Removed row': array([[4, 4, 4, 4]])}, {'Run': 4, 'Sum of remaining points': 68, 'Removed row': array([[4, 4, 4, 4]])}]正如您所看到的,它似乎總是使用最后一次運行的“removed_row”變量,而不是特定于運行的“removed_row”
查看完整描述

3 回答

?
侃侃無極

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

嗯,確實很棘手!


問題出在任務(wù)上:


run_results = {'Run':i,"Sum of remaining points": sum ,"Removed row": removed_row}

它存儲對 的引用removed_row,就像在 Python 中變量只是對對象的引用一樣。


創(chuàng)建新數(shù)組np.array(removed_row):


import random

import numpy as np


points = np.array([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]])

index = 1

all_results = []

N = 5

for i in range(N):

    np.random.shuffle(points)

    removed_row = points[:index]

    print(f'Removed row from run {i}: {removed_row}')

    remaining_rows = points[index:]

    sum = np.sum(remaining_rows)

    run_results = {'Run':i,"Sum of remaining points": sum ,"Removed row": np.array(removed_row)}

    all_results.append(run_results)

print(all_results)

輸出:


Removed row from run 0: [[4 4 4 4]]

Removed row from run 1: [[6 6 6 6]]

Removed row from run 2: [[6 6 6 6]]

Removed row from run 3: [[3 3 3 3]]

Removed row from run 4: [[4 4 4 4]]

[{'Run': 0, 'Sum of remaining points': 68, 'Removed row': array([[4, 4, 4, 4]])}, {'Run': 1, 'Sum of remaining points': 60, 'Removed row': array([[6, 6, 6, 6]])}, {'Run': 2, 'Sum of remaining points': 60, 'Removed row': array([[6, 6, 6, 6]])}, {'Run': 3, 'Sum of remaining points': 72, 'Removed row': array([[3, 3, 3, 3]])}, {'Run': 4, 'Sum of remaining points': 68, 'Removed row': array([[4, 4, 4, 4]])}]



查看完整回答
反對 回復(fù) 2023-08-15
?
炎炎設(shè)計

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

您需要實際刪除該行:


import random

import numpy as np


Points = np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4],[5,5,5,5],[6,6,6,6]])

index = 1

all_results = []

N = 5

for i in range(N):

    np.random.shuffle(Points)

    removed_row = Points[:index]

    Points = Points[index:]     # <<<<<<< Remove row

    print(f'Removed row from run {i}: {removed_row}')

    remaining_rows = Points[index:]

    sum = np.sum(remaining_rows)

    run_results = {'Run':i,"Sum of remaining points": sum ,"Removed row": removed_row}

    all_results.append(run_results)

print(all_results)

輸出


Removed row from run 0: [[3 3 3 3]]

Removed row from run 1: [[6 6 6 6]]

Removed row from run 2: [[1 1 1 1]]

Removed row from run 3: [[2 2 2 2]]

Removed row from run 4: [[4 4 4 4]]

[{'Run': 0, 'Sum of remaining points': 56, 'Removed row': array([[3, 3, 3, 3]])}, 

 {'Run': 1, 'Sum of remaining points': 40, 'Removed row': array([[6, 6, 6, 6]])}, 

 {'Run': 2, 'Sum of remaining points': 24, 'Removed row': array([[1, 1, 1, 1]])}, 

 {'Run': 3, 'Sum of remaining points': 16, 'Removed row': array([[2, 2, 2, 2]])}, 

 {'Run': 4, 'Sum of remaining points':  0, 'Removed row': array([[4, 4, 4, 4]])}]


查看完整回答
反對 回復(fù) 2023-08-15
?
白衣染霜花

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

請注意,隨機(jī)播放需要很長時間。這是一種大量矢量化的方法:


# choose the index to drop first:

to_drop = np.random.choice(np.arange(len(Points)), N, replace=False)


# remain sum:

remains = Points.sum(0) - Points[to_drop[::-1]].cumsum(0)


out = [{'run':i, 'sum_renmaining': remains[i], 'remove row': Points[to_drop[i]]} for i in range(N)]



查看完整回答
反對 回復(fù) 2023-08-15
  • 3 回答
  • 0 關(guān)注
  • 194 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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