Rookie Question: Passing a nested list into a function?

B

Brett

I am trying to pass the value of a nested list into a function (to my "if"
statement) as follows:

textfilelist = [["titlelist1[x][1]"]]
def idfer(listlength, comparelistlength, list):
while x < (listlength - 1):
while y < comparelistlength:
if list == titlelist2[y][1]:
(I cutoff the end to focus on the problem)

#Here i call my function hoping i am passing the value within
textfilelist[0][0] to my function
idfer(textfilelist[0][1], textfilelist[1][1], textfilelist[0][0])


--------------
I don't if there is a "legal" way to do this, but I would appreciate some
guidance.

Thanks,

Brett
 
J

James Stroud

Do you really want quotation marks around "titlelist1[x][1]" ?

e.g.
textfilelist = [["titlelist1[x][1]"]]
textfilelist[0][1]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IndexError: list index out of range



I am trying to pass the value of a nested list into a function (to my "if"
statement) as follows:

textfilelist = [["titlelist1[x][1]"]]
def idfer(listlength, comparelistlength, list):
while x < (listlength - 1):
while y < comparelistlength:
if list == titlelist2[y][1]:
(I cutoff the end to focus on the problem)

#Here i call my function hoping i am passing the value within
textfilelist[0][0] to my function
idfer(textfilelist[0][1], textfilelist[1][1], textfilelist[0][0])


--------------
I don't if there is a "legal" way to do this, but I would appreciate some
guidance.

Thanks,

Brett

--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
B

Brett

I think so. I later reference it as textfilelist[0][0]. My intent is to be
able to use: titlelist1[x][1] as part of my "if" statement in my function.

-Brett



James Stroud said:
Do you really want quotation marks around "titlelist1[x][1]" ?

e.g.
textfilelist = [["titlelist1[x][1]"]]
textfilelist[0][1]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IndexError: list index out of range



I am trying to pass the value of a nested list into a function (to my
"if"
statement) as follows:

textfilelist = [["titlelist1[x][1]"]]
def idfer(listlength, comparelistlength, list):
while x < (listlength - 1):
while y < comparelistlength:
if list == titlelist2[y][1]:
(I cutoff the end to focus on the problem)

#Here i call my function hoping i am passing the value within
textfilelist[0][0] to my function
idfer(textfilelist[0][1], textfilelist[1][1], textfilelist[0][0])


--------------
I don't if there is a "legal" way to do this, but I would appreciate some
guidance.

Thanks,

Brett

--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
S

Sean

Brett,

Hard to tell exactly what you're trying to do here, but it looks like
you'd be better served using one of the built in python data
structures.

For example: If you're trying to compare some elements of these
textfiles that are broken into titles, and contents for each file, try
something like:

myfiles = [ 'test.txt', 'test1.txt', 'test2.txt', 'test3.txt',
'test4.txt' ]
myfiledict = {}

for filename in myfiles:
openfile = file(filename)
myfiledict[filename] = [ line.strip() for line in
openfile.readlines()]
openfile.close()
# easier to read this way
# self.content = []
# for line in f.readlines():
# self.content.append(line.strip())


# then the contents of the file "test.txt" are accessable with the
expression:
# myfiledict['test.txt']

# if you make a few lists of files, then you can compare them like this

for k1, k2 in myfiledict, cmpfiledict:
# if list == titlelist2[y][1]: From your code. The line below is
what
# I think you want.
if k1 == k2:
# Whatever happens in your code that you've clipped.

If you post the rest of your code, or email me, then I'll be happy to
answer your question.

-S
 
D

Dennis Lee Bieber

I am trying to pass the value of a nested list into a function (to my "if"
statement) as follows:

textfilelist = [["titlelist1[x][1]"]]

This list consists of one element -- a character string
"titlelist1[x][1]"
def idfer(listlength, comparelistlength, list):

Don't use "list" as a variable, you are overriding a built-in
function.
while x < (listlength - 1):

Is there any chance you want a for loop rather than a while
loop? "x" does not have a value here, and nowhere do you assign it one.

for x in range(listlength): # range returns 0..arg-1
while y < comparelistlength:

for y in range(comparelistlength):
if list == titlelist2[y][1]:

And where is "titlelist2" defined
(I cutoff the end to focus on the problem)

#Here i call my function hoping i am passing the value within
textfilelist[0][0] to my function
idfer(textfilelist[0][1], textfilelist[1][1], textfilelist[0][0])

There are no elements for [1] (in either position)

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top