Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9167013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:17:17+00:00 2026-06-17T15:17:17+00:00

I’m writing a function that parses strings into lists that get used by another

  • 0

I’m writing a function that parses strings into lists that get used by another function. One of the operations that it performs is that it attaches a character to a string inside the (sometimes deeply recursive) list at a particular depth of recursion (the depth defined by a variable named lvl). This operation, a function named listSurgery that is supposed to get called with a list of indices that indicate where the next list is inside the previous list, with the final index telling what index within the deep list to perform an operation at, is getting called with a blank list of indices, and I don’t know why. The list that it should be getting called with is [-1], but debugging shows that it’s getting called with []. Here is the code, abbreviated:

def listAssign(lst,index,item):
    """Assigns item item to list lst at index index, returns modified list."""
    lst[index] = item
    return lst

def listInsert(lst,index,item):
    """Inserts item item to list lst at index index, returns modified list."""
    print "listInsert just got called with these arguments:",lst,index,item
    if index == 'end':
        index = len(lst)
    lst.insert(index,item)
    return lst

def listSurgery(lst,indices,f,*extraArgs):
    """Performs operation f on list lst at depth at indices indices, returns modified list."""
    print "listSurgery just got called with these arguments:",lst,indices,f,extraArgs
    parent = lst
    for index in indices[:-1]:
        parent = parent[index]
    parent = f(parent,indices[-1],*extraArgs)
    return listSurgery(lst,indices[:-1],listAssign,parent)

def parseStringToList(s):
    """Takes in a user-input string, and converts it into a list to be passed into parseListToExpr."""
    # ...
    l = [] # List to build from string; built by serially appending stuff as it comes up
    b = True # Bool for whether the parser is experiencing spaces (supposed to be True if last character processed was a space)
    t = False # Bool for whether the parser is experiencing a string of non-alphanumeric characters (supposed to be True if last character was a non-alphanumeric character)
    lvl = 0 # Keeps track of depth at which operations are supposed to be occurring
    for c in s:
        if c == ' ': # If c is a space, ignore it but send signal to break off any strings currently being written to
            b = True
        # Some elifs for c being non alphanumeric
        else: # If c is alphanumeric, append it to the string it's working on
            print c,"got passed as an alphanumeric; lvl is",lvl
            assert c.isalnum()
            if b or t: # If the string it's working on isn't alphanumeric or doesn't exist, append a new string
                l = listSurgery(l,[-1]*lvl + ['end'],listInsert,'')
                b, t = False, False
            l = listSurgery(l,[-1]*(lvl+1),lambda x,y,z:listAssign(x,y,x[y]+z),c)
        print l
    return l

while op != 'exit' and op != 'quit': # Keep a REPL unless the user types "exit" or "quit", in which case exit
    op = raw_input("> ")
    if op == 'help':
        pass # Print help stuff
    elif op in {'quit','exit'}:
        pass
    else:
        print str(parseStringToList(op))

I called the code with python -tt code.py and typed 1+1=2, and this is what I got:

> 1+1=2
1 got passed as an alphanumeric; lvl is 0
listSurgery just got called with these arguments: [] ['end'] <function listInsert at 0x10e9d16e0> ('',)
listInsert just got called with these arguments: [] end 
listSurgery just got called with these arguments: [''] [] <function listAssign at 0x10e9d10c8> ([''],)
Traceback (most recent call last):
  File "analysis.py", line 276, in <module>
    print str(parseStringToList(op))
  File "analysis.py", line 218, in parseStringToList
    l = listSurgery(l,[-1]*lvl + ['end'],listInsert,'')
  File "analysis.py", line 63, in listSurgery
    return listSurgery(lst,indices[:-1],listAssign,parent)
  File "analysis.py", line 62, in listSurgery
    parent = f(parent,indices[-1],*extraArgs)
IndexError: list index out of range

Can anyone explain this? Why is listSurgery getting [] instead of [-1]? lvl is 0, and the argument that’s supposed to be passed at that point is [-1]*(lvl+1). Never even mind why it’s getting called with [''] instead of '1'.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:17:18+00:00Added an answer on June 17, 2026 at 3:17 pm

    Your lvl is 0, so [-1]*lvl + ['end'] is ['end'], and indices[:-1] is ['end'][:-1]. Now [‘end’] is a list of length 1, so ['end'][:-1] is the same thing as ['end'][:1-1], which is the same thing as ['end'][:0]. This evaluates to empty list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am confused How to use looping for Json response Array in another Array.
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.