Inverted Index Python
I'm trying to create a dictionary of the form:
{: [ , {}]}
For example:
d = {term: [number, {number1: number2}]}
I tried to create the dictionary inside but I'm new and I couldn't understand how it's possible. The problem is that I want the form of d and I want to update number or the dictionary that contains number1 as key and number2 as value when finding term.
So the question is:
Is it possible to create a dictionary like d ? And if so, how can I access term, number and the inside dictionay?
python python-3.x list dictionary
add a comment |
I'm trying to create a dictionary of the form:
{: [ , {}]}
For example:
d = {term: [number, {number1: number2}]}
I tried to create the dictionary inside but I'm new and I couldn't understand how it's possible. The problem is that I want the form of d and I want to update number or the dictionary that contains number1 as key and number2 as value when finding term.
So the question is:
Is it possible to create a dictionary like d ? And if so, how can I access term, number and the inside dictionay?
python python-3.x list dictionary
add a comment |
I'm trying to create a dictionary of the form:
{: [ , {}]}
For example:
d = {term: [number, {number1: number2}]}
I tried to create the dictionary inside but I'm new and I couldn't understand how it's possible. The problem is that I want the form of d and I want to update number or the dictionary that contains number1 as key and number2 as value when finding term.
So the question is:
Is it possible to create a dictionary like d ? And if so, how can I access term, number and the inside dictionay?
python python-3.x list dictionary
I'm trying to create a dictionary of the form:
{: [ , {}]}
For example:
d = {term: [number, {number1: number2}]}
I tried to create the dictionary inside but I'm new and I couldn't understand how it's possible. The problem is that I want the form of d and I want to update number or the dictionary that contains number1 as key and number2 as value when finding term.
So the question is:
Is it possible to create a dictionary like d ? And if so, how can I access term, number and the inside dictionay?
python python-3.x list dictionary
python python-3.x list dictionary
edited Nov 19 '18 at 14:35


