Python Forum
Index in Python list contains multiple elements.How to access each val and pass them
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Index in Python list contains multiple elements.How to access each val and pass them
#1
I have written a code in Python 3 that extracts values from a table(.csv) using pandas and appends them to a list. My code is:
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])
This gives me an output of

Output:
1507.50 1507.44 1507.09 1507.00
So as you can see, my index[0] at tl_list[] has these 4 elements. My question is how do i access them individually? To be more specific, I want to pass each value to a function, one at a time

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 = []
So that my write_xml function takes the first value at tl_list and br_list then the next value sequentially. Right now it tales all values at once. Any ideas how i can do it?
Reply
#2
tl_list[0][0], tl_list[0][1]

You can loop over tl_list[0] in that case.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [Faircom CTree] Access from Python? Winfried 2 557 Feb-23-2024, 01:26 PM
Last Post: Winfried
  Join each list elements with string in other DF handy88 0 1,542 Feb-09-2021, 07:00 PM
Last Post: handy88
  [split] Getting Index Error - list index out of range krishna 2 2,621 Jan-09-2021, 08:29 AM
Last Post: buran
  Formula with elements of list - If-condition regarding the lists elements lewielewis 2 2,741 May-08-2020, 01:41 PM
Last Post: nnk
  Getting Index Error - list index out of range RahulSingh 2 6,167 Feb-03-2020, 07:17 AM
Last Post: RahulSingh
  pandas.read_sas with chunksize: IndexError list index out of range axelle 0 2,594 Jan-28-2020, 09:30 AM
Last Post: axelle
  How to access dataframe elements SriMekala 4 3,242 Jul-30-2019, 01:50 AM
Last Post: scidam
  Python dictionary adds only the last elements of a list mahmoud899 1 2,486 Dec-13-2018, 06:30 PM
Last Post: ichabod801
  Checking the elements of a matrix with an elements of a list juniorcoder 11 5,889 Sep-17-2018, 03:02 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020