if -string- in set doesn't work [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
i'm trying to check if the input is in a set. I get the set from a words.dat file. the set is correct, but the else statement always gets executed. This is my code:
import fileinput
def words2array(words):
woorden = set([])
for line in fileinput.input(words):
woorden.add(line)
return woorden
def krijgInput():
input = raw_input().upper()
input += "/n"
return input
woordSet = words2array("words.dat")
input = krijgInput()
if 'input' in woordSet:
print "Ja"
else:
print "Nee"
print input
print woordSet
This is a part of the set, I can't post the full set because it has around 80k-120k words.
'SPOUT\n', 'BLASTOMA\n', 'HINDGUT\n', 'FORGOER\n', 'LEOPARDS\n', 'SPECULUM\n', 'KEROSENES\n', 'CARIED\n', 'SOUBISES\n', 'ANIMALS\n', 'DISSERTS\n', 'OMISSIVE\n', 'VIREMIC\n', 'FLUSTERING\n', 'PLENUMS\n', 'VACUITY\n',
this is a little bit from words.dat:
AARDVARK
AARDVARKS
AARDWOLF
AARDWOLVES
AAS
AASVOGEL
AASVOGELS
ABA
ABACA
ABACAS
ABACI
ABACK
ABACUS
ABACUSES
ABAFT

You are checking for the literal string 'input', not the variable input, which you managed to use properly several times.
Also, don't use input as a variable name, as it masks the built-in.
And compare /n to 'BLASTOMA\n' - / is different from \. Use \n.
And here's an easier way to do this:
with open('words.dat') as f:
if raw_input().upper()+'\n' in set(f):
print 'Ja'
else:
print 'Nee'
Note that that depends on having an "empty" line at the end of the file. If the last line is just the last word without a newline, use if raw_input().upper() in set(map(str.strip, f)) instead.

You need to strip the "\n"s: from the lines that you retrieve from the file:
for line in fileinput.input(words):
woorden.add(line.strip('\n')) # strip
& change
if 'input' in woordSet: # check if string in set, wrong
to
if input in woordSet: # check if input in set
& as #grc mentioned in his comment below, in order for this to work, you need to drop this line
input += "/n"
so as to get the neat set of elements that you want.

you are trying to search "input" as string in word set.
Please change below lines as:
input = krijgInput()
if input in woordSet:
Here you will search user input from set.

Related