jpp
92.1k2053103
92.1k2053103
asked Nov 19 '18 at 14:29
Devilhorn
7519
7519
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
d = {"term": [5, {6: 7}]}
The key's value is a list:
d["term"]
[5, {6: 7}]
The list 's 1st element:
d["term"][0]
5
The list 's second element is a dictionary:
d["term"][1]
{6: 7}
The value of the dictionary's key '6' is 7:
d["term"][1][6]
7
Edit:
Some examples for modification:
d = {"term": [5, {6: 7}]}
d["term"].append(10)
print(d)
Out: {'term': [5, {6: 7}, 10]}
l=d["term"]
l[0]=55
print(d)
Out: {'term': [55, {6: 7}, 10]}
insidedict=l[1]
print(insidedict)
{6: 7}
insidedict[66]=77
print(d)
{'term': [55, {6: 7, 66: 77}, 10]}
If I have multiple keys ,say term , term1 , termk how can I add values in the inside dictionary of term or update term's numbers or update term's inside dictionary ?
– Devilhorn
Nov 19 '18 at 16:26
@Devilhorn I 've edited the code, see above.
– kantal
Nov 19 '18 at 17:24
add a comment |
Sure, just define it as you have:
d = {'term': [5, {6: 7}]}
Since your dictionary has just one key, you an access the key via:
key = next(iter(d))
You can then access the value 5
via a couple of ways:
number = d[key][0]
number = next(iter(d.values()))[0]
Similarly, you can access the inner dictionary via either:
inner_dict = d[key][1]
inner_dict = next(iter(d.values()))[1]
And repeat the process for inner_dict
if you want to access its key / value.
First, I would like to create an empty dictionary of the form {: [ , {}]} . Can you show me an example ?
– Devilhorn
Nov 19 '18 at 22:56
@Devilhorn, Just instantiate it withd = {'': [0, {0: 0}]}
, though I'm not sure why this is helpful. Python isn't strongly typed, you don't need to define in advance the type of every key and value.
– jpp
Nov 20 '18 at 0:00
Thank's @jpp , I instantiated it outside the loop I'm using it with the first values I need and it worked perfectly. As for the empty dictionary I was curious to find a way for educational purposes..
– Devilhorn
Nov 20 '18 at 0:04
I have another problem, how can I get only the keys of the inside dictionary. For instance: say inside dictionary is :{name1:5,name2:2,name3:7} I need to check if name4 is in the inside dictionary as key , if not add it with empty value . So the dictionary in that case would be: inside dict is: {name1:5,name2:2,name3:7,name4:''} else if name4 is in dictionary it would remain the same.
– Devilhorn
Nov 20 '18 at 0:34
I found that if I reach inside dict with: d[term][1].keys() I get all the keys but I don't know how to continue
– Devilhorn
Nov 20 '18 at 0:40
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%2f53376777%2finverted-index-python%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
d = {"term": [5, {6: 7}]}
The key's value is a list:
d["term"]
[5, {6: 7}]
The list 's 1st element:
d["term"][0]
5
The list 's second element is a dictionary:
d["term"][1]
{6: 7}
The value of the dictionary's key '6' is 7:
d["term"][1][6]
7
Edit:
Some examples for modification:
d = {"term": [5, {6: 7}]}
d["term"].append(10)
print(d)
Out: {'term': [5, {6: 7}, 10]}
l=d["term"]
l[0]=55
print(d)
Out: {'term': [55, {6: 7}, 10]}
insidedict=l[1]
print(insidedict)
{6: 7}
insidedict[66]=77
print(d)
{'term': [55, {6: 7, 66: 77}, 10]}
If I have multiple keys ,say term , term1 , termk how can I add values in the inside dictionary of term or update term's numbers or update term's inside dictionary ?
– Devilhorn
Nov 19 '18 at 16:26
@Devilhorn I 've edited the code, see above.
– kantal
Nov 19 '18 at 17:24
add a comment |
d = {"term": [5, {6: 7}]}
The key's value is a list:
d["term"]
[5, {6: 7}]
The list 's 1st element:
d["term"][0]
5
The list 's second element is a dictionary:
d["term"][1]
{6: 7}
The value of the dictionary's key '6' is 7:
d["term"][1][6]
7
Edit:
Some examples for modification:
d = {"term": [5, {6: 7}]}
d["term"].append(10)
print(d)
Out: {'term': [5, {6: 7}, 10]}
l=d["term"]
l[0]=55
print(d)
Out: {'term': [55, {6: 7}, 10]}
insidedict=l[1]
print(insidedict)
{6: 7}
insidedict[66]=77
print(d)
{'term': [55, {6: 7, 66: 77}, 10]}
If I have multiple keys ,say term , term1 , termk how can I add values in the inside dictionary of term or update term's numbers or update term's inside dictionary ?
– Devilhorn
Nov 19 '18 at 16:26
@Devilhorn I 've edited the code, see above.
– kantal
Nov 19 '18 at 17:24
add a comment |
d = {"term": [5, {6: 7}]}
The key's value is a list:
d["term"]
[5, {6: 7}]
The list 's 1st element:
d["term"][0]
5
The list 's second element is a dictionary:
d["term"][1]
{6: 7}
The value of the dictionary's key '6' is 7:
d["term"][1][6]
7
Edit:
Some examples for modification:
d = {"term": [5, {6: 7}]}
d["term"].append(10)
print(d)
Out: {'term': [5, {6: 7}, 10]}
l=d["term"]
l[0]=55
print(d)
Out: {'term': [55, {6: 7}, 10]}
insidedict=l[1]
print(insidedict)
{6: 7}
insidedict[66]=77
print(d)
{'term': [55, {6: 7, 66: 77}, 10]}
d = {"term": [5, {6: 7}]}
The key's value is a list:
d["term"]
[5, {6: 7}]
The list 's 1st element:
d["term"][0]
5
The list 's second element is a dictionary:
d["term"][1]
{6: 7}
The value of the dictionary's key '6' is 7:
d["term"][1][6]
7
Edit:
Some examples for modification:
d = {"term": [5, {6: 7}]}
d["term"].append(10)
print(d)
Out: {'term': [5, {6: 7}, 10]}
l=d["term"]
l[0]=55
print(d)
Out: {'term': [55, {6: 7}, 10]}
insidedict=l[1]
print(insidedict)
{6: 7}
insidedict[66]=77
print(d)
{'term': [55, {6: 7, 66: 77}, 10]}
edited Nov 19 '18 at 17:23
answered Nov 19 '18 at 15:27


