Rookie Question: Passing a nested list into a function?

Discussion in 'Python' started by Brett, Apr 15, 2005.

  1. Brett

    Brett Guest

    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
    Brett, Apr 15, 2005
    #1
    1. Advertising

  2. Brett

    James Stroud Guest

    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/
    James Stroud, Apr 15, 2005
    #2
    1. Advertising

  3. Brett

    Brett Guest

    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" <> wrote in message
    news:...
    >
    > 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/
    Brett, Apr 15, 2005
    #3
  4. Brett

    Sean Guest

    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
    Sean, Apr 15, 2005
    #4
  5. On Thu, 14 Apr 2005 20:51:08 -0500, "Brett" <> 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)

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, Apr 15, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Big E

    Rookie Question

    Big E, Jun 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    339
    avnrao
    Jun 17, 2004
  2. Greg Smith

    Rookie question

    Greg Smith, Oct 7, 2005, in forum: Java
    Replies:
    4
    Views:
    349
    Monique Y. Mudama
    Oct 8, 2005
  3. Ian
    Replies:
    4
    Views:
    377
    Ian Rutgers
    Jan 22, 2005
  4. Raskolnikow
    Replies:
    1
    Views:
    348
    Michiel Salters
    Jul 18, 2003
  5. Richard A. DeVenezia

    Nested function X vs. nested varX = function

    Richard A. DeVenezia, Sep 8, 2003, in forum: Javascript
    Replies:
    1
    Views:
    122
    Code Ronin
    Sep 9, 2003
Loading...

Share This Page