为了账号安全,请及时绑定邮箱和手机立即绑定

在 for 循环中将字典添加到列表中,每次运行的最后一个 dict.key 的值未正确添加

在 for 循环中将字典添加到列表中,每次运行的最后一个 dict.key 的值未正确添加

Qyouu 2023-08-15 16:59:31
我通过创建 for 循环随机删除数据数组的行并重新计算指标(总和)。在 for 循环的每次迭代中,我都会打乱数据并删除第一行值。然后我对剩余行中的值求和。对于每次迭代或运行,我想跟踪运行编号、剩余点的总和以及被删除的行。我通过为每次运行创建结果字典,然后将这些结果附加到列表中来实现此目的。但是,当我打印出结果字典列表时,运行编号和总和是正确的,但每个结果字典中已删除行的值似乎始终是上次运行中删除的行,而不是其特定的值跑步。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贡献2051条经验 获得超10个赞

嗯,确实很棘手!


问题出在任务上:


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

它存储对 的引用removed_row,就像在 Python 中变量只是对对象的引用一样。


创建新数组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]])}]



查看完整回答
反对 回复 2023-08-15
?
炎炎设计

TA贡献1808条经验 获得超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]])}]


查看完整回答
反对 回复 2023-08-15
?
白衣染霜花

TA贡献1796条经验 获得超10个赞

请注意,随机播放需要很长时间。这是一种大量矢量化的方法:


# 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)]



查看完整回答
反对 回复 2023-08-15
  • 3 回答
  • 0 关注
  • 119 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信