kantal
62227
62227
If I have multiple keys ,say term , term1 , termk how can I add values in the inside dictionary of term or update term's numbers or update term's inside dictionary ?
– Devilhorn
Nov 19 '18 at 16:26
@Devilhorn I 've edited the code, see above.
– kantal
Nov 19 '18 at 17:24
add a comment |
If I have multiple keys ,say term , term1 , termk how can I add values in the inside dictionary of term or update term's numbers or update term's inside dictionary ?
– Devilhorn
Nov 19 '18 at 16:26
@Devilhorn I 've edited the code, see above.
– kantal
Nov 19 '18 at 17:24
If I have multiple keys ,say term , term1 , termk how can I add values in the inside dictionary of term or update term's numbers or update term's inside dictionary ?
– Devilhorn
Nov 19 '18 at 16:26
If I have multiple keys ,say term , term1 , termk how can I add values in the inside dictionary of term or update term's numbers or update term's inside dictionary ?
– Devilhorn
Nov 19 '18 at 16:26
@Devilhorn I 've edited the code, see above.
– kantal
Nov 19 '18 at 17:24
@Devilhorn I 've edited the code, see above.
– kantal
Nov 19 '18 at 17:24
add a comment |
Sure, just define it as you have:
d = {'term': [5, {6: 7}]}
Since your dictionary has just one key, you an access the key via:
key = next(iter(d))
You can then access the value 5
via a couple of ways:
number = d[key][0]
number = next(iter(d.values()))[0]
Similarly, you can access the inner dictionary via either:
inner_dict = d[key][1]
inner_dict = next(iter(d.values()))[1]
And repeat the process for inner_dict
if you want to access its key / value.
First, I would like to create an empty dictionary of the form {: [ , {}]} . Can you show me an example ?
– Devilhorn
Nov 19 '18 at 22:56
@Devilhorn, Just instantiate it withd = {'': [0, {0: 0}]}
, though I'm not sure why this is helpful. Python isn't strongly typed, you don't need to define in advance the type of every key and value.
– jpp
Nov 20 '18 at 0:00
Thank's @jpp , I instantiated it outside the loop I'm using it with the first values I need and it worked perfectly. As for the empty dictionary I was curious to find a way for educational purposes..
– Devilhorn
Nov 20 '18 at 0:04
I have another problem, how can I get only the keys of the inside dictionary. For instance: say inside dictionary is :{name1:5,name2:2,name3:7} I need to check if name4 is in the inside dictionary as key , if not add it with empty value . So the dictionary in that case would be: inside dict is: {name1:5,name2:2,name3:7,name4:''} else if name4 is in dictionary it would remain the same.
– Devilhorn
Nov 20 '18 at 0:34
I found that if I reach inside dict with: d[term][1].keys() I get all the keys but I don't know how to continue
– Devilhorn
Nov 20 '18 at 0:40
add a comment |
Sure, just define it as you have:
d = {'term': [5, {6: 7}]}
Since your dictionary has just one key, you an access the key via:
key = next(iter(d))
You can then access the value 5
via a couple of ways:
number = d[key][0]
number = next(iter(d.values()))[0]
Similarly, you can access the inner dictionary via either:
inner_dict = d[key][1]
inner_dict = next(iter(d.values()))[1]
And repeat the process for inner_dict
if you want to access its key / value.
First, I would like to create an empty dictionary of the form {: [ , {}]} . Can you show me an example ?
– Devilhorn
Nov 19 '18 at 22:56
@Devilhorn, Just instantiate it withd = {'': [0, {0: 0}]}
, though I'm not sure why this is helpful. Python isn't strongly typed, you don't need to define in advance the type of every key and value.
– jpp
Nov 20 '18 at 0:00
Thank's @jpp , I instantiated it outside the loop I'm using it with the first values I need and it worked perfectly. As for the empty dictionary I was curious to find a way for educational purposes..
– Devilhorn
Nov 20 '18 at 0:04
I have another problem, how can I get only the keys of the inside dictionary. For instance: say inside dictionary is :{name1:5,name2:2,name3:7} I need to check if name4 is in the inside dictionary as key , if not add it with empty value . So the dictionary in that case would be: inside dict is: {name1:5,name2:2,name3:7,name4:''} else if name4 is in dictionary it would remain the same.
– Devilhorn
Nov 20 '18 at 0:34
I found that if I reach inside dict with: d[term][1].keys() I get all the keys but I don't know how to continue
– Devilhorn
Nov 20 '18 at 0:40
add a comment |
Sure, just define it as you have:
d = {'term': [5, {6: 7}]}
Since your dictionary has just one key, you an access the key via:
key = next(iter(d))
You can then access the value 5
via a couple of ways:
number = d[key][0]
number = next(iter(d.values()))[0]
Similarly, you can access the inner dictionary via either:
inner_dict = d[key][1]
inner_dict = next(iter(d.values()))[1]
And repeat the process for inner_dict
if you want to access its key / value.
Sure, just define it as you have:
d = {'term': [5, {6: 7}]}
Since your dictionary has just one key, you an access the key via:
key = next(iter(d))
You can then access the value 5
via a couple of ways:
number = d[key][0]
number = next(iter(d.values()))[0]
Similarly, you can access the inner dictionary via either:
inner_dict = d[key][1]
inner_dict = next(iter(d.values()))[1]
And repeat the process for inner_dict
if you want to access its key / value.
answered Nov 19 '18 at 14:34


