python - Python列表中的索引包含多个元素。如何访问每个并将它们传递给不同的函数?

标签 python python-3.x

我用 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[] 处的索引[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 中的第一个值,然后依次获取下一个值。现在它同时讲述了所有的值(value)观。我有什么想法可以做到吗?

最佳答案

您可以分割字符串并迭代结果列表:

for tl in tl_list[0].split():
    write_xml(image_folder, img, object_list, tl, br_list, savedir)

关于python - Python列表中的索引包含多个元素。如何访问每个并将它们传递给不同的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53558975/

相关文章:

python - 添加总计的值和列数

python - 删除重复行?

python - 列表理解到子列表

python - 如何通过检查 python 列表是否与给定模式匹配来对列表进行排序?

python - 在Python中对值进行分组和划分

python - 如何在 django 应用程序中获取复选框值

python-3.x - 如何将订单簿数据正确写入具有相同时间戳的 influxdb

python - Tkinter 大文本对话(单击按钮时保存字符串)

python - 对原始字符串进行编码,以便将其解码为 json

python - 当元素包含 numpy 数组时,无法测试 python 列表元素成员资格