TypeError: 'type' object is not subscriptable, when checking length of a nested-list
I have been getting a TypeError
when running the code below. I don't understand why it won't take the length of listLicencePlates
, when its a list
? I tried to figure out for about an hour now and can't seem to see the mistake. Maybe someone can point out why this happens and how to fix it?
When I run this code:
for x in range[len(listLicencePlates)]:
I get this error:
TypeError: 'type' object is not subscriptable
Here's my full code:
def match_list(witnessedLicensePlate, listLicencePlates):
matchPlates = len(listLicencePlates)*[len((witnessedLicensePlate))*[""]]
for x in range[len(listLicencePlates)]:
y = 0
for y in range(len(witnessedLicensePlate)):
if witnessedLicensePlate[y] != listLicencePlates[x][y] and witnessedLicensePlate[y] != "?":
y = len(witnessedLicensePlate)
else:
matchPlates[x][y] = listLicencePlates[x][y]
return matchPlates
print(match_list("VF???55",["VX33322","VF12355","VF77455","DA?????","VF10055"]))
python typeerror nested-lists
add a comment |
I have been getting a TypeError
when running the code below. I don't understand why it won't take the length of listLicencePlates
, when its a list
? I tried to figure out for about an hour now and can't seem to see the mistake. Maybe someone can point out why this happens and how to fix it?
When I run this code:
for x in range[len(listLicencePlates)]:
I get this error:
TypeError: 'type' object is not subscriptable
Here's my full code:
def match_list(witnessedLicensePlate, listLicencePlates):
matchPlates = len(listLicencePlates)*[len((witnessedLicensePlate))*[""]]
for x in range[len(listLicencePlates)]:
y = 0
for y in range(len(witnessedLicensePlate)):
if witnessedLicensePlate[y] != listLicencePlates[x][y] and witnessedLicensePlate[y] != "?":
y = len(witnessedLicensePlate)
else:
matchPlates[x][y] = listLicencePlates[x][y]
return matchPlates
print(match_list("VF???55",["VX33322","VF12355","VF77455","DA?????","VF10055"]))
python typeerror nested-lists
7
range(
, notrange[
.
– melpomene
Nov 27 '18 at 22:10
3
But anyway, iterating over range(len(something)) is almost always the wrong thing to do in Python.
– Daniel Roseman
Nov 27 '18 at 22:17
add a comment |
I have been getting a TypeError
when running the code below. I don't understand why it won't take the length of listLicencePlates
, when its a list
? I tried to figure out for about an hour now and can't seem to see the mistake. Maybe someone can point out why this happens and how to fix it?
When I run this code:
for x in range[len(listLicencePlates)]:
I get this error:
TypeError: 'type' object is not subscriptable
Here's my full code:
def match_list(witnessedLicensePlate, listLicencePlates):
matchPlates = len(listLicencePlates)*[len((witnessedLicensePlate))*[""]]
for x in range[len(listLicencePlates)]:
y = 0
for y in range(len(witnessedLicensePlate)):
if witnessedLicensePlate[y] != listLicencePlates[x][y] and witnessedLicensePlate[y] != "?":
y = len(witnessedLicensePlate)
else:
matchPlates[x][y] = listLicencePlates[x][y]
return matchPlates
print(match_list("VF???55",["VX33322","VF12355","VF77455","DA?????","VF10055"]))
python typeerror nested-lists
I have been getting a TypeError
when running the code below. I don't understand why it won't take the length of listLicencePlates
, when its a list
? I tried to figure out for about an hour now and can't seem to see the mistake. Maybe someone can point out why this happens and how to fix it?
When I run this code:
for x in range[len(listLicencePlates)]:
I get this error:
TypeError: 'type' object is not subscriptable
Here's my full code:
def match_list(witnessedLicensePlate, listLicencePlates):
matchPlates = len(listLicencePlates)*[len((witnessedLicensePlate))*[""]]
for x in range[len(listLicencePlates)]:
y = 0
for y in range(len(witnessedLicensePlate)):
if witnessedLicensePlate[y] != listLicencePlates[x][y] and witnessedLicensePlate[y] != "?":
y = len(witnessedLicensePlate)
else:
matchPlates[x][y] = listLicencePlates[x][y]
return matchPlates
print(match_list("VF???55",["VX33322","VF12355","VF77455","DA?????","VF10055"]))
python typeerror nested-lists
python typeerror nested-lists
edited Nov 27 '18 at 23:13
Supa Mega Ducky Momo da Waffle
2,00651129
2,00651129
asked Nov 27 '18 at 22:04
Sindre KateSindre Kate
9
9
7
range(
, notrange[
.
– melpomene
Nov 27 '18 at 22:10
3
But anyway, iterating over range(len(something)) is almost always the wrong thing to do in Python.
– Daniel Roseman
Nov 27 '18 at 22:17
add a comment |
7
range(
, notrange[
.
– melpomene
Nov 27 '18 at 22:10
3
But anyway, iterating over range(len(something)) is almost always the wrong thing to do in Python.
– Daniel Roseman
Nov 27 '18 at 22:17
7
7
range(
, not range[
.– melpomene
Nov 27 '18 at 22:10
range(
, not range[
.– melpomene
Nov 27 '18 at 22:10
3
3
But anyway, iterating over range(len(something)) is almost always the wrong thing to do in Python.
– Daniel Roseman
Nov 27 '18 at 22:17
But anyway, iterating over range(len(something)) is almost always the wrong thing to do in Python.
– Daniel Roseman
Nov 27 '18 at 22:17
add a comment |
2 Answers
2
active
oldest
votes
I dont know exactly what you want to achieve but I ran the same code and it works fine. Although I must say, I changed range[ to range( as corrected above.
add a comment |
Fixing the TypeError:
The interpreter is telling you exactly where and why you're getting the issue. Here's your MVCE:
>>> range[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable
You can't use square brackets, or subscripts (i.e. ) on the built-in
range
. Rather, use parenthesis (()
):
>>> range(1)
range(0, 1)
So change this line:
for x in range[len(listLicencePlates)] # bad news, range[...]
to be
for x in range(len(listLicencePlates)) # Better, range(...)
and you'll get past the TypeError
.
Fixing the core issue:
I am guessing, however, that the output you get from this fix isn't what you want. This is the output:
[['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5']]
Which doesn't look useful.
What I think you probably want is an array of bools
, where each bool
describes whether or not the plate matched character by character, and the "?"
represents a wildcard character.
Let's try a more Pythonic and less C-style approach to the problem with some extraordinarily verbose comments:
def match_list2(witnessed_plate, license_plates):
matching_plates =
# just loop through the items, no need for range/indices
for possible_match in license_plates:
# If they match, create 2-elt list with
# the plate as first elt and whether or not it matches as second
# Worry about do_plates_match later. Just get the high level logic
# down pat and sound first.
matching_plates.append([possible_match,
do_plates_match(possible_match, witnessed_plate)])
return matching_plates
def do_plates_match(plate1, plate2): # Define if they match (the time to worry is upon us)
# Loop through characters from plate1 and plate2 simultaneously
for first_char, second_char in zip(plate1, plate2):
if first_char == "?" or second_char == "?":
# If we get a question mark, go to the next character
continue
elif first_char != second_char:
# If they don't match at *some* character,
# we know they can't match regardless of what's next, so return False
return False
# Only way to get here is we must have matched for all characters, return True.
return True
and for those who find the comments distracting and would rather just read the code straight away:
def match_list2(witnessed_plate, license_plates):
matching_plates =
for possible_match in license_plates:
matching_plates.append([possible_match, do_plates_match(possible_match, witnessed_plate)])
return matching_plates
def do_plates_match(plate1, plate2):
for first_char, second_char in zip(plate1, plate2):
if first_char == "?" or second_char == "?": continue
elif first_char != second_char: return False
return True
Now calling:
plate = "VF???55"
print("Plate: ", plate)
print(match_list2(plate,["VX33322","VF12355","VF77455","DA?????","VF10055"]))
outputs:
Plate: VF???55
[['VX33322', False], ['VF12355', True], ['VF77455', True], ['DA?????', False], ['VF10055', True]]
clearly showing which plates match, and which don't.
HTH.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53508923%2ftypeerror-type-object-is-not-subscriptable-when-checking-length-of-a-nested%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I dont know exactly what you want to achieve but I ran the same code and it works fine. Although I must say, I changed range[ to range( as corrected above.
add a comment |
I dont know exactly what you want to achieve but I ran the same code and it works fine. Although I must say, I changed range[ to range( as corrected above.
add a comment |
I dont know exactly what you want to achieve but I ran the same code and it works fine. Although I must say, I changed range[ to range( as corrected above.
I dont know exactly what you want to achieve but I ran the same code and it works fine. Although I must say, I changed range[ to range( as corrected above.
answered Nov 27 '18 at 22:22
Mr.TeeyMr.Teey
112
112
add a comment |
add a comment |
Fixing the TypeError:
The interpreter is telling you exactly where and why you're getting the issue. Here's your MVCE:
>>> range[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable
You can't use square brackets, or subscripts (i.e. ) on the built-in
range
. Rather, use parenthesis (()
):
>>> range(1)
range(0, 1)
So change this line:
for x in range[len(listLicencePlates)] # bad news, range[...]
to be
for x in range(len(listLicencePlates)) # Better, range(...)
and you'll get past the TypeError
.
Fixing the core issue:
I am guessing, however, that the output you get from this fix isn't what you want. This is the output:
[['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5']]
Which doesn't look useful.
What I think you probably want is an array of bools
, where each bool
describes whether or not the plate matched character by character, and the "?"
represents a wildcard character.
Let's try a more Pythonic and less C-style approach to the problem with some extraordinarily verbose comments:
def match_list2(witnessed_plate, license_plates):
matching_plates =
# just loop through the items, no need for range/indices
for possible_match in license_plates:
# If they match, create 2-elt list with
# the plate as first elt and whether or not it matches as second
# Worry about do_plates_match later. Just get the high level logic
# down pat and sound first.
matching_plates.append([possible_match,
do_plates_match(possible_match, witnessed_plate)])
return matching_plates
def do_plates_match(plate1, plate2): # Define if they match (the time to worry is upon us)
# Loop through characters from plate1 and plate2 simultaneously
for first_char, second_char in zip(plate1, plate2):
if first_char == "?" or second_char == "?":
# If we get a question mark, go to the next character
continue
elif first_char != second_char:
# If they don't match at *some* character,
# we know they can't match regardless of what's next, so return False
return False
# Only way to get here is we must have matched for all characters, return True.
return True
and for those who find the comments distracting and would rather just read the code straight away:
def match_list2(witnessed_plate, license_plates):
matching_plates =
for possible_match in license_plates:
matching_plates.append([possible_match, do_plates_match(possible_match, witnessed_plate)])
return matching_plates
def do_plates_match(plate1, plate2):
for first_char, second_char in zip(plate1, plate2):
if first_char == "?" or second_char == "?": continue
elif first_char != second_char: return False
return True
Now calling:
plate = "VF???55"
print("Plate: ", plate)
print(match_list2(plate,["VX33322","VF12355","VF77455","DA?????","VF10055"]))
outputs:
Plate: VF???55
[['VX33322', False], ['VF12355', True], ['VF77455', True], ['DA?????', False], ['VF10055', True]]
clearly showing which plates match, and which don't.
HTH.
add a comment |
Fixing the TypeError:
The interpreter is telling you exactly where and why you're getting the issue. Here's your MVCE:
>>> range[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable
You can't use square brackets, or subscripts (i.e. ) on the built-in
range
. Rather, use parenthesis (()
):
>>> range(1)
range(0, 1)
So change this line:
for x in range[len(listLicencePlates)] # bad news, range[...]
to be
for x in range(len(listLicencePlates)) # Better, range(...)
and you'll get past the TypeError
.
Fixing the core issue:
I am guessing, however, that the output you get from this fix isn't what you want. This is the output:
[['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5']]
Which doesn't look useful.
What I think you probably want is an array of bools
, where each bool
describes whether or not the plate matched character by character, and the "?"
represents a wildcard character.
Let's try a more Pythonic and less C-style approach to the problem with some extraordinarily verbose comments:
def match_list2(witnessed_plate, license_plates):
matching_plates =
# just loop through the items, no need for range/indices
for possible_match in license_plates:
# If they match, create 2-elt list with
# the plate as first elt and whether or not it matches as second
# Worry about do_plates_match later. Just get the high level logic
# down pat and sound first.
matching_plates.append([possible_match,
do_plates_match(possible_match, witnessed_plate)])
return matching_plates
def do_plates_match(plate1, plate2): # Define if they match (the time to worry is upon us)
# Loop through characters from plate1 and plate2 simultaneously
for first_char, second_char in zip(plate1, plate2):
if first_char == "?" or second_char == "?":
# If we get a question mark, go to the next character
continue
elif first_char != second_char:
# If they don't match at *some* character,
# we know they can't match regardless of what's next, so return False
return False
# Only way to get here is we must have matched for all characters, return True.
return True
and for those who find the comments distracting and would rather just read the code straight away:
def match_list2(witnessed_plate, license_plates):
matching_plates =
for possible_match in license_plates:
matching_plates.append([possible_match, do_plates_match(possible_match, witnessed_plate)])
return matching_plates
def do_plates_match(plate1, plate2):
for first_char, second_char in zip(plate1, plate2):
if first_char == "?" or second_char == "?": continue
elif first_char != second_char: return False
return True
Now calling:
plate = "VF???55"
print("Plate: ", plate)
print(match_list2(plate,["VX33322","VF12355","VF77455","DA?????","VF10055"]))
outputs:
Plate: VF???55
[['VX33322', False], ['VF12355', True], ['VF77455', True], ['DA?????', False], ['VF10055', True]]
clearly showing which plates match, and which don't.
HTH.
add a comment |
Fixing the TypeError:
The interpreter is telling you exactly where and why you're getting the issue. Here's your MVCE:
>>> range[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable
You can't use square brackets, or subscripts (i.e. ) on the built-in
range
. Rather, use parenthesis (()
):
>>> range(1)
range(0, 1)
So change this line:
for x in range[len(listLicencePlates)] # bad news, range[...]
to be
for x in range(len(listLicencePlates)) # Better, range(...)
and you'll get past the TypeError
.
Fixing the core issue:
I am guessing, however, that the output you get from this fix isn't what you want. This is the output:
[['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5']]
Which doesn't look useful.
What I think you probably want is an array of bools
, where each bool
describes whether or not the plate matched character by character, and the "?"
represents a wildcard character.
Let's try a more Pythonic and less C-style approach to the problem with some extraordinarily verbose comments:
def match_list2(witnessed_plate, license_plates):
matching_plates =
# just loop through the items, no need for range/indices
for possible_match in license_plates:
# If they match, create 2-elt list with
# the plate as first elt and whether or not it matches as second
# Worry about do_plates_match later. Just get the high level logic
# down pat and sound first.
matching_plates.append([possible_match,
do_plates_match(possible_match, witnessed_plate)])
return matching_plates
def do_plates_match(plate1, plate2): # Define if they match (the time to worry is upon us)
# Loop through characters from plate1 and plate2 simultaneously
for first_char, second_char in zip(plate1, plate2):
if first_char == "?" or second_char == "?":
# If we get a question mark, go to the next character
continue
elif first_char != second_char:
# If they don't match at *some* character,
# we know they can't match regardless of what's next, so return False
return False
# Only way to get here is we must have matched for all characters, return True.
return True
and for those who find the comments distracting and would rather just read the code straight away:
def match_list2(witnessed_plate, license_plates):
matching_plates =
for possible_match in license_plates:
matching_plates.append([possible_match, do_plates_match(possible_match, witnessed_plate)])
return matching_plates
def do_plates_match(plate1, plate2):
for first_char, second_char in zip(plate1, plate2):
if first_char == "?" or second_char == "?": continue
elif first_char != second_char: return False
return True
Now calling:
plate = "VF???55"
print("Plate: ", plate)
print(match_list2(plate,["VX33322","VF12355","VF77455","DA?????","VF10055"]))
outputs:
Plate: VF???55
[['VX33322', False], ['VF12355', True], ['VF77455', True], ['DA?????', False], ['VF10055', True]]
clearly showing which plates match, and which don't.
HTH.
Fixing the TypeError:
The interpreter is telling you exactly where and why you're getting the issue. Here's your MVCE:
>>> range[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable
You can't use square brackets, or subscripts (i.e. ) on the built-in
range
. Rather, use parenthesis (()
):
>>> range(1)
range(0, 1)
So change this line:
for x in range[len(listLicencePlates)] # bad news, range[...]
to be
for x in range(len(listLicencePlates)) # Better, range(...)
and you'll get past the TypeError
.
Fixing the core issue:
I am guessing, however, that the output you get from this fix isn't what you want. This is the output:
[['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5'], ['V', 'F', '1', '0', '0', '5', '5']]
Which doesn't look useful.
What I think you probably want is an array of bools
, where each bool
describes whether or not the plate matched character by character, and the "?"
represents a wildcard character.
Let's try a more Pythonic and less C-style approach to the problem with some extraordinarily verbose comments:
def match_list2(witnessed_plate, license_plates):
matching_plates =
# just loop through the items, no need for range/indices
for possible_match in license_plates:
# If they match, create 2-elt list with
# the plate as first elt and whether or not it matches as second
# Worry about do_plates_match later. Just get the high level logic
# down pat and sound first.
matching_plates.append([possible_match,
do_plates_match(possible_match, witnessed_plate)])
return matching_plates
def do_plates_match(plate1, plate2): # Define if they match (the time to worry is upon us)
# Loop through characters from plate1 and plate2 simultaneously
for first_char, second_char in zip(plate1, plate2):
if first_char == "?" or second_char == "?":
# If we get a question mark, go to the next character
continue
elif first_char != second_char:
# If they don't match at *some* character,
# we know they can't match regardless of what's next, so return False
return False
# Only way to get here is we must have matched for all characters, return True.
return True
and for those who find the comments distracting and would rather just read the code straight away:
def match_list2(witnessed_plate, license_plates):
matching_plates =
for possible_match in license_plates:
matching_plates.append([possible_match, do_plates_match(possible_match, witnessed_plate)])
return matching_plates
def do_plates_match(plate1, plate2):
for first_char, second_char in zip(plate1, plate2):
if first_char == "?" or second_char == "?": continue
elif first_char != second_char: return False
return True
Now calling:
plate = "VF???55"
print("Plate: ", plate)
print(match_list2(plate,["VX33322","VF12355","VF77455","DA?????","VF10055"]))
outputs:
Plate: VF???55
[['VX33322', False], ['VF12355', True], ['VF77455', True], ['DA?????', False], ['VF10055', True]]
clearly showing which plates match, and which don't.
HTH.
answered Nov 27 '18 at 22:58
Matt MessersmithMatt Messersmith
6,24921832
6,24921832
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53508923%2ftypeerror-type-object-is-not-subscriptable-when-checking-length-of-a-nested%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
7
range(
, notrange[
.– melpomene
Nov 27 '18 at 22:10
3
But anyway, iterating over range(len(something)) is almost always the wrong thing to do in Python.
– Daniel Roseman
Nov 27 '18 at 22:17