python - Python 列表中的索引包含多个元素。如何访问每个并将它们传递给不同的功能?
问题描述
我在 Python 3 中编写了一个代码,它使用 pandas 从表(.csv)中提取值并将它们附加到一个列表中。我的代码是:
import os
import numpy as np
import csv
import pandas as pd
from generate_xml import write_xml
# global constants
img = None
tl_list = []
br_list = []
object_list = []
tl_x = []
tl_y = []
br_x =[]
br_y =[]
# constants
obj = 'red_hat'
df = pd.read_csv('ring_1_05_sam.csv')
tl_x = df.iloc[5:10, 0:1] - 30
tl_y = df.iloc[5:10, 1:2] - 30
br_x = df.iloc[5:10, 0:1] + 30
br_y = df.iloc[5:10, 1:2] + 30
tl_x = (tl_x.to_string(index=False, header=False))
tl_y = (tl_y.to_string(index=False, header=False))
br_x = (br_x.to_string(index=False, header=False))
br_y = (br_y.to_string(index=False, header=False))
tl_list = (tl_x, tl_y)
br_list = (br_x, br_y)
object_list.append(obj)
print(tl_list[0])
这给了我一个输出
1507.50
1507.44
1507.09
1507.00
如您所见,我在 tl_list[] 处的 index[0] 有这 4 个元素。我的问题是如何单独访问它们?更具体地说,我想将每个值传递给一个函数,一次一个
import os
import numpy as np
import csv
import pandas as pd
from generate_xml import write_xml
# global constants
img = None
tl_list = []
br_list = []
object_list = []
tl_x = []
tl_y = []
br_x =[]
br_y =[]
# constants
obj = 'red_hat'
df = pd.read_csv('ring_1_05_sam.csv')
tl_x = df.iloc[5:10, 0:1] - 30
tl_y = df.iloc[5:10, 1:2] - 30
br_x = df.iloc[5:10, 0:1] + 30
br_y = df.iloc[5:10, 1:2] + 30
tl_x = (tl_x.to_string(index=False, header=False))
tl_y = (tl_y.to_string(index=False, header=False))
br_x = (br_x.to_string(index=False, header=False))
br_y = (br_y.to_string(index=False, header=False))
tl_list = (tl_x, tl_y)
br_list = (br_x, br_y)
object_list.append(obj)
write_xml(image_folder, img, object_list, tl_list, br_list, savedir)
tl_list = []
br_list = []
object_list = []
img = None
这样我的 write_xml 函数在 tl_list 和 br_list 处获取第一个值,然后依次获取下一个值。现在它同时讲述了所有的价值观。任何想法我该怎么做?
解决方案
您可以split
字符串并遍历结果列表:
for tl in tl_list[0].split():
write_xml(image_folder, img, object_list, tl, br_list, savedir)
推荐阅读
- python - 如何在 Google App Engine Mail API 中添加自定义电子邮件标头
- jekyll - 在 .github.io 站点中使用 Jekyll 渲染代码块时出现问题
- laravel - 行组服务器端 Yajra 数据表
- reporting-services - 为什么我的 SSRS 报告将分组数据分成两个组
- java - 你知道如何将一个订单从一个片段发送到一个类吗?
- javascript - 打开键盘时反应原生底部标签栏向上推
- java - 如何使用未知节点将 Firebase 数据库中的值显示到 Firebase 回收视图?
- docker - docker-compose 在 windows 上抛出一个 TypeEroor 的基本示例
- typescript - 如何使用 TypeScript 编译器 API 中的 --incremental?
- r - 检查矩阵中每个单元格的非零值,并将函数从非零值单元格应用到 R 中的最后一个单元格