Tuples indexing in python
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am trying to to put json data into a tuple and print it.
dict_all = json.loads(jsonFormat)
index = 0
for data in dict_all:
key = tuple((dict_all[index]['package_size_code'],
dict_all[index]['fda_ther_equiv_code'],
dict_all[index]['fda_application_number'],
dict_all[index]['clotting_factor_indicator'],
dict_all[index]['year'],
dict_all[index]['fda_product_name'],
dict_all[index]['labeler_name'],
...
index = index + 1
print(key)
The output is:
('60', 'NR', '204153', 'N', '2018', 'LUZU Cream 1% 60gm', 'MEDICIS DERMATOLOGICS, INC.', '99207085060', '0850', 'GM', '2013-11-14T00:00:00', '2014-03-14T00:00:00', 'N', '2014-03-14T00:00:00', '60000', '99207', '1', 'S', '2', '3')
('60', 'AB', '21758', 'N', '2018', 'VANOS CREAM .1%', 'MEDICIS DERMATOLOGICS, INC.', '99207052560', '0525', 'GM', '2005-02-11T00:00:00', '2005-02-21T00:00:00', 'N', '2005-02-21T00:00:00', '60000', '99207', '1', 'I', '2', '3')
....
Seems fine so far. But when I try to print key[0], I get values:
60
60
...
Shouldn't it be:
('60', 'NR', '204153', 'N', '2018', 'LUZU Cream 1% 60gm', 'MEDICIS DERMATOLOGICS, INC.', '99207085060', '0850', 'GM', '2013-11-14T00:00:00', '2014-03-14T00:00:00', 'N', '2014-03-14T00:00:00', '60000', '99207', '1', 'S', '2', '3')
One row is a element in the tuple. I would like it to be as shown above. Not sure where I am going wrong.
python python-3.x
|
show 1 more comment
I am trying to to put json data into a tuple and print it.
dict_all = json.loads(jsonFormat)
index = 0
for data in dict_all:
key = tuple((dict_all[index]['package_size_code'],
dict_all[index]['fda_ther_equiv_code'],
dict_all[index]['fda_application_number'],
dict_all[index]['clotting_factor_indicator'],
dict_all[index]['year'],
dict_all[index]['fda_product_name'],
dict_all[index]['labeler_name'],
...
index = index + 1
print(key)
The output is:
('60', 'NR', '204153', 'N', '2018', 'LUZU Cream 1% 60gm', 'MEDICIS DERMATOLOGICS, INC.', '99207085060', '0850', 'GM', '2013-11-14T00:00:00', '2014-03-14T00:00:00', 'N', '2014-03-14T00:00:00', '60000', '99207', '1', 'S', '2', '3')
('60', 'AB', '21758', 'N', '2018', 'VANOS CREAM .1%', 'MEDICIS DERMATOLOGICS, INC.', '99207052560', '0525', 'GM', '2005-02-11T00:00:00', '2005-02-21T00:00:00', 'N', '2005-02-21T00:00:00', '60000', '99207', '1', 'I', '2', '3')
....
Seems fine so far. But when I try to print key[0], I get values:
60
60
...
Shouldn't it be:
('60', 'NR', '204153', 'N', '2018', 'LUZU Cream 1% 60gm', 'MEDICIS DERMATOLOGICS, INC.', '99207085060', '0850', 'GM', '2013-11-14T00:00:00', '2014-03-14T00:00:00', 'N', '2014-03-14T00:00:00', '60000', '99207', '1', 'S', '2', '3')
One row is a element in the tuple. I would like it to be as shown above. Not sure where I am going wrong.
python python-3.x
1
key
is just a single tuple - you see several because you callprint(key)
for each index! Usingkey[0]
gets you the first element of the key tuple, but again once for each index.
– MisterMiyagi
Nov 16 '18 at 13:48
You are not saving yourkey
variable anywhere. You just overwrite it each time round the loop.
– quamrana
Nov 16 '18 at 13:48
No,key
is first('60', 'NR', '204153', ...)
and you dokey[0]
which is60
. Next time,key
is the next tuple andkey[0]
is still60
.
– Austin
Nov 16 '18 at 13:48
Ohhh the print placed in the loop confused me. I thought it was outside. Still not used to python synthax.
– Avinash Prabhakar
Nov 16 '18 at 13:50
1
Actually, the print you've provided here is outside the loop, so this code should have only printed the last value ofkey
.
– glibdud
Nov 16 '18 at 13:51
|
show 1 more comment
I am trying to to put json data into a tuple and print it.
dict_all = json.loads(jsonFormat)
index = 0
for data in dict_all:
key = tuple((dict_all[index]['package_size_code'],
dict_all[index]['fda_ther_equiv_code'],
dict_all[index]['fda_application_number'],
dict_all[index]['clotting_factor_indicator'],
dict_all[index]['year'],
dict_all[index]['fda_product_name'],
dict_all[index]['labeler_name'],
...
index = index + 1
print(key)
The output is:
('60', 'NR', '204153', 'N', '2018', 'LUZU Cream 1% 60gm', 'MEDICIS DERMATOLOGICS, INC.', '99207085060', '0850', 'GM', '2013-11-14T00:00:00', '2014-03-14T00:00:00', 'N', '2014-03-14T00:00:00', '60000', '99207', '1', 'S', '2', '3')
('60', 'AB', '21758', 'N', '2018', 'VANOS CREAM .1%', 'MEDICIS DERMATOLOGICS, INC.', '99207052560', '0525', 'GM', '2005-02-11T00:00:00', '2005-02-21T00:00:00', 'N', '2005-02-21T00:00:00', '60000', '99207', '1', 'I', '2', '3')
....
Seems fine so far. But when I try to print key[0], I get values:
60
60
...
Shouldn't it be:
('60', 'NR', '204153', 'N', '2018', 'LUZU Cream 1% 60gm', 'MEDICIS DERMATOLOGICS, INC.', '99207085060', '0850', 'GM', '2013-11-14T00:00:00', '2014-03-14T00:00:00', 'N', '2014-03-14T00:00:00', '60000', '99207', '1', 'S', '2', '3')
One row is a element in the tuple. I would like it to be as shown above. Not sure where I am going wrong.
python python-3.x
I am trying to to put json data into a tuple and print it.
dict_all = json.loads(jsonFormat)
index = 0
for data in dict_all:
key = tuple((dict_all[index]['package_size_code'],
dict_all[index]['fda_ther_equiv_code'],
dict_all[index]['fda_application_number'],
dict_all[index]['clotting_factor_indicator'],
dict_all[index]['year'],
dict_all[index]['fda_product_name'],
dict_all[index]['labeler_name'],
...
index = index + 1
print(key)
The output is:
('60', 'NR', '204153', 'N', '2018', 'LUZU Cream 1% 60gm', 'MEDICIS DERMATOLOGICS, INC.', '99207085060', '0850', 'GM', '2013-11-14T00:00:00', '2014-03-14T00:00:00', 'N', '2014-03-14T00:00:00', '60000', '99207', '1', 'S', '2', '3')
('60', 'AB', '21758', 'N', '2018', 'VANOS CREAM .1%', 'MEDICIS DERMATOLOGICS, INC.', '99207052560', '0525', 'GM', '2005-02-11T00:00:00', '2005-02-21T00:00:00', 'N', '2005-02-21T00:00:00', '60000', '99207', '1', 'I', '2', '3')
....
Seems fine so far. But when I try to print key[0], I get values:
60
60
...
Shouldn't it be:
('60', 'NR', '204153', 'N', '2018', 'LUZU Cream 1% 60gm', 'MEDICIS DERMATOLOGICS, INC.', '99207085060', '0850', 'GM', '2013-11-14T00:00:00', '2014-03-14T00:00:00', 'N', '2014-03-14T00:00:00', '60000', '99207', '1', 'S', '2', '3')
One row is a element in the tuple. I would like it to be as shown above. Not sure where I am going wrong.
python python-3.x
python python-3.x
asked Nov 16 '18 at 13:45
Avinash PrabhakarAvinash Prabhakar
13413
13413
1
key
is just a single tuple - you see several because you callprint(key)
for each index! Usingkey[0]
gets you the first element of the key tuple, but again once for each index.
– MisterMiyagi
Nov 16 '18 at 13:48
You are not saving yourkey
variable anywhere. You just overwrite it each time round the loop.
– quamrana
Nov 16 '18 at 13:48
No,key
is first('60', 'NR', '204153', ...)
and you dokey[0]
which is60
. Next time,key
is the next tuple andkey[0]
is still60
.
– Austin
Nov 16 '18 at 13:48
Ohhh the print placed in the loop confused me. I thought it was outside. Still not used to python synthax.
– Avinash Prabhakar
Nov 16 '18 at 13:50
1
Actually, the print you've provided here is outside the loop, so this code should have only printed the last value ofkey
.
– glibdud
Nov 16 '18 at 13:51
|
show 1 more comment
1
key
is just a single tuple - you see several because you callprint(key)
for each index! Usingkey[0]
gets you the first element of the key tuple, but again once for each index.
– MisterMiyagi
Nov 16 '18 at 13:48
You are not saving yourkey
variable anywhere. You just overwrite it each time round the loop.
– quamrana
Nov 16 '18 at 13:48
No,key
is first('60', 'NR', '204153', ...)
and you dokey[0]
which is60
. Next time,key
is the next tuple andkey[0]
is still60
.
– Austin
Nov 16 '18 at 13:48
Ohhh the print placed in the loop confused me. I thought it was outside. Still not used to python synthax.
– Avinash Prabhakar
Nov 16 '18 at 13:50
1
Actually, the print you've provided here is outside the loop, so this code should have only printed the last value ofkey
.
– glibdud
Nov 16 '18 at 13:51
1
1
key
is just a single tuple - you see several because you call print(key)
for each index! Using key[0]
gets you the first element of the key tuple, but again once for each index.– MisterMiyagi
Nov 16 '18 at 13:48
key
is just a single tuple - you see several because you call print(key)
for each index! Using key[0]
gets you the first element of the key tuple, but again once for each index.– MisterMiyagi
Nov 16 '18 at 13:48
You are not saving your
key
variable anywhere. You just overwrite it each time round the loop.– quamrana
Nov 16 '18 at 13:48
You are not saving your
key
variable anywhere. You just overwrite it each time round the loop.– quamrana
Nov 16 '18 at 13:48
No,
key
is first ('60', 'NR', '204153', ...)
and you do key[0]
which is 60
. Next time, key
is the next tuple and key[0]
is still 60
.– Austin
Nov 16 '18 at 13:48
No,
key
is first ('60', 'NR', '204153', ...)
and you do key[0]
which is 60
. Next time, key
is the next tuple and key[0]
is still 60
.– Austin
Nov 16 '18 at 13:48
Ohhh the print placed in the loop confused me. I thought it was outside. Still not used to python synthax.
– Avinash Prabhakar
Nov 16 '18 at 13:50
Ohhh the print placed in the loop confused me. I thought it was outside. Still not used to python synthax.
– Avinash Prabhakar
Nov 16 '18 at 13:50
1
1
Actually, the print you've provided here is outside the loop, so this code should have only printed the last value of
key
.– glibdud
Nov 16 '18 at 13:51
Actually, the print you've provided here is outside the loop, so this code should have only printed the last value of
key
.– glibdud
Nov 16 '18 at 13:51
|
show 1 more comment
1 Answer
1
active
oldest
votes
You might have meant to do this:
dict_all = json.loads(jsonFormat)
index = 0
key =
for data in dict_all:
key.append(tuple((dict_all[index]['package_size_code'],
dict_all[index]['fda_ther_equiv_code'],
dict_all[index]['fda_application_number'],
dict_all[index]['clotting_factor_indicator'],
dict_all[index]['year'],
dict_all[index]['fda_product_name'],
dict_all[index]['labeler_name'],
...
)
index = index + 1
print(key)
print(key[0])
The key
variable is now a list which above gets printed out once after the loop has finished. And as an example, prints the first element of the list.
Thanks. I should have used list from the very beginning.
– Avinash Prabhakar
Nov 16 '18 at 13:53
I was actually trying to use tuple as Sqlite3 uses them for inserts but it looks like list itself is enough.
– Avinash Prabhakar
Nov 16 '18 at 13:58
So tuples are not needed at all. I am just doing what you did but without the tuple thing.
– Avinash Prabhakar
Nov 16 '18 at 13:59
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%2f53339095%2ftuples-indexing-in-python%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
You might have meant to do this:
dict_all = json.loads(jsonFormat)
index = 0
key =
for data in dict_all:
key.append(tuple((dict_all[index]['package_size_code'],
dict_all[index]['fda_ther_equiv_code'],
dict_all[index]['fda_application_number'],
dict_all[index]['clotting_factor_indicator'],
dict_all[index]['year'],
dict_all[index]['fda_product_name'],
dict_all[index]['labeler_name'],
...
)
index = index + 1
print(key)
print(key[0])
The key
variable is now a list which above gets printed out once after the loop has finished. And as an example, prints the first element of the list.
Thanks. I should have used list from the very beginning.
– Avinash Prabhakar
Nov 16 '18 at 13:53
I was actually trying to use tuple as Sqlite3 uses them for inserts but it looks like list itself is enough.
– Avinash Prabhakar
Nov 16 '18 at 13:58
So tuples are not needed at all. I am just doing what you did but without the tuple thing.
– Avinash Prabhakar
Nov 16 '18 at 13:59
add a comment |
You might have meant to do this:
dict_all = json.loads(jsonFormat)
index = 0
key =
for data in dict_all:
key.append(tuple((dict_all[index]['package_size_code'],
dict_all[index]['fda_ther_equiv_code'],
dict_all[index]['fda_application_number'],
dict_all[index]['clotting_factor_indicator'],
dict_all[index]['year'],
dict_all[index]['fda_product_name'],
dict_all[index]['labeler_name'],
...
)
index = index + 1
print(key)
print(key[0])
The key
variable is now a list which above gets printed out once after the loop has finished. And as an example, prints the first element of the list.
Thanks. I should have used list from the very beginning.
– Avinash Prabhakar
Nov 16 '18 at 13:53
I was actually trying to use tuple as Sqlite3 uses them for inserts but it looks like list itself is enough.
– Avinash Prabhakar
Nov 16 '18 at 13:58
So tuples are not needed at all. I am just doing what you did but without the tuple thing.
– Avinash Prabhakar
Nov 16 '18 at 13:59
add a comment |
You might have meant to do this:
dict_all = json.loads(jsonFormat)
index = 0
key =
for data in dict_all:
key.append(tuple((dict_all[index]['package_size_code'],
dict_all[index]['fda_ther_equiv_code'],
dict_all[index]['fda_application_number'],
dict_all[index]['clotting_factor_indicator'],
dict_all[index]['year'],
dict_all[index]['fda_product_name'],
dict_all[index]['labeler_name'],
...
)
index = index + 1
print(key)
print(key[0])
The key
variable is now a list which above gets printed out once after the loop has finished. And as an example, prints the first element of the list.
You might have meant to do this:
dict_all = json.loads(jsonFormat)
index = 0
key =
for data in dict_all:
key.append(tuple((dict_all[index]['package_size_code'],
dict_all[index]['fda_ther_equiv_code'],
dict_all[index]['fda_application_number'],
dict_all[index]['clotting_factor_indicator'],
dict_all[index]['year'],
dict_all[index]['fda_product_name'],
dict_all[index]['labeler_name'],
...
)
index = index + 1
print(key)
print(key[0])
The key
variable is now a list which above gets printed out once after the loop has finished. And as an example, prints the first element of the list.
answered Nov 16 '18 at 13:50
quamranaquamrana
13.6k74054
13.6k74054
Thanks. I should have used list from the very beginning.
– Avinash Prabhakar
Nov 16 '18 at 13:53
I was actually trying to use tuple as Sqlite3 uses them for inserts but it looks like list itself is enough.
– Avinash Prabhakar
Nov 16 '18 at 13:58
So tuples are not needed at all. I am just doing what you did but without the tuple thing.
– Avinash Prabhakar
Nov 16 '18 at 13:59
add a comment |
Thanks. I should have used list from the very beginning.
– Avinash Prabhakar
Nov 16 '18 at 13:53
I was actually trying to use tuple as Sqlite3 uses them for inserts but it looks like list itself is enough.
– Avinash Prabhakar
Nov 16 '18 at 13:58
So tuples are not needed at all. I am just doing what you did but without the tuple thing.
– Avinash Prabhakar
Nov 16 '18 at 13:59
Thanks. I should have used list from the very beginning.
– Avinash Prabhakar
Nov 16 '18 at 13:53
Thanks. I should have used list from the very beginning.
– Avinash Prabhakar
Nov 16 '18 at 13:53
I was actually trying to use tuple as Sqlite3 uses them for inserts but it looks like list itself is enough.
– Avinash Prabhakar
Nov 16 '18 at 13:58
I was actually trying to use tuple as Sqlite3 uses them for inserts but it looks like list itself is enough.
– Avinash Prabhakar
Nov 16 '18 at 13:58
So tuples are not needed at all. I am just doing what you did but without the tuple thing.
– Avinash Prabhakar
Nov 16 '18 at 13:59
So tuples are not needed at all. I am just doing what you did but without the tuple thing.
– Avinash Prabhakar
Nov 16 '18 at 13:59
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%2f53339095%2ftuples-indexing-in-python%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
1
key
is just a single tuple - you see several because you callprint(key)
for each index! Usingkey[0]
gets you the first element of the key tuple, but again once for each index.– MisterMiyagi
Nov 16 '18 at 13:48
You are not saving your
key
variable anywhere. You just overwrite it each time round the loop.– quamrana
Nov 16 '18 at 13:48
No,
key
is first('60', 'NR', '204153', ...)
and you dokey[0]
which is60
. Next time,key
is the next tuple andkey[0]
is still60
.– Austin
Nov 16 '18 at 13:48
Ohhh the print placed in the loop confused me. I thought it was outside. Still not used to python synthax.
– Avinash Prabhakar
Nov 16 '18 at 13:50
1
Actually, the print you've provided here is outside the loop, so this code should have only printed the last value of
key
.– glibdud
Nov 16 '18 at 13:51