Adding words from a file to a dictionary
I want to add each word from a text file to a dictionary, how do I do this?
(I have a file 'words.txt', I have opened and read the file and the list of words is in the variable "lines" below)
d = {}
for i in lines:
for word in i.split():
d[???] = word
What code do I put where the '???' is?
I basically want the dictionary to look like this:
{0: firstword, 1: secondword, 2: thirdword, 3: fourthword...}
I figured that getting the index position of each word in the list could work but I'm not exactly sure how to do this.
It doesn't seem too complicated to do but I'm stuck.
python dictionary
add a comment |
I want to add each word from a text file to a dictionary, how do I do this?
(I have a file 'words.txt', I have opened and read the file and the list of words is in the variable "lines" below)
d = {}
for i in lines:
for word in i.split():
d[???] = word
What code do I put where the '???' is?
I basically want the dictionary to look like this:
{0: firstword, 1: secondword, 2: thirdword, 3: fourthword...}
I figured that getting the index position of each word in the list could work but I'm not exactly sure how to do this.
It doesn't seem too complicated to do but I'm stuck.
python dictionary
If you just want have separate index for each element python2.7:d[len(d)] = word, python3:d[str(len(d))] = word
– Filip Młynarski
Nov 24 '18 at 15:55
6
Why do you need a dictionary for that? Why not a plain list containing all words from the file?
– Some programmer dude
Nov 24 '18 at 15:57
@FilipMłynarski Thanks
– Mandingo
Nov 24 '18 at 19:32
@Someprogrammerdude It's for a larger program that I need to write for my coursework. It's what they wanted as part of their instructions. Most of the tasks that were set specified a specific way of doing it - whether convenient or efficient or not.
– Mandingo
Nov 24 '18 at 19:33
add a comment |
I want to add each word from a text file to a dictionary, how do I do this?
(I have a file 'words.txt', I have opened and read the file and the list of words is in the variable "lines" below)
d = {}
for i in lines:
for word in i.split():
d[???] = word
What code do I put where the '???' is?
I basically want the dictionary to look like this:
{0: firstword, 1: secondword, 2: thirdword, 3: fourthword...}
I figured that getting the index position of each word in the list could work but I'm not exactly sure how to do this.
It doesn't seem too complicated to do but I'm stuck.
python dictionary
I want to add each word from a text file to a dictionary, how do I do this?
(I have a file 'words.txt', I have opened and read the file and the list of words is in the variable "lines" below)
d = {}
for i in lines:
for word in i.split():
d[???] = word
What code do I put where the '???' is?
I basically want the dictionary to look like this:
{0: firstword, 1: secondword, 2: thirdword, 3: fourthword...}
I figured that getting the index position of each word in the list could work but I'm not exactly sure how to do this.
It doesn't seem too complicated to do but I'm stuck.
python dictionary
python dictionary
asked Nov 24 '18 at 15:53
MandingoMandingo
34229
34229
If you just want have separate index for each element python2.7:d[len(d)] = word, python3:d[str(len(d))] = word
– Filip Młynarski
Nov 24 '18 at 15:55
6
Why do you need a dictionary for that? Why not a plain list containing all words from the file?
– Some programmer dude
Nov 24 '18 at 15:57
@FilipMłynarski Thanks
– Mandingo
Nov 24 '18 at 19:32
@Someprogrammerdude It's for a larger program that I need to write for my coursework. It's what they wanted as part of their instructions. Most of the tasks that were set specified a specific way of doing it - whether convenient or efficient or not.
– Mandingo
Nov 24 '18 at 19:33
add a comment |
If you just want have separate index for each element python2.7:d[len(d)] = word, python3:d[str(len(d))] = word
– Filip Młynarski
Nov 24 '18 at 15:55
6
Why do you need a dictionary for that? Why not a plain list containing all words from the file?
– Some programmer dude
Nov 24 '18 at 15:57
@FilipMłynarski Thanks
– Mandingo
Nov 24 '18 at 19:32
@Someprogrammerdude It's for a larger program that I need to write for my coursework. It's what they wanted as part of their instructions. Most of the tasks that were set specified a specific way of doing it - whether convenient or efficient or not.
– Mandingo
Nov 24 '18 at 19:33
If you just want have separate index for each element python2.7:
d[len(d)] = word, python3: d[str(len(d))] = word– Filip Młynarski
Nov 24 '18 at 15:55
If you just want have separate index for each element python2.7:
d[len(d)] = word, python3: d[str(len(d))] = word– Filip Młynarski
Nov 24 '18 at 15:55
6
6
Why do you need a dictionary for that? Why not a plain list containing all words from the file?
– Some programmer dude
Nov 24 '18 at 15:57
Why do you need a dictionary for that? Why not a plain list containing all words from the file?
– Some programmer dude
Nov 24 '18 at 15:57
@FilipMłynarski Thanks
– Mandingo
Nov 24 '18 at 19:32
@FilipMłynarski Thanks
– Mandingo
Nov 24 '18 at 19:32
@Someprogrammerdude It's for a larger program that I need to write for my coursework. It's what they wanted as part of their instructions. Most of the tasks that were set specified a specific way of doing it - whether convenient or efficient or not.
– Mandingo
Nov 24 '18 at 19:33
@Someprogrammerdude It's for a larger program that I need to write for my coursework. It's what they wanted as part of their instructions. Most of the tasks that were set specified a specific way of doing it - whether convenient or efficient or not.
– Mandingo
Nov 24 '18 at 19:33
add a comment |
5 Answers
5
active
oldest
votes
First open a file and write some lines.
fname = 'textfile.txt'
with open(fname, 'w') as textfile:
textfile.write('zero one two three four fiven')
textfile.write('six seven eight nine ten')
Enumerate through the words in whichever fashion you desire. If you use a generator expression it works nicely with a dict comprehension.
word_positions = {}
with open(fname, 'r') as textfile:
words = (word for line in textfile.readlines() for word in line.split())
word_positions = {i: word for i, word in enumerate(words)}
This yields,
word_positions
{0: 'zero',
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight',
9: 'nine',
10: 'ten'}
if you add a middle line with duplicates like "textfile.write('zero one two three four fiven')", your output contains the duplicates.
– user1269942
Nov 24 '18 at 16:41
The question doesn't seem concerned with duplicates though. It's very easy to avoid duplicates by using a set if need be.
– Austin Mackillop
Nov 24 '18 at 17:05
add a comment |
say you have a variable words having list of words ['firstword', 'secondword', 'thirdword', 'fourthword']
so your code would be like:
d = {}
for k, v in enumerate(words):
d[k] = v
add a comment |
You can keep track of the "current index" in a separate variable c and use that as the value for the word in your dictionary:
d = {}
c = 0
for i in lines:
for word in i.split():
d[word] = c
c += 1
Note that here the dictionary will store the highest index of the duplicated word.
add a comment |
Each line overwrites the line before it in your dictionary. But you can work around that like:
d = {}
k = 0
for i in lines:
for word in i.split():
d[str(k)] = word
k = k + 1
Why are you using dictionary for this? Dictionaries are useful when they are used with keys with meanings. You could've just used a list for this task.
Also, you can increase the performance by preallocating your list and then fill it with your algorithm.
add a comment |
There are many answers questioning why you need to do this which is valid, however I'll try and answer the direct question. Also, I think dealing with duplicates is necessary. The lower index(first time word is seen) takes precedence...which is an assumption on my part, but it makes sense considering your question.
#first populate a word:index dictionary
#ensure duplicates don't overwrite...for this use "in" which is fast
d1 = {}
ix = 0
for i in lines:
for word in i.split():
if word not in d1:
#only add word to the dict if it is NOT already in (addressing duplicates)
d1[word] = ix
ix += 1
#now "reverse" the dict
d = {} #new dict
for word in d1:
d[d1[word]] = word
now you have a dict word:index with unique words+index
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%2f53459848%2fadding-words-from-a-file-to-a-dictionary%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
First open a file and write some lines.
fname = 'textfile.txt'
with open(fname, 'w') as textfile:
textfile.write('zero one two three four fiven')
textfile.write('six seven eight nine ten')
Enumerate through the words in whichever fashion you desire. If you use a generator expression it works nicely with a dict comprehension.
word_positions = {}
with open(fname, 'r') as textfile:
words = (word for line in textfile.readlines() for word in line.split())
word_positions = {i: word for i, word in enumerate(words)}
This yields,
word_positions
{0: 'zero',
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight',
9: 'nine',
10: 'ten'}
if you add a middle line with duplicates like "textfile.write('zero one two three four fiven')", your output contains the duplicates.
– user1269942
Nov 24 '18 at 16:41
The question doesn't seem concerned with duplicates though. It's very easy to avoid duplicates by using a set if need be.
– Austin Mackillop
Nov 24 '18 at 17:05
add a comment |
First open a file and write some lines.
fname = 'textfile.txt'
with open(fname, 'w') as textfile:
textfile.write('zero one two three four fiven')
textfile.write('six seven eight nine ten')
Enumerate through the words in whichever fashion you desire. If you use a generator expression it works nicely with a dict comprehension.
word_positions = {}
with open(fname, 'r') as textfile:
words = (word for line in textfile.readlines() for word in line.split())
word_positions = {i: word for i, word in enumerate(words)}
This yields,
word_positions
{0: 'zero',
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight',
9: 'nine',
10: 'ten'}
if you add a middle line with duplicates like "textfile.write('zero one two three four fiven')", your output contains the duplicates.
– user1269942
Nov 24 '18 at 16:41
The question doesn't seem concerned with duplicates though. It's very easy to avoid duplicates by using a set if need be.
– Austin Mackillop
Nov 24 '18 at 17:05
add a comment |
First open a file and write some lines.
fname = 'textfile.txt'
with open(fname, 'w') as textfile:
textfile.write('zero one two three four fiven')
textfile.write('six seven eight nine ten')
Enumerate through the words in whichever fashion you desire. If you use a generator expression it works nicely with a dict comprehension.
word_positions = {}
with open(fname, 'r') as textfile:
words = (word for line in textfile.readlines() for word in line.split())
word_positions = {i: word for i, word in enumerate(words)}
This yields,
word_positions
{0: 'zero',
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight',
9: 'nine',
10: 'ten'}
First open a file and write some lines.
fname = 'textfile.txt'
with open(fname, 'w') as textfile:
textfile.write('zero one two three four fiven')
textfile.write('six seven eight nine ten')
Enumerate through the words in whichever fashion you desire. If you use a generator expression it works nicely with a dict comprehension.
word_positions = {}
with open(fname, 'r') as textfile:
words = (word for line in textfile.readlines() for word in line.split())
word_positions = {i: word for i, word in enumerate(words)}
This yields,
word_positions
{0: 'zero',
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight',
9: 'nine',
10: 'ten'}
answered Nov 24 '18 at 16:27
Austin MackillopAustin Mackillop
35517
35517
if you add a middle line with duplicates like "textfile.write('zero one two three four fiven')", your output contains the duplicates.
– user1269942
Nov 24 '18 at 16:41
The question doesn't seem concerned with duplicates though. It's very easy to avoid duplicates by using a set if need be.
– Austin Mackillop
Nov 24 '18 at 17:05
add a comment |
if you add a middle line with duplicates like "textfile.write('zero one two three four fiven')", your output contains the duplicates.
– user1269942
Nov 24 '18 at 16:41
The question doesn't seem concerned with duplicates though. It's very easy to avoid duplicates by using a set if need be.
– Austin Mackillop
Nov 24 '18 at 17:05
if you add a middle line with duplicates like "textfile.write('zero one two three four fiven')", your output contains the duplicates.
– user1269942
Nov 24 '18 at 16:41
if you add a middle line with duplicates like "textfile.write('zero one two three four fiven')", your output contains the duplicates.
– user1269942
Nov 24 '18 at 16:41
The question doesn't seem concerned with duplicates though. It's very easy to avoid duplicates by using a set if need be.
– Austin Mackillop
Nov 24 '18 at 17:05
The question doesn't seem concerned with duplicates though. It's very easy to avoid duplicates by using a set if need be.
– Austin Mackillop
Nov 24 '18 at 17:05
add a comment |
say you have a variable words having list of words ['firstword', 'secondword', 'thirdword', 'fourthword']
so your code would be like:
d = {}
for k, v in enumerate(words):
d[k] = v
add a comment |
say you have a variable words having list of words ['firstword', 'secondword', 'thirdword', 'fourthword']
so your code would be like:
d = {}
for k, v in enumerate(words):
d[k] = v
add a comment |
say you have a variable words having list of words ['firstword', 'secondword', 'thirdword', 'fourthword']
so your code would be like:
d = {}
for k, v in enumerate(words):
d[k] = v
say you have a variable words having list of words ['firstword', 'secondword', 'thirdword', 'fourthword']
so your code would be like:
d = {}
for k, v in enumerate(words):
d[k] = v
answered Nov 24 '18 at 15:58
GahanGahan
2,79531232
2,79531232
add a comment |
add a comment |
You can keep track of the "current index" in a separate variable c and use that as the value for the word in your dictionary:
d = {}
c = 0
for i in lines:
for word in i.split():
d[word] = c
c += 1
Note that here the dictionary will store the highest index of the duplicated word.
add a comment |
You can keep track of the "current index" in a separate variable c and use that as the value for the word in your dictionary:
d = {}
c = 0
for i in lines:
for word in i.split():
d[word] = c
c += 1
Note that here the dictionary will store the highest index of the duplicated word.
add a comment |
You can keep track of the "current index" in a separate variable c and use that as the value for the word in your dictionary:
d = {}
c = 0
for i in lines:
for word in i.split():
d[word] = c
c += 1
Note that here the dictionary will store the highest index of the duplicated word.
You can keep track of the "current index" in a separate variable c and use that as the value for the word in your dictionary:
d = {}
c = 0
for i in lines:
for word in i.split():
d[word] = c
c += 1
Note that here the dictionary will store the highest index of the duplicated word.
answered Nov 24 '18 at 15:59
sliderslider
8,21811129
8,21811129
add a comment |
add a comment |
Each line overwrites the line before it in your dictionary. But you can work around that like:
d = {}
k = 0
for i in lines:
for word in i.split():
d[str(k)] = word
k = k + 1
Why are you using dictionary for this? Dictionaries are useful when they are used with keys with meanings. You could've just used a list for this task.
Also, you can increase the performance by preallocating your list and then fill it with your algorithm.
add a comment |
Each line overwrites the line before it in your dictionary. But you can work around that like:
d = {}
k = 0
for i in lines:
for word in i.split():
d[str(k)] = word
k = k + 1
Why are you using dictionary for this? Dictionaries are useful when they are used with keys with meanings. You could've just used a list for this task.
Also, you can increase the performance by preallocating your list and then fill it with your algorithm.
add a comment |
Each line overwrites the line before it in your dictionary. But you can work around that like:
d = {}
k = 0
for i in lines:
for word in i.split():
d[str(k)] = word
k = k + 1
Why are you using dictionary for this? Dictionaries are useful when they are used with keys with meanings. You could've just used a list for this task.
Also, you can increase the performance by preallocating your list and then fill it with your algorithm.
Each line overwrites the line before it in your dictionary. But you can work around that like:
d = {}
k = 0
for i in lines:
for word in i.split():
d[str(k)] = word
k = k + 1
Why are you using dictionary for this? Dictionaries are useful when they are used with keys with meanings. You could've just used a list for this task.
Also, you can increase the performance by preallocating your list and then fill it with your algorithm.
answered Nov 24 '18 at 16:03
Talip Tolga SarıTalip Tolga Sarı
836
836
add a comment |
add a comment |
There are many answers questioning why you need to do this which is valid, however I'll try and answer the direct question. Also, I think dealing with duplicates is necessary. The lower index(first time word is seen) takes precedence...which is an assumption on my part, but it makes sense considering your question.
#first populate a word:index dictionary
#ensure duplicates don't overwrite...for this use "in" which is fast
d1 = {}
ix = 0
for i in lines:
for word in i.split():
if word not in d1:
#only add word to the dict if it is NOT already in (addressing duplicates)
d1[word] = ix
ix += 1
#now "reverse" the dict
d = {} #new dict
for word in d1:
d[d1[word]] = word
now you have a dict word:index with unique words+index
add a comment |
There are many answers questioning why you need to do this which is valid, however I'll try and answer the direct question. Also, I think dealing with duplicates is necessary. The lower index(first time word is seen) takes precedence...which is an assumption on my part, but it makes sense considering your question.
#first populate a word:index dictionary
#ensure duplicates don't overwrite...for this use "in" which is fast
d1 = {}
ix = 0
for i in lines:
for word in i.split():
if word not in d1:
#only add word to the dict if it is NOT already in (addressing duplicates)
d1[word] = ix
ix += 1
#now "reverse" the dict
d = {} #new dict
for word in d1:
d[d1[word]] = word
now you have a dict word:index with unique words+index
add a comment |
There are many answers questioning why you need to do this which is valid, however I'll try and answer the direct question. Also, I think dealing with duplicates is necessary. The lower index(first time word is seen) takes precedence...which is an assumption on my part, but it makes sense considering your question.
#first populate a word:index dictionary
#ensure duplicates don't overwrite...for this use "in" which is fast
d1 = {}
ix = 0
for i in lines:
for word in i.split():
if word not in d1:
#only add word to the dict if it is NOT already in (addressing duplicates)
d1[word] = ix
ix += 1
#now "reverse" the dict
d = {} #new dict
for word in d1:
d[d1[word]] = word
now you have a dict word:index with unique words+index
There are many answers questioning why you need to do this which is valid, however I'll try and answer the direct question. Also, I think dealing with duplicates is necessary. The lower index(first time word is seen) takes precedence...which is an assumption on my part, but it makes sense considering your question.
#first populate a word:index dictionary
#ensure duplicates don't overwrite...for this use "in" which is fast
d1 = {}
ix = 0
for i in lines:
for word in i.split():
if word not in d1:
#only add word to the dict if it is NOT already in (addressing duplicates)
d1[word] = ix
ix += 1
#now "reverse" the dict
d = {} #new dict
for word in d1:
d[d1[word]] = word
now you have a dict word:index with unique words+index
edited Nov 24 '18 at 16:40
answered Nov 24 '18 at 16:25
user1269942user1269942
2,1301420
2,1301420
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%2f53459848%2fadding-words-from-a-file-to-a-dictionary%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
If you just want have separate index for each element python2.7:
d[len(d)] = word, python3:d[str(len(d))] = word– Filip Młynarski
Nov 24 '18 at 15:55
6
Why do you need a dictionary for that? Why not a plain list containing all words from the file?
– Some programmer dude
Nov 24 '18 at 15:57
@FilipMłynarski Thanks
– Mandingo
Nov 24 '18 at 19:32
@Someprogrammerdude It's for a larger program that I need to write for my coursework. It's what they wanted as part of their instructions. Most of the tasks that were set specified a specific way of doing it - whether convenient or efficient or not.
– Mandingo
Nov 24 '18 at 19:33