Python for loop not iterating - python

I'm trying to loop through a list of strings and add them to a dictionary if their length equals a length input by the user. When the last loop runs, it only runs one time. I know this because the first word in the dictionary is 8 characters long, and when the user input is 8, it prints just that word, and not the other 8 character words. If the input is 3, an empty dictionary is printed. Why is my loop not iterating through all of the words in the list linelist?
wordLength = raw_input("Enter a word length ")
word_dict = {}
infile = open("dictionary.txt")
for line in infile:
line = line.strip()
linelist = line.split(" ")
for word in linelist:
if len(word) == int(wordLength):
if len(word) in word_dict:
word_dict[len(word)] = word_dict[len(word)].append(word)
else:
word_dict[len(word)] = word
print word_dict

Each time your first loop runs, it sets linelist to a new value, overwriting any old value. After that first loop runs, linelist will contain only the split result from the last line of the file. Every time you process one line of the file, you are throwing away whatever you did with the previous line.
If you want to build a list of all words in the dictionary file, you need to make a list and append to it on each iteration of your for line in infile loop.
Also, it doesn't make much sense to use split on each line if each line is just one word, since there will be no splitting to be done.

for line in infile:
line = line.strip()
linelist = line.split(" ")
Every time you do linelist = line.split(" "), that replaces the old linelist with words from just the last line. The list ends up only holding words from the last line. If you want words from the entire file, create a single linelist and extend it with new words:
linelist = []
for line in infile:
# split with no argument splits on any run of whitespace, trimming
# leading and trailing whitespace
linelist += line.split()
# ^ this means add the elements of line.split() to linelist
Since apparently every word is on its own line, though, you shouldn't even be using split:
words = [line.strip() for line in infile]

Your second loop is not indented, so you run it only on the last value of linelist.

Related

read words from file, line by line and concatenate to paragraph

I have a really long list of words that are on each line. How do I make a program that takes in all that and print them all side by side?
I tried making the word an element of a list, but I don't know how to proceed.
Here's the code I've tried so far:
def convert(lst):
return([i for item in lst for i in item.split()])
lst = [''' -The list of words come here- ''']
print(convert(lst))
If you already have the words in a list, you can use the join() function to concatenate them. See https://docs.python.org/3/library/stdtypes.html#str.join
words = open('your_file.txt').readlines()
separator = ' '
print(separator.join(words))
Another, a little bit more cumbersome method would be to print the words using the builtin print() function but suppress the newline that print() normally adds automatically to the end of your argument.
words = open('your_file.txt').readlines()
for word in words:
print(word, end=' ')
Try this, and example.txt just has a list of words going down line by line.
with open("example.txt", "r") as a_file:
sentence = ""
for line in a_file:
stripped_line = line.strip()
sentence = sentence + f"{stripped_line} "
print(sentence)
If your input file is really large and you cant fit it all in memory, you can read the words lazy and write them to disk instead of holding the whole output in memory.
# create a generator that yields each individual line
lines = (l for l in open('words'))
with open("output", "w+") as writer:
# read the file line by line to avoid memory issues
while True:
try:
line = next(lines)
# add to the paragraph in the out file
writer.write(line.replace('\n', ' '))
except StopIteration:
break
You can check the working example here: https://replit.com/#bluebrown/readwritewords#main.py

does for keyword understand that a string is an iteration?

Question :
" Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in alphabetical order."
Code:
fname = input("Enter file name: ")
fh = open(fname)
hh = list()
for sen in fh:
sen=sen.split()
for element in sen:
if element not in hh:
hh.append(element)
hh.sort()
print(hh)
I want to make sure that I understood the code. So first we took the file name then opened it then we created an empty list then we split the strings into a list and then we checked if the elements in sen is in the empty list we created and then we appended it and printed.
Also, I have a question when using the for keyword, does the for keyword understand that each word in the file is an iteration even before splitting it??
Python str.split() documentation: https://docs.python.org/3/library/stdtypes.html#str.split
fname = input("Enter file name: ") #-- user enters name of a file
fh = open(fname) #-------------------- open the file
hh = list() #------------------------- create an empty list
for sen in fh: #---------------------- loop through lines in the file
sen=sen.split() #----------------- split the line into words
for element in sen: #------------- loop through words in the line
if element not in hh: #------- if word is not in the list of unique words
hh.append(element) #------ add the word to the list
hh.sort() #--------------- organize the list
print(hh) #--------------------------- print the list of unique words
hh will be a list of all the unique words in the file.
The best way to work with files in Python is to use Context Managers.
Python Context Manager documentation: https://docs.python.org/3/library/contextlib.html
You should probably use:
filename = input("Enter file name: ")
unique_words = list()
with open(filename, "r") as file: # 'with' Context Manager
for line in file:
line = line.split()
for word in line:
if word not in unique_words:
unique_words.append(word)
unique_words.sort()
print(unique_words)

