Showing words in file that start with a specific letter [duplicate]
This question already has an answer here:
Iterator Object for Removing Duplicates in Python
2 answers
My code is able to show each word that starts with a specific letter from a text file, but I want it to not show duplicate words. Here is my code:
with open('text.txt','r') as myFile:
data=myFile.read().lower()
for s in data.split():
if s.startswith("r"):
print(s)
Like I said, my code does print the words but it shows duplicates. Thank you for helping
python python-3.x file-io
marked as duplicate by MisterMiyagi, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 23:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Iterator Object for Removing Duplicates in Python
2 answers
My code is able to show each word that starts with a specific letter from a text file, but I want it to not show duplicate words. Here is my code:
with open('text.txt','r') as myFile:
data=myFile.read().lower()
for s in data.split():
if s.startswith("r"):
print(s)
Like I said, my code does print the words but it shows duplicates. Thank you for helping
python python-3.x file-io
marked as duplicate by MisterMiyagi, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 23:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Iterator Object for Removing Duplicates in Python
2 answers
My code is able to show each word that starts with a specific letter from a text file, but I want it to not show duplicate words. Here is my code:
with open('text.txt','r') as myFile:
data=myFile.read().lower()
for s in data.split():
if s.startswith("r"):
print(s)
Like I said, my code does print the words but it shows duplicates. Thank you for helping
python python-3.x file-io
This question already has an answer here:
Iterator Object for Removing Duplicates in Python
2 answers
My code is able to show each word that starts with a specific letter from a text file, but I want it to not show duplicate words. Here is my code:
with open('text.txt','r') as myFile:
data=myFile.read().lower()
for s in data.split():
if s.startswith("r"):
print(s)
Like I said, my code does print the words but it shows duplicates. Thank you for helping
This question already has an answer here:
Iterator Object for Removing Duplicates in Python
2 answers
python python-3.x file-io
python python-3.x file-io
asked Nov 13 '18 at 20:08
gbenyugbenyu
224
224
marked as duplicate by MisterMiyagi, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 23:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by MisterMiyagi, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 23:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Use a set:
with open('text.txt', 'r') as myFile:
data = myFile.read().lower()
seen = set()
for s in data.split():
if s not in seen and s.startswith("r"):
seen.add(s)
print(s)
add a comment |
This is an optimized version the will read the file line by line without loading it all into memory:
seen_words = set()
with open('text.txt', 'r') as my_file:
for line in my_file:
for word in line.lower().split():
if word not in seen_words:
print(word)
seen_words.add(word)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use a set:
with open('text.txt', 'r') as myFile:
data = myFile.read().lower()
seen = set()
for s in data.split():
if s not in seen and s.startswith("r"):
seen.add(s)
print(s)
add a comment |
Use a set:
with open('text.txt', 'r') as myFile:
data = myFile.read().lower()
seen = set()
for s in data.split():
if s not in seen and s.startswith("r"):
seen.add(s)
print(s)
add a comment |
Use a set:
with open('text.txt', 'r') as myFile:
data = myFile.read().lower()
seen = set()
for s in data.split():
if s not in seen and s.startswith("r"):
seen.add(s)
print(s)
Use a set:
with open('text.txt', 'r') as myFile:
data = myFile.read().lower()
seen = set()
for s in data.split():
if s not in seen and s.startswith("r"):
seen.add(s)
print(s)
answered Nov 13 '18 at 20:09
Daniel MesejoDaniel Mesejo
16.3k21330
16.3k21330
add a comment |
add a comment |
This is an optimized version the will read the file line by line without loading it all into memory:
seen_words = set()
with open('text.txt', 'r') as my_file:
for line in my_file:
for word in line.lower().split():
if word not in seen_words:
print(word)
seen_words.add(word)
add a comment |
This is an optimized version the will read the file line by line without loading it all into memory:
seen_words = set()
with open('text.txt', 'r') as my_file:
for line in my_file:
for word in line.lower().split():
if word not in seen_words:
print(word)
seen_words.add(word)
add a comment |
This is an optimized version the will read the file line by line without loading it all into memory:
seen_words = set()
with open('text.txt', 'r') as my_file:
for line in my_file:
for word in line.lower().split():
if word not in seen_words:
print(word)
seen_words.add(word)
This is an optimized version the will read the file line by line without loading it all into memory:
seen_words = set()
with open('text.txt', 'r') as my_file:
for line in my_file:
for word in line.lower().split():
if word not in seen_words:
print(word)
seen_words.add(word)
answered Nov 13 '18 at 20:13
DalvenjiaDalvenjia
968612
968612
add a comment |
add a comment |