By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,525 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,525 IT Pros & Developers. It's quick & easy.

Rookie Question: Passing a nested list into a function?

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

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

On Thursday 14 April 2005 06:51 pm, Brett wrote: 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/
Jul 19 '05 #2

P: n/a
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" <js*****@mbi.ucla.edu> wrote in message
news:ma**************************************@pyth on.org...

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

On Thursday 14 April 2005 06:51 pm, Brett wrote:
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/

Jul 19 '05 #3

P: n/a
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

Jul 19 '05 #4

P: n/a
On Thu, 14 Apr 2005 20:51:08 -0500, "Brett" <bj***@yahoo.com> declaimed
the following in comp.lang.python:
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)

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.