How do I compare 2 lines in a string in Python

I have the console output stored in a string in Python.
It looks like:
output ="Status of xyz
Process is running
Status of abc
Process is stopped"
I want to get last word of each line and compare with last word of next line.
How can I do this in Python?.
First you need to separate the string into a list of lines:
lines = output.split('\n') #splits into lines
Then you need to loop over the lines and split the line into words
#we go through all lines except the last, to check the line with the next
for lineIndex in range(len(lines)-1):
# split line to words
WordsLine1 = lines[lineIndex].split()
WordsLine2 = lines[lineIndex+1].split() # split next line to words
#now check if the last word of the line is equal to the last word of the other line.
if ( WordsLine1[-1] == WordLine2[-1]):
#equal do stuff..
Here's the data
data = """\
Status of xyz Process is running
Status of abc Process is stopped
"""
Split into lines in a cross-platform manner:
lines = data.splitlines()
Loop over the lines pairwise, so you have the current line and the previous line at the same time (using zip):
for previous, current in zip(lines, lines[1:]):
lastword = previous.split()[-1]
if lastword == current.split()[-1]:
print('Both lines end with the same word: {word}'.format(word=lastword))
Alternatively, if you don't like how zip looks, we can loop over the lines pairwise by repeatedly setting a variable to store the last line:
last = None
for line in lines:
if last is not None and line.split()[-1] == last.split()[-1]:
print('both lines have the same last word')
last = line

Splitting a line, then searching for string

sry im still new to python.
My complete code so far:
for line in file:
line = line.split("\t")
if my_var in line[1]:
print line[13]
What the program should do, is reading lines from a file.
the lines have the following Format:
"word" \t "word" \t "word" ...
The Programm should split each line into a list of strings containing the words
==> list = (word1, word2, word3, ...)
then i wish to test if the word at index 1 matches a given word, and if so i wish to print the word at index 13 (each line has the same ammount of elements)
What i dont understand is, writing:
for line in file:
line = line.split("\t")
word = line[1]
print word
works, while
for line in file:
line = line.split("\t")
word = line[1]
if my_var in word:
print line[13]
does not work.
Im pretty shure there is an easy solution to this Problem and that i simply cant find it.
Your error is because of the following line :
print line[16]
Your splited list hasn't 16 item it is just contain 4 item and you have tried to get the 16th index.

read line from file but store as list (python)

i want to read a specific line in a textfile and store the elements in a list.
My textfile looks like this
'item1' 'item2' 'item3'
I always end up with a list with every letter as an element
what I tried
line = file.readline()
for u in line:
#do something
line = file.readline()
for u in line.split():
# do stuff
This assumes the items are split by whitespace.
split the line by spaces and then add them to the list:
# line = ('item1' 'item2' 'item3') example of line
listed = []
line = file.readline()
for u in line.split(' '):
listed.append(u)
for e in listed:
print(e)
What you have there will read one whole line in, and then loop through each character that was in that line. What you probably want to do is split that line into your 3 items. Provided they are separated by a space, you could do this:
line = file.readline() # Read the line in as before
singles = line.split(' ') # Split the line wherever there are spaces found. You can choose any character though
for item in singles: # Loop through all items, in your example there will be 3
#Do something
You can reduce the number of lines (and variables) here by stringing the various functions used together, but I left them separate for ease of understanding.
You can try:
for u in line.split():
Which assumes there are whitespaces between each item. Otherwise you'll simply iterate over a str and thus iterate character by character.
You might also want to do:
u = u.strip('\'')
to get rid of the '
I'd use with, re and basically take anything between apostrophes... (this'll work for strings that have spaces inside them (eg: item 1 item 2, but obviously nested or string escape sequences won't be caught).
import re
with open('somefile') as fin:
print re.findall("'(.*?)'", next(fin))
# ['item1', 'item2', 'item3']
If you want all the characters of the line in a list you could try this.
This use double list comprehension.
with open('stackoverflow.txt', 'r') as file:
charlist = [c for word in file.readline().split(' ') for c in word ]
print(charlist)
If you want to get rid off some char, you can apply some filter for example; I don't want the char = ' in my list.
with open('stackoverflow.txt', 'r') as file:
charlist = [c for word in file.readline().split(' ') for c in word if(c != "'")]
print(charlist)
If this double list comprehension looks strange is the same of this.
with open('stackoverflow.txt', 'r') as file:
charlist = []
line = file.readline()
for word in line.split(' '):
for c in word:
if(c != "'"):
charlist.append(c)
print(charlist)

Categories