按字母顺序排序 if 语句不起作用
下面的 if 语句在某个地方有问题,我无法弄清楚。是否有任何约定或方法误用可能导致其无法正常运行? checkList 是用户输入的句子,lis 是一个很大的单词列表。
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 checkList 是一个字符串,那么有
不需要
"".join(checkList)
。它只是给你同样的回报
字符串:
第一行,
string =
有错误"".join(checkList)
缩进。将其移回原来的位置
与其他线齐平
定义。
不要将变量命名为
字符串
。它覆盖标准Python模块
同名。
大概
match(wordList, lis)
返回一个列表。排序方法
对列表进行排序,并返回
None
。由于
None == None
为True
,永远正确。
更有可能的是,你想要的是
与
sort
方法不同,sorted 函数返回
排序列表。
正如 Alex Martelli 指出的,
始终具有与
相同的真值
因此使用
str.lower
作为键
用于排序(而不是作为
比较之前进行转换
==
) 可能不是您想要的。声明
可以简化为
If checkList is a string, then there
is no need for
"".join(checkList)
.It just gives you back the same
string:
The first line,
string =
has the wrong"".join(checkList)
indentation. Move it back to be
flush with the other lines in the
definition.
Don't name a variable
string
. Itoverrides 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
isTrue
,is always true.
More likely, what you want is
Unlike the
sort
method, thesorted function returns the
sorted list.
As Alex Martelli points out,
always has the same truth value as
So using
str.lower
as thekey
for sorting (rather than as a
transformation before comparing with
==
) is probably not what you want.The statement
can be simplified to
.sort
与容器的几乎所有其他 mutator 方法一样,返回None
。因此,将a.sort()
与b.sort()
进行比较是荒谬的,因为它们都是None
!我认为您想将sorted(match(wordList, lis), key=str.lower)
与sorted(worldList, key=str.lower)
进行比较。请注意,
key
实际上与您使用它的方式无关:如果两个列表包含大小写不同的项目,则它们将不比较相等,即使它们是“比较”排序!因此,更好的想法可能是将
sorted(s.lower() for s in match(wordList, lis))
与sorted(s.lower() for s in worList)
进行比较代码>.请注意,此处不需要key=
,因为您正在比较小写 项目,因此它们会“按性质”按这种方式排序。.sort
, like just about every other mutator method of containers, returnsNone
. So comparinga.sort()
tob.sort()
is absurd because they'll both beNone
! I think you want to comparesorted(match(wordList, lis), key=str.lower)
withsorted(worldList, key=str.lower)
.Note that the
key
is actually irrelevant the way you're using it: if the two lists have items that differ in case, they will not compare equal even if they're sorted "comparably"!So a better idea might be to compare
sorted(s.lower() for s in match(wordList, lis))
withsorted(s.lower() for s in worList)
. Note that thekey=
is unneeded here since you're comparing the lowercased items so they'll sort that way "by nature".