The if statement below has a problem in it somewhere and I can not figure it out. Any conventions or method misuses that might be causing it to not function right? checkList is a user inputed sentence and lis is a large list of words.
def realCheck(checkList):
string = "".join(checkList)
print string
wordList = string.split()
if match(wordList, lis).sort(key=str.lower) == wordList.sort(key=str.lower):
return True
else:
return False
If checkList is a string, then there
is no need for "".join(checkList)
.
It just gives you back the same
string:
In [94]: checkList="This is a sentence"
In [95]: "".join(checkList)
Out[95]: 'This is a sentence'
The first line, string =
"".join(checkList)
has the wrong
indentation. Move it back to be
flush with the other lines in the
definition.
Don't name a variable string
. It
overrides the standard Python module
of the same name.
Presumably match(wordList, lis)
returns a list. The sort method
sorts the list, and returns None
.
Since None == None
is True
,
if match(wordList, lis).sort(key=str.lower) == wordList.sort(key=str.lower):
is always true.
More likely, what you want is
sorted(astr.lower() for astr in match(wordList, lis))==sorted(astr.lower() for astr in wordList)
Unlike the sort
method, the
sorted function returns the
sorted list.
As Alex Martelli points out,
sorted(match(wordList, lis),key=str.lower)==sorted(wordList,key=str.lower)
always has the same truth value as
sorted(match(wordList, lis))==sorted(wordList)
So using str.lower
as the key
for sorting (rather than as a
transformation before comparing with
==
) is probably not what you want.
The statement
if condition:
return True
else:
return False
can be simplified to
return condition