pset6 similarities more …object does not support item assignment
I'm stuck with pset6 similarities more. When I want to insert the tuples into my matrix, I keep getting the message 'NoneType' object does not support item assignment. If I change none to 0 or to (None, None), I get the same message : int/tuple object does not support item assignment. What am I overlooking?
def distances(a, b):
"""Calculate edit distance from a to b"""
# create matrix of dimensions len(a) + 1 and len(b) + 1 and fill with zeros
matrix = [[None for i in range(len(b) + 1)] for j in range(len(a) + 1)]
matrix[0][0] = (0, None)
for x in range (1, len(a) + 1):
matrix[x][0] = (x, Operation.INSERTED)
for y in range (1, len(b) + 1):
matrix[0][y] = (y, Operation.DELETED)
for x in range(1, len(a) + 1):
for y in range(1, len(b) + 1):
if a[x - 1] == b[y - 1]:
matrix[x][y] = (min(matrix[x - 1][y - 1][0], matrix[x][y - 1][0], matrix[x - 1][y][0]), None)
else:
matrix[x][y][0] = min(matrix[x - 1][y - 1][0] + 1, matrix[x][y - 1][0] + 1, matrix[x - 1][y][0] + 1)
if matrix[x][y][0] == matrix[x - 1][y - 1][0] + 1:
matrix[x][y][1] = Operation.SUBSTITUTED
if matrix[x][y][0] == matrix[x][y - 1][0] + 1:
matrix[x][y][1] = Operation.INSERTED
else:
matrix[x][y][1] = Operation.DELETED
return matrix
python
add a comment |
I'm stuck with pset6 similarities more. When I want to insert the tuples into my matrix, I keep getting the message 'NoneType' object does not support item assignment. If I change none to 0 or to (None, None), I get the same message : int/tuple object does not support item assignment. What am I overlooking?
def distances(a, b):
"""Calculate edit distance from a to b"""
# create matrix of dimensions len(a) + 1 and len(b) + 1 and fill with zeros
matrix = [[None for i in range(len(b) + 1)] for j in range(len(a) + 1)]
matrix[0][0] = (0, None)
for x in range (1, len(a) + 1):
matrix[x][0] = (x, Operation.INSERTED)
for y in range (1, len(b) + 1):
matrix[0][y] = (y, Operation.DELETED)
for x in range(1, len(a) + 1):
for y in range(1, len(b) + 1):
if a[x - 1] == b[y - 1]:
matrix[x][y] = (min(matrix[x - 1][y - 1][0], matrix[x][y - 1][0], matrix[x - 1][y][0]), None)
else:
matrix[x][y][0] = min(matrix[x - 1][y - 1][0] + 1, matrix[x][y - 1][0] + 1, matrix[x - 1][y][0] + 1)
if matrix[x][y][0] == matrix[x - 1][y - 1][0] + 1:
matrix[x][y][1] = Operation.SUBSTITUTED
if matrix[x][y][0] == matrix[x][y - 1][0] + 1:
matrix[x][y][1] = Operation.INSERTED
else:
matrix[x][y][1] = Operation.DELETED
return matrix
python
as the error suggests, tuples can't be changed in-place, but they can be overlaid by new tuples. try creating a new tuple and assigning it tomatrix[x][y]
, rather than assigning the tuple elements themselves
– Garrett
Nov 15 '18 at 5:38
Thanks, it worked!
– Ida
Nov 15 '18 at 19:26
great! I converted my comment to an answer. If you're happy with it, accepting the answer will make the question appear resolved.
– Garrett
Nov 15 '18 at 21:19
add a comment |
I'm stuck with pset6 similarities more. When I want to insert the tuples into my matrix, I keep getting the message 'NoneType' object does not support item assignment. If I change none to 0 or to (None, None), I get the same message : int/tuple object does not support item assignment. What am I overlooking?
def distances(a, b):
"""Calculate edit distance from a to b"""
# create matrix of dimensions len(a) + 1 and len(b) + 1 and fill with zeros
matrix = [[None for i in range(len(b) + 1)] for j in range(len(a) + 1)]
matrix[0][0] = (0, None)
for x in range (1, len(a) + 1):
matrix[x][0] = (x, Operation.INSERTED)
for y in range (1, len(b) + 1):
matrix[0][y] = (y, Operation.DELETED)
for x in range(1, len(a) + 1):
for y in range(1, len(b) + 1):
if a[x - 1] == b[y - 1]:
matrix[x][y] = (min(matrix[x - 1][y - 1][0], matrix[x][y - 1][0], matrix[x - 1][y][0]), None)
else:
matrix[x][y][0] = min(matrix[x - 1][y - 1][0] + 1, matrix[x][y - 1][0] + 1, matrix[x - 1][y][0] + 1)
if matrix[x][y][0] == matrix[x - 1][y - 1][0] + 1:
matrix[x][y][1] = Operation.SUBSTITUTED
if matrix[x][y][0] == matrix[x][y - 1][0] + 1:
matrix[x][y][1] = Operation.INSERTED
else:
matrix[x][y][1] = Operation.DELETED
return matrix
python
I'm stuck with pset6 similarities more. When I want to insert the tuples into my matrix, I keep getting the message 'NoneType' object does not support item assignment. If I change none to 0 or to (None, None), I get the same message : int/tuple object does not support item assignment. What am I overlooking?
def distances(a, b):
"""Calculate edit distance from a to b"""
# create matrix of dimensions len(a) + 1 and len(b) + 1 and fill with zeros
matrix = [[None for i in range(len(b) + 1)] for j in range(len(a) + 1)]
matrix[0][0] = (0, None)
for x in range (1, len(a) + 1):
matrix[x][0] = (x, Operation.INSERTED)
for y in range (1, len(b) + 1):
matrix[0][y] = (y, Operation.DELETED)
for x in range(1, len(a) + 1):
for y in range(1, len(b) + 1):
if a[x - 1] == b[y - 1]:
matrix[x][y] = (min(matrix[x - 1][y - 1][0], matrix[x][y - 1][0], matrix[x - 1][y][0]), None)
else:
matrix[x][y][0] = min(matrix[x - 1][y - 1][0] + 1, matrix[x][y - 1][0] + 1, matrix[x - 1][y][0] + 1)
if matrix[x][y][0] == matrix[x - 1][y - 1][0] + 1:
matrix[x][y][1] = Operation.SUBSTITUTED
if matrix[x][y][0] == matrix[x][y - 1][0] + 1:
matrix[x][y][1] = Operation.INSERTED
else:
matrix[x][y][1] = Operation.DELETED
return matrix
python
python
edited Nov 15 '18 at 19:26
Ida
asked Nov 15 '18 at 2:21
IdaIda
135
135
as the error suggests, tuples can't be changed in-place, but they can be overlaid by new tuples. try creating a new tuple and assigning it tomatrix[x][y]
, rather than assigning the tuple elements themselves
– Garrett
Nov 15 '18 at 5:38
Thanks, it worked!
– Ida
Nov 15 '18 at 19:26
great! I converted my comment to an answer. If you're happy with it, accepting the answer will make the question appear resolved.
– Garrett
Nov 15 '18 at 21:19
add a comment |
as the error suggests, tuples can't be changed in-place, but they can be overlaid by new tuples. try creating a new tuple and assigning it tomatrix[x][y]
, rather than assigning the tuple elements themselves
– Garrett
Nov 15 '18 at 5:38
Thanks, it worked!
– Ida
Nov 15 '18 at 19:26
great! I converted my comment to an answer. If you're happy with it, accepting the answer will make the question appear resolved.
– Garrett
Nov 15 '18 at 21:19
as the error suggests, tuples can't be changed in-place, but they can be overlaid by new tuples. try creating a new tuple and assigning it to
matrix[x][y]
, rather than assigning the tuple elements themselves– Garrett
Nov 15 '18 at 5:38
as the error suggests, tuples can't be changed in-place, but they can be overlaid by new tuples. try creating a new tuple and assigning it to
matrix[x][y]
, rather than assigning the tuple elements themselves– Garrett
Nov 15 '18 at 5:38
Thanks, it worked!
– Ida
Nov 15 '18 at 19:26
Thanks, it worked!
– Ida
Nov 15 '18 at 19:26
great! I converted my comment to an answer. If you're happy with it, accepting the answer will make the question appear resolved.
– Garrett
Nov 15 '18 at 21:19
great! I converted my comment to an answer. If you're happy with it, accepting the answer will make the question appear resolved.
– Garrett
Nov 15 '18 at 21:19
add a comment |
1 Answer
1
active
oldest
votes
The tuple object does not support item assignment
error message means Python tuples can't be changed in-place, but they can be overlaid by new tuples. Try creating a new tuple and assigning it to matrix[x][y]
, rather than assigning the tuple elements themselves.
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%2f53311517%2fpset6-similarities-more-object-does-not-support-item-assignment%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The tuple object does not support item assignment
error message means Python tuples can't be changed in-place, but they can be overlaid by new tuples. Try creating a new tuple and assigning it to matrix[x][y]
, rather than assigning the tuple elements themselves.
add a comment |
The tuple object does not support item assignment
error message means Python tuples can't be changed in-place, but they can be overlaid by new tuples. Try creating a new tuple and assigning it to matrix[x][y]
, rather than assigning the tuple elements themselves.
add a comment |
The tuple object does not support item assignment
error message means Python tuples can't be changed in-place, but they can be overlaid by new tuples. Try creating a new tuple and assigning it to matrix[x][y]
, rather than assigning the tuple elements themselves.
The tuple object does not support item assignment
error message means Python tuples can't be changed in-place, but they can be overlaid by new tuples. Try creating a new tuple and assigning it to matrix[x][y]
, rather than assigning the tuple elements themselves.
answered Nov 15 '18 at 21:17
GarrettGarrett
22.1k34544
22.1k34544
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%2f53311517%2fpset6-similarities-more-object-does-not-support-item-assignment%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
as the error suggests, tuples can't be changed in-place, but they can be overlaid by new tuples. try creating a new tuple and assigning it to
matrix[x][y]
, rather than assigning the tuple elements themselves– Garrett
Nov 15 '18 at 5:38
Thanks, it worked!
– Ida
Nov 15 '18 at 19:26
great! I converted my comment to an answer. If you're happy with it, accepting the answer will make the question appear resolved.
– Garrett
Nov 15 '18 at 21:19