我在for循环中的数组中添加值,但它一次又一次地在循环中添加相同的值。 这是我的代码:
for document in activityData:
activityInfo.append(document)
for document in activityInfo:
actRegCount = actregM.countAppliedReg(str(document['_id']),timeInt)
if (not document['owner'] in ownerIds) and (document['deleted']==0):
ownerIds['a']=str(document['owner'])
ownerIds['b']=actRegCount
ownerIds['c']=0
array.append(ownerIds)
和
for document in jobData:
jobInfo.append(document)
for document in jobInfo:
jobApplyCount = jobApplyM.countAppliedJob(str(document['_id']),timeInt)
if (not document['owner'] in ownerIds) and (document['deleted']==0):
ownerIds['a']=str(document['owner'])
ownerIds['b']=0
ownerIds['c']=str(jobApplyCount)
array.append(ownerIds)
但它会一次又一次地增加相同的值。
输出:
[{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'},
{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'},
{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'},
{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'},
{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'},
{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'},
{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'},
{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'},
{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'},
{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'},
{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'},
{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'},
{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'},
{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'},
{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'},
{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'},
{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'},
{'a': '55a646a1874183dc018b4567', 'b': 0, 'c': '0'}]
我是python的新手,所以可能是一个愚蠢的错误。欢迎任何建议。
答案 0 :(得分:0)
在OwnerId
中尝试copy append
{并将append
放入if
):
import copy
for document in jobData:
jobInfo.append(document)
for document in jobInfo:
jobApplyCount = jobApplyM.countAppliedJob(str(document['_id']),timeInt)
if (not document['owner'] in ownerIds) and (document['deleted']==0):
ownerIds['a']=str(document['owner'])
ownerIds['b']=0
ownerIds['c']=str(jobApplyCount)
array.append(copy.deepcopy(ownerIds))