0

I'm trying to take values from a text file that I already have read into a seperate list, and I'm trying to find the specific location of each 'E' in the text file. Every time I print the list after using the following method, it comes up empty. I'm not sure what I'm doing wrong. AGENTS is a constant equal to 6

for a in range(0,2,1):
    agentLocations.append([])
    for c in range(0,AGENTS,1):
        indices = [(i,x) for i, x in enumerate(aMaze) if x == 'E']
        agentLocations[a].append(0)
        print(indices)

The code for getting the file into its own list is as follows:

for line in file:
    for char in line:
        for r in range (0, ROWS, 1):
            aMaze.append ([])
            aMaze[r].append(char)
    print ("".join(aMaze[ROWS-1]))

The file inputted looks like

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~H                           ~
~                            ~
~                            ~
~                            ~
~#                           ~
~                            ~
~                            ~
~        E                   ~
~                            ~
~                            ~
~                            ~
~               ##### #######~
~      E        #E           ~
~               #           E~
~               #   ES       ~
~               E            ~
~                            ~
~                            ~
################

And I am looking for the index locations of the 'E' characters in the list.

6
  • Please fix your code formatting Nov 18, 2014 at 22:27
  • what is AGENTS? also range(0, ROWS, 1) == range(ROWS), without seeing some input and expected output its going to be hard to answer Nov 18, 2014 at 22:34
  • Is there a rationale or precedent for leaving a space before the parens for append(). That gives me a jolt while reading: a small thing, but not helpful. Nov 18, 2014 at 22:34
  • 1
    You need to show us how you are "printing the list". We can't guess what list you are printing or how you are printing it. Do you mean the indicies list or the agentLocations[a] list? Please see stackoverflow/help/mvce for how to ask a good question of this nature. Nov 18, 2014 at 22:36
  • I fixed the code above with the information you guys asked for. Sorry this is my first time asking a question, Thanks for your help
    – Simran Rai
    Nov 18, 2014 at 23:01

1 Answer 1

0

I think the problem is that your aMaze is not being built correctly. Here's why I think that...

To debug your code relating to the printing indicies problem, you need to first elaborate the list comprehension, then add prints.

I did that, and I found that the enumeration of aMaze produces zillions of iterations. Is that what you were expecting? It was not what I was expecting.

So then I printed aMaze itself. aMaze has zillions of empty lists in it: in fact, ROWS number of them for each character in the input file. ( aMaze.append ([]) is inside the inner loop that executes ROWs times for every character in the file).

That doesn't seem right (I had to guess what ROWS is).

Therefore your logic around building the aMaze array appears to be wrong, but it is hard to tell what is wrong with it, because it's hard to determine the intent. For example why is it that printing the ROWs rows of aMaze successfully prints the maze: obviously something is working how you expected, which makes it hard to understand what is not right from your point of view.

My answer, and recommendation: take a re-look at how you are constructing aMaze. If you can't debug it, ask a new question focussed on what is giving you trouble there.

Here is the final complete code after my debugging session.

agentLocations = []
AGENTS=1
ROWS=20

aMaze = []

with open("foo.txt") as f:
    for line in f:
        for char in line:
            for r in range (0, ROWS, 1):
                aMaze.append ([])
                aMaze[r].append(char)
    print ("".join(aMaze[ROWS-1]))

print aMaze

for a in range(0,2,1):
    agentLocations.append([])
    for c in range(0,AGENTS,1):
#        indices = [(i,x) for i, x in enumerate(aMaze) if x == 'E']
        indices = []
        for i,x in enumerate(aMaze):
            print "i,x", i,x
            if x =='E':
                indicies.append((i,x))

        agentLocations[a].append(0)
        print(indices)

Note that I had to guess what ROWS and the initial value of aMaze and agentLocations is. I also couldn't see the point of repeating c loop AGENTS times, so I set AGENTS to 1.

These were not hard to guess, but it means you still haven't mastered MVCE (as per my comment to your question). It will help you to get better help here, to master that, and you will find the act of creating a MVCE often presents the solution in the process.

Not the answer you're looking for? Browse other questions tagged or ask your own question.