Python Username Generator [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
The following is my code:
string1 = (input("What is your name?")) #creates a string, stores the name in it
first1 = string1[:1]
string2 = (input("What is your last name?"))
first3 = string2[:3]
from random import randint
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
print (sentence)
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
print (sentence)
It works, but I am having some trouble. I need to loop this 5 times - but it doesn't work for some reason!
P.S. Python 3!
You are creating a tuple called sentence, rather than a string
If you change that line to this:
sentence = "".join((str(randint(0,9)), first1.lower(), first3.upper()))
It will create a string that has no gaps, like so when printed:
What is your name?First
What is your last name?Last
5fLAS
You are creating a list, not a string so it seems logical that you get issues when trying to print it...
To append to a string you can do that :
sentence = str(randint(0,9))+(first1.lower())+(first3.upper())
In Python, you don't give a type to your variables, it depends of what you put INTO them.
In Python, elements between parenthesis "()" are considered as TUPLE (a type similar to a list, but that cant be modified), "[]" stands for a list, "{}" stands for a dictionnary. So you must NOT put parts of a string between parenthesis and separated with commas or you will turn them into a list of words. Instead you can use "+" that means to Python that you are making a string concatenation. Also note that i casted your random number into string. If you give 3 strings separated by "+" Python will automatically detect sentence as a String. Wp you've created a string !
If you have a list and want to get rid of the spaces you can also use :
"".join(yourlist)
However i don't recommend it here, you would be creating a list and using a join for nothing since you can create a string from the begining.
Edit: As for the loop, we can't answer if you don't copy paste the code of the loop and the error, it could be a million things.

Find and remove duplicate words in lines in a file using python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to remove extra words that follow the first one in sequence separated by ";" on each line and return just one copy of that sequnce in a line:
Data:
XTY1;XTY3;XTY3;XTY3;XTY2;XTY1;XTY1;XTY1
XTY3;XTY4;XTY4;XTY3;XTY2;XTY7;XTY7;XTY1
XTY10;XTY3;XTY4;XTY2;XTY2;XTY11;XTY11;XTY1
Required output:
XTY1;XTY3;XTY2;XTY1
XTY3;XTY4;XTY3;XTY2;XTY7;XTY1
XTY10;XTY3;XTY4;XTY2;XTY11
My code is as follows:
for line in cluster3_urls:
list_of_words = line.split(',')
for i in list_of_words:
next_word = list_of_words[list_of_words.index(i) + 1]
if list_of_words == next_word:
list_of_words=list_of_words
print list_of_words
Can someone please let me know why my code did not work?
Many things wrong with your code. Consider itertools.groupby:
from itertools import groupby
input = 'XTYYY1;XTYYY3;XTYYY3;XTYYY3;XTYYY2;XTYYY1;XTYYY1;XTYYY1'
output = ';'.join([k for k, g in groupby(input.split(';'))])
# output: 'XTYYY1;XTYYY3;XTYYY2;XTYYY1'
I think the problem is that you are spiting based on commas instead of semicolon
try change the line to
list_of_words = line.split(';')
Your code right now is failing because you are splitting on the wrong delimiter. In addition, once you fix that, your code will additionally fail on next_word = list_of_words[list_of_words.index(i) + 1] when you reach the last word. The rest of your code just makes no sense, with your if statement comparing an array to an element of that array, then setting that arrow to itself (doing nothing). I recommend you completely rewrite your code.

cant assign to function call [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I dont understand why this is happening.. It is not happening with anyone else I know.... Here is my code so far
# creating a string which will be our main sentence
string = input("Please enter a sentence.\n")
str(string)
# creates a list and then splits the string up and puts all the parts into a list
stringlist = []
string.lower()
string = string.split()
stringlist.append(string)
# prints the list to check for any errors during splitting
print(stringlist)
find = input("Which word would you like to find?\n")
while find in stringlist:
index = stringlist.index(find)
stringlist(index) = ""
indexpositions.append(str(index + 1))
What I am trying to do is to find a word in a sentence and find all the indexes of it.
You're using the wrong parentheses to index stringlist. You should do stringlist[index]=""
List of problems I could find
In Python, you need to use [] access the elements of a list. So you need to change your code to
stringlist[index] = ""
indexpositions.append(str[index + 1])
Apart from that,
str(string)
...
string.lower()
are NOOPs. First line simply converts the string to a string object and discards it immediately and the second simply converts the string to lower case string and returns a new string object and that is also ignored. Probably you meant
string = str(string)
...
string = string.lower()
Also, the str(string) part is not necessary, because input function returns a string object only.
Another problem is, string.find, returns -1 if the item is not found in the list. In Python sequences can have negative indexes. So, you may want to be aware of that case as well.
So your code can be written like this
stringlist = input("Please enter a sentence.\n").lower().split()

Split a string by a comma and cast the second half as an int [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
With the following expected input:
[u'able,991', u'about,11', u'burger,15', u'actor,22']
How can I split each string by the comma and return the second half of the string as an int?
This is what I have so far:
def split_fileA(line):
# split the input line in word and count on the comma
<ENTER_CODE_HERE>
# turn the count to an integer
<ENTER_CODE_HERE>
return (word, count)
One of the first things you'll need in learning how to code, is to get to know the set of functions and types you have natively available to you. Python's built-in functions is a good place to start. Also get the habit of consulting the documentation for the stuff you use; it's a good habit. In this case you'll need split and int. Split does pretty much what it says, it splits a given string into multiple tokens, given a separator. You'll find several examples with a simple search in google. int, on the other hand, parses a string (one of the things it does) into a numeric value.
In your case, this is what it means:
def split_fileA(line):
# split the input line in word and count on the comma
word, count = line.split(',')
# turn the count to an integer
count = int(count)
return (word, count)
You won't get this much here in stackoverflow, has other users are often reluctant to do your homework for you. It seems to me that you are at the very beginning of learning how to code so I hope this helps you get started, but remember that learning is also about trial and error.

re.findall working in console but not in script? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm probably missing something very basic here, but here goes:
I'm using Python 2.7 and regex to identify digits within a string.
In the console, I type in:
>>> newstr = 'NukeNews/File_132.txt'
>>> int(re.findall(r'\d+',newstr)[0])
132
Which is what I expect.
However, in the script I'm running, I have the strings stored in a dictionary, linedict. I'm running this script:
news_id=[]
for line in line_vec:
print linedict[line]
newstr= linedict[line]
id_int = re.findall('r\d+',newstr)
print id_int
news_id.append(id_int)
It's a long list, but the output looks like:
NukeNews/File_132.txt
[]
So - the correct string is registered, but it's not matching on anything.
I was calling the first item in the list earlier (to match the console input of int(re.findall(r'\d+',newstr)[0]), but the script is telling me that the regex didn't find any instances of the digits in the string. I would expect this to return:
NukeNews/File_132.txt
['132']
Any idea why it's not working as expected? When I try running re.match(r'/d+',newstr) I also get an empty group (following the groups example on https://docs.python.org/2/library/re.html).
Edit: As pointed out, this is a case of not being careful with 'r' and r'*'. I'm just going to leave this up in case anyone else googling "why does my regex work in console but not in script" forgets to check this typo, like I did.
You've got your r inside the quotes so instead of getting a "raw string" you're getting a string with an 'r' in it ...
id_int = re.findall('r\d+',newstr)
# ^
# should be:
id_int = re.findall(r'\d+',newstr)
your "console" version also only takes the first of the found matches compared to your "script" version which appends the entire list.

Categories

Resources