2

All right, I'm a novice at python, and here's the snippet of code in question:

<!-- language: lang-py -->

List = [["W","w"],["A","A"],["a","a"]]

def ascii():
    x = 0
    y = 0
    aValues = [[],[],[]]
    for item in List:
        for item in List[x]:
            c = "0" 
            c = ord(List[x[y]])
            y = y + 1
            aValues[x].append(c)
        x = x + 1

    return aValues
aValues = ascii()
print (aValues)

And, when I try to execute this, I get this error message:

>>> 
Traceback (most recent call last):
  File "/Users/Hersh/Desktop/Python/ascii_conversion.py", line 16, in <module>
    aValues = ascii()
  File "/Users/Hersh/Desktop/Python/ascii_conversion.py", line 10, in ascii
    c = ord(List[x[y]])
TypeError: 'int' object is not subscriptable
>>> 

What exactly is the problem, and how can I fix it?

6 Answers 6

2

As indicated in the error message, the erroneous line is

c = ord(List[x[y]])

x is an integer (like 0). Instead, you want:

c = ord(List[x][y])

i.e. take the x-th element of List (which is itself a list), and take the y-th element of that.

However, your method of iteration is very unpythonic. You never use the item variables, but you should. For example, a shorter way of writing the line is:

c = ord(item)

By using map and list comprehensions, you can cut down your code to:

def ascii():
   return [map(ord, l) for l in List]
Sign up to request clarification or add additional context in comments.

1 Comment

Its unclear what version of Python the OP is using, but its worthwhile to note that your map and listcomp solution will behave differently in Py3 - it will give you a list of iterators rather than nested lists. For cross-compatibility and, arguably, for shear clarity that it is nested lists, nested list comprehensions might be preferable: [[ord(m) for m in l] for l in List].
1

I'm not sure of what exactly you intend to do with the function, it has several errors. Try this and tell me if this is what you wanted:

List = [["W", "w"], ["A", "A"], ["a", "a"]]
aValues = [[ord(e1), ord(e2)] for e1, e2 in List]
print(aValues)

EDIT 1 :

Alternatively, if each sublist contains more than two elements, this version is better and will work for the general case:

aValues = [map(ord, pair) for pair in List]

EDIT 2 :

According to the comments, you need to use a function. All right, then let's implement the solution as a function - first thing, the input for the function should be received as a parameter, not as a global variable (List) as you have currently in your code. Then, the result will be returned, and I'll take the opportunity to show yet another way to solve the problem at hand:

def ascii(lst):
    return [[ord(element) for element in pair] for pair in lst]

Use it like this:

List = [["W", "w"], ["A", "A"], ["a", "a"]]
ascii(List)
> [[87, 119], [65, 65], [97, 97]]

4 Comments

I liked the previous version better. If all(len(e) == 2 for e in List), then you should really be using tuples instead of lists.
It achieves the goal, more or less. I was told to always do my code in functions. (This is part of a larger segment of code, of course)
@HershS. I updated my answer, this time using a function. If it's what you needed, please don't forget to accept the answer that was most helpful (click the check mark to its left)
Yeah, this was helpful, a lot! I didn't know I could do it that way, thought I had to do it separately. Thanks!
1

Hmm. Unfortunately you have quite a lot of issues here. They mostly stem from your misunderstanding of loops in Python.

When you do for item in List, item is set to each element in the list in turn. So, you can't then do for item in List[x] on the next line - that makes no sense. item is the inner list already - so you want to do for inner_item in item (or, call your outer list variable something more sensible).

The next two lines make no sense either. There's no point setting c to "0" then immediately setting it to something else. And don't forget, as I said above, you already have the item in the inner loop, which we've called inner_item. So your code should read c = ord(inner_item).

Hope this helps.

Comments

1

The correct way to index into a 2-dimensional list is not:

c = ord(List[x[y]])

But instead:

c = ord(List[x][y])

Your error comes from the fact that x[y] is an invalid sub-expression, because x is an integer and [] is the subscript operator. You cannot subscript an integer.

However, you don't actually need to index into your list to accomplish the same thing:

def ascii():
    x = 0 ## redundant
    y = 0 ## redundant
    aValues = [[],[],[]]
    for item in List:
        for item in List[x]: ## you are reusing 'item', change the name here to 'subItem'
            c = "0" ## redundant
            c = ord(List[x[y]]) ## redundant, replace with: c = ord(subItem)
            y = y + 1 ## redundant
            aValues[x].append(c)
        x = x + 1 ## redundant

Comments

0

Change line 10 to

c = ord(item)

Comments

0

x is not actually an array. It's an integer that you're incrementing.

You need to figure out what it is that you're trying to do, and look up the appropriate syntax for it. It's not very clear what x[y] is supposed to be representing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.