jpp
92.1k2053103
92.1k2053103
First, I would like to create an empty dictionary of the form {: [ , {}]} . Can you show me an example ?
– Devilhorn
Nov 19 '18 at 22:56
@Devilhorn, Just instantiate it withd = {'': [0, {0: 0}]}
, though I'm not sure why this is helpful. Python isn't strongly typed, you don't need to define in advance the type of every key and value.
– jpp
Nov 20 '18 at 0:00
Thank's @jpp , I instantiated it outside the loop I'm using it with the first values I need and it worked perfectly. As for the empty dictionary I was curious to find a way for educational purposes..
– Devilhorn
Nov 20 '18 at 0:04
I have another problem, how can I get only the keys of the inside dictionary. For instance: say inside dictionary is :{name1:5,name2:2,name3:7} I need to check if name4 is in the inside dictionary as key , if not add it with empty value . So the dictionary in that case would be: inside dict is: {name1:5,name2:2,name3:7,name4:''} else if name4 is in dictionary it would remain the same.
– Devilhorn
Nov 20 '18 at 0:34
I found that if I reach inside dict with: d[term][1].keys() I get all the keys but I don't know how to continue
– Devilhorn
Nov 20 '18 at 0:40
add a comment |
First, I would like to create an empty dictionary of the form {: [ , {}]} . Can you show me an example ?
– Devilhorn
Nov 19 '18 at 22:56
@Devilhorn, Just instantiate it withd = {'': [0, {0: 0}]}
, though I'm not sure why this is helpful. Python isn't strongly typed, you don't need to define in advance the type of every key and value.
– jpp
Nov 20 '18 at 0:00
Thank's @jpp , I instantiated it outside the loop I'm using it with the first values I need and it worked perfectly. As for the empty dictionary I was curious to find a way for educational purposes..
– Devilhorn
Nov 20 '18 at 0:04
I have another problem, how can I get only the keys of the inside dictionary. For instance: say inside dictionary is :{name1:5,name2:2,name3:7} I need to check if name4 is in the inside dictionary as key , if not add it with empty value . So the dictionary in that case would be: inside dict is: {name1:5,name2:2,name3:7,name4:''} else if name4 is in dictionary it would remain the same.
– Devilhorn
Nov 20 '18 at 0:34
I found that if I reach inside dict with: d[term][1].keys() I get all the keys but I don't know how to continue
– Devilhorn
Nov 20 '18 at 0:40
First, I would like to create an empty dictionary of the form {: [ , {}]} . Can you show me an example ?
– Devilhorn
Nov 19 '18 at 22:56
First, I would like to create an empty dictionary of the form {: [ , {}]} . Can you show me an example ?
– Devilhorn
Nov 19 '18 at 22:56
@Devilhorn, Just instantiate it with
d = {'': [0, {0: 0}]}
, though I'm not sure why this is helpful. Python isn't strongly typed, you don't need to define in advance the type of every key and value.– jpp
Nov 20 '18 at 0:00
@Devilhorn, Just instantiate it with
d = {'': [0, {0: 0}]}
, though I'm not sure why this is helpful. Python isn't strongly typed, you don't need to define in advance the type of every key and value.– jpp
Nov 20 '18 at 0:00
Thank's @jpp , I instantiated it outside the loop I'm using it with the first values I need and it worked perfectly. As for the empty dictionary I was curious to find a way for educational purposes..
– Devilhorn
Nov 20 '18 at 0:04
Thank's @jpp , I instantiated it outside the loop I'm using it with the first values I need and it worked perfectly. As for the empty dictionary I was curious to find a way for educational purposes..
– Devilhorn
Nov 20 '18 at 0:04
I have another problem, how can I get only the keys of the inside dictionary. For instance: say inside dictionary is :{name1:5,name2:2,name3:7} I need to check if name4 is in the inside dictionary as key , if not add it with empty value . So the dictionary in that case would be: inside dict is: {name1:5,name2:2,name3:7,name4:''} else if name4 is in dictionary it would remain the same.
– Devilhorn
Nov 20 '18 at 0:34
I have another problem, how can I get only the keys of the inside dictionary. For instance: say inside dictionary is :{name1:5,name2:2,name3:7} I need to check if name4 is in the inside dictionary as key , if not add it with empty value . So the dictionary in that case would be: inside dict is: {name1:5,name2:2,name3:7,name4:''} else if name4 is in dictionary it would remain the same.
– Devilhorn
Nov 20 '18 at 0:34
I found that if I reach inside dict with: d[term][1].keys() I get all the keys but I don't know how to continue
– Devilhorn
Nov 20 '18 at 0:40
I found that if I reach inside dict with: d[term][1].keys() I get all the keys but I don't know how to continue
– Devilhorn
Nov 20 '18 at 0:40
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53376777%2finverted-index-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