Py学习  »  Python

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

Shameendra • 6 年前 • 1482 次点击  

我用python 3编写了一个代码,它使用panda从表(.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取第一个值,然后按顺序取下一个值。现在它同时讲述了所有的价值观。有什么办法吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43290
文章 [ 1 ]  |  最新文章 6 年前
blhsing
Reply   •   1 楼
blhsing    6 年前

你可以 split 字符串并遍历结果列表:

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