如何使用循环访问和保存 "array of lists" 中的元素
How to access and save elements in "array of lists" using loop
我有一个包含图像信息的 base64 编码的 tsv 文件。我读了它并解码了它。这里我贴一张图片的信息。格式如下:
{'199476': array([[194.04953 , 184.43268 , 639. , 425.29 ],
[318.4676 , 15.762019, 421.1574 , 253.30766 ],
[219.09093 , 258.60388 , 452.2419 , 384.9459 ],
[ 0. , 47.438347, 480.05542 , 425.29 ],
[ 0. , 0. , 363.19708 , 279.19672 ],
[148.79245 , 0. , 639. , 210.85545 ],
[ 0. , 0. , 258.4733 , 334.14972 ],
[158.05794 , 320.6399 , 501.2573 , 425.29 ],
[452.9518 , 0. , 639. , 165.6061 ],
[ 0. , 255.72142 , 407.57358 , 425.29 ],
[ 0. , 22.864079, 140.49962 , 183.17285 ],
[361.452 , 212.55576 , 639. , 388.3693 ],
[554.8864 , 0. , 639. , 101.53771 ],
[404.05536 , 185.98639 , 578.6646 , 228.58595 ],
[ 0. , 140.13982 , 123.02028 , 159.72864 ],
[244.72183 , 250.01173 , 433.06476 , 369.9772 ],
[128.43852 , 236.70012 , 473.5753 , 369.5989 ],
[ 52.357124, 39.874092, 75.0104 , 59.43568 ],
[144.01859 , 125.3886 , 227.6077 , 204.53206 ],
[236.9453 , 0. , 338.77094 , 123.54699 ],
[543.54865 , 229.0633 , 627.2213 , 327.51517 ],
[ 30.98246 , 158.72491 , 326.61508 , 277.84668 ],
[338.70795 , 80.93436 , 405.304 , 245.34258 ],
[151.49304 , 129.96037 , 177.9043 , 200.56523 ],
[ 94.5418 , 197.5486 , 316.87244 , 253.77058 ],
[354.94272 , 0. , 464.82605 , 226.65717 ],
[298.1035 , 156.23262 , 315.1587 , 191.82831 ],
[414.6097 , 133.66512 , 639. , 222.94992 ],
[ 95.302864, 92.23008 , 126.67215 , 141.62634 ],
[184.92436 , 133.2275 , 211.72473 , 199.64825 ],
[446.2884 , 203.46916 , 631.4623 , 366.25217 ],
[ 73.46329 , 189.02185 , 332.6924 , 204.93848 ],
[575.9691 , 204.70457 , 639. , 244.41171 ],
[ 9.975321, 115.68157 , 88.33198 , 147.83711 ]], dtype=float32)}
我使用的代码如下:
if __name__ == '__main__':
in_data = {}
with open(infile, "r+b") as tsv_in_file:
reader = csv.DictReader(codecs.iterdecode(tsv_in_file,'utf-8'), delimiter='\t', fieldnames = FIELDNAMES)
#print(reader)
for item in reader:
for field in ['boxes']:
item[field] = np.frombuffer(base64.b64decode(item[field]), dtype=np.float32).reshape((int(item['num_boxes']),-1))
in_data[item['image_id']]= item['boxes']
break
print (in_data)
现在我正尝试在像这样对数组元素应用公式后保存值
for item in reader:
for field in ['boxes']:
item[field] = np.frombuffer(base64.b64decode(item[field]), dtype=np.float32).reshape((int(item['num_boxes']),-1))
in_data[item['image_id']] = (((item['boxes'][1][0])-(item['boxes'][0][0]))/(item['boxes'][0][2]))#[0]#[0]#[j]#[j]#[((i+1)-(j))]
in_data[item['image_id']] = (((item['boxes'][1][1])-(item['boxes'][0][1]))/(item['boxes'][0][3]))
in_data[item['image_id']] = ((item['boxes'][1][2])/(item['boxes'][0][2]))
in_data[item['image_id']] = ((item['boxes'][1][3])/(item['boxes'][0][3]))
in_data[item['image_id']]=(((item['boxes'][1][2])*(item['boxes'][1][3]))/((item['boxes'][0][2])*(item['boxes'][0][3])))
print (in_data)
但它只保存最后的结果。我想将其所有答案保存为包含 5 列的 1st 列表。我该怎么做?
另外,我为 1st 和 2nd 列表做了这件事。我想为 1st 和 3rd 列表以及最多列表数执行此操作。我已经对索引值进行了硬编码。我需要一个循环,但我无法在此处应用 loop。请帮助我
你总是在改写你的结果。尝试其中之一
附加版本
in_data[item['image_id']] = []
in_data[item['image_id']].append(your_code_for_column_0)
in_data[item['image_id']].append(your_code_for_column_1)
...
分配版本
in_data[item['image_id']] = [0]*5
in_data[item['image_id']][0] = your_code_for_column_0
in_data[item['image_id']][1] = your_code_for_column_1
...
我有一个包含图像信息的 base64 编码的 tsv 文件。我读了它并解码了它。这里我贴一张图片的信息。格式如下:
{'199476': array([[194.04953 , 184.43268 , 639. , 425.29 ],
[318.4676 , 15.762019, 421.1574 , 253.30766 ],
[219.09093 , 258.60388 , 452.2419 , 384.9459 ],
[ 0. , 47.438347, 480.05542 , 425.29 ],
[ 0. , 0. , 363.19708 , 279.19672 ],
[148.79245 , 0. , 639. , 210.85545 ],
[ 0. , 0. , 258.4733 , 334.14972 ],
[158.05794 , 320.6399 , 501.2573 , 425.29 ],
[452.9518 , 0. , 639. , 165.6061 ],
[ 0. , 255.72142 , 407.57358 , 425.29 ],
[ 0. , 22.864079, 140.49962 , 183.17285 ],
[361.452 , 212.55576 , 639. , 388.3693 ],
[554.8864 , 0. , 639. , 101.53771 ],
[404.05536 , 185.98639 , 578.6646 , 228.58595 ],
[ 0. , 140.13982 , 123.02028 , 159.72864 ],
[244.72183 , 250.01173 , 433.06476 , 369.9772 ],
[128.43852 , 236.70012 , 473.5753 , 369.5989 ],
[ 52.357124, 39.874092, 75.0104 , 59.43568 ],
[144.01859 , 125.3886 , 227.6077 , 204.53206 ],
[236.9453 , 0. , 338.77094 , 123.54699 ],
[543.54865 , 229.0633 , 627.2213 , 327.51517 ],
[ 30.98246 , 158.72491 , 326.61508 , 277.84668 ],
[338.70795 , 80.93436 , 405.304 , 245.34258 ],
[151.49304 , 129.96037 , 177.9043 , 200.56523 ],
[ 94.5418 , 197.5486 , 316.87244 , 253.77058 ],
[354.94272 , 0. , 464.82605 , 226.65717 ],
[298.1035 , 156.23262 , 315.1587 , 191.82831 ],
[414.6097 , 133.66512 , 639. , 222.94992 ],
[ 95.302864, 92.23008 , 126.67215 , 141.62634 ],
[184.92436 , 133.2275 , 211.72473 , 199.64825 ],
[446.2884 , 203.46916 , 631.4623 , 366.25217 ],
[ 73.46329 , 189.02185 , 332.6924 , 204.93848 ],
[575.9691 , 204.70457 , 639. , 244.41171 ],
[ 9.975321, 115.68157 , 88.33198 , 147.83711 ]], dtype=float32)}
我使用的代码如下:
if __name__ == '__main__':
in_data = {}
with open(infile, "r+b") as tsv_in_file:
reader = csv.DictReader(codecs.iterdecode(tsv_in_file,'utf-8'), delimiter='\t', fieldnames = FIELDNAMES)
#print(reader)
for item in reader:
for field in ['boxes']:
item[field] = np.frombuffer(base64.b64decode(item[field]), dtype=np.float32).reshape((int(item['num_boxes']),-1))
in_data[item['image_id']]= item['boxes']
break
print (in_data)
现在我正尝试在像这样对数组元素应用公式后保存值
for item in reader:
for field in ['boxes']:
item[field] = np.frombuffer(base64.b64decode(item[field]), dtype=np.float32).reshape((int(item['num_boxes']),-1))
in_data[item['image_id']] = (((item['boxes'][1][0])-(item['boxes'][0][0]))/(item['boxes'][0][2]))#[0]#[0]#[j]#[j]#[((i+1)-(j))]
in_data[item['image_id']] = (((item['boxes'][1][1])-(item['boxes'][0][1]))/(item['boxes'][0][3]))
in_data[item['image_id']] = ((item['boxes'][1][2])/(item['boxes'][0][2]))
in_data[item['image_id']] = ((item['boxes'][1][3])/(item['boxes'][0][3]))
in_data[item['image_id']]=(((item['boxes'][1][2])*(item['boxes'][1][3]))/((item['boxes'][0][2])*(item['boxes'][0][3])))
print (in_data)
但它只保存最后的结果。我想将其所有答案保存为包含 5 列的 1st 列表。我该怎么做?
另外,我为 1st 和 2nd 列表做了这件事。我想为 1st 和 3rd 列表以及最多列表数执行此操作。我已经对索引值进行了硬编码。我需要一个循环,但我无法在此处应用 loop。请帮助我
你总是在改写你的结果。尝试其中之一
附加版本
in_data[item['image_id']] = []
in_data[item['image_id']].append(your_code_for_column_0)
in_data[item['image_id']].append(your_code_for_column_1)
...
分配版本
in_data[item['image_id']] = [0]*5
in_data[item['image_id']][0] = your_code_for_column_0
in_data[item['image_id']][1] = your_code_for_column_1
...