Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改代码以使用列表理解_Python - Fatal编程技术网

Python 更改代码以使用列表理解

Python 更改代码以使用列表理解,python,Python,对Python非常陌生,并尝试通过一些在线示例自学。我解决了一个问题,但当我和一个朋友谈话时,他告诉我应该使用列表理解来完成这样的任务。唯一的问题是,我不知道如何使用列表理解而不是生成器函数来实现任务。这是有效的代码,非常感谢您的帮助 #!/usr/bin/python def find_longest_word(sentence): word = sentence.split() long_word = max(len(s) for s in word) # [x for x

对Python非常陌生,并尝试通过一些在线示例自学。我解决了一个问题,但当我和一个朋友谈话时,他告诉我应该使用列表理解来完成这样的任务。唯一的问题是,我不知道如何使用列表理解而不是生成器函数来实现任务。这是有效的代码,非常感谢您的帮助

#!/usr/bin/python

def find_longest_word(sentence):
    word = sentence.split()
    long_word = max(len(s) for s in word) # [x for x in range]    
    print "The length of the longest word is: ",long_word
    #return
find_longest_word("The quick brown fox jumps over the lazy dog with pneumonia") # For testing

谢谢

列表理解似乎不适合您寻求的结果。相反,您可以使用位置参数键将列表
word
传递给内置函数
max()
:该函数对
word
中的每个元素进行操作,并返回一个值(在本例中为长度)作为排序值:

len(max(word,key=len))

列表理解似乎不适合你所寻求的结果。相反,您可以使用位置参数键将列表
word
传递给内置函数
max()
:该函数对
word
中的每个元素进行操作,并返回一个值(在本例中为长度)作为排序值:

len(max(word,key=len))

让我们快速讨论一下列表理解

让我们为循环取一个普通的
,如下所示:

sentence = 'Hello how are you doing today?'
lengths = []
for word in sentence.split():
    lengths.append(len(word))
这相当于:

[len(word) for word in sentence.split()]
单for循环列表理解的正常语法是
[列表中值的值]
在这里,您可以看到列表中值的
,该值与正常for循环相同。唯一的区别是返回的值不是在for循环之后,而是在for循环之前


对于您的案例,您可以这样做:
max([len(word)表示句子中的单词。split()])
让我们来谈谈列表理解快速

让我们为循环
取一个普通的
,如下所示:

sentence = 'Hello how are you doing today?'
lengths = []
for word in sentence.split():
    lengths.append(len(word))
这相当于:

[len(word) for word in sentence.split()]
单for循环列表理解的正常语法是
[列表中值的值]
在这里,您可以看到列表中值的
,该值与正常for循环相同。唯一的区别是返回的值不是在for循环之后,而是在for循环之前


对于您的案例,您可以这样做:
max([len(word)表示句子中的单词。split()])
要使用列表理解,您可以将
max(len(s)表示单词中的s)
替换为
max([len(s)表示单词中的s])
,但不需要这样做,两个示例都可以正常工作。为了解决这个任务,你只需要理解这个列表理解是如何工作的。对于初学者来说,更容易理解其工作原理:

def find_longest_word(sentence):
    res = []
    words = sentence.split()
    for word in words:
        res.append(len(word))
    long_word = max(res)
    print "The length of the longest word is: ", long_word

find_longest_word("The quick brown fox jumps over the lazy dog")

而不是使用一些列表理解或生成器。

要使用列表理解,您可以将
max(len(s)表示word中的s)
替换为
max([len(s)表示word中的s])
,但无需这样做,两个示例都可以正常工作。为了解决这个任务,你只需要理解这个列表理解是如何工作的。对于初学者来说,更容易理解其工作原理:

def find_longest_word(sentence):
    res = []
    words = sentence.split()
    for word in words:
        res.append(len(word))
    long_word = max(res)
    print "The length of the longest word is: ", long_word

find_longest_word("The quick brown fox jumps over the lazy dog")

而不是使用一些列表理解或生成器。

比列表理解更好的是,使用高阶函数(可以将另一个函数作为参数的函数),例如
max
max
参数将应用于
语句中的每个元素。split()
并根据该参数确定顺序。以下是几个例子:

>>> def find_longest_word(sentence):
...     longest = max(sentence.split(), key=len)
...     print(longest, len(longest))
...
>>> find_longest_word("The quick brown fox jumps over the lazy dog")
quick 5
>>> find_longest_word("The quick brown fox juuuuumps over the lazy dog")
juuuuumps 9
>>>

请注意,
len
是python的内置函数,用于确定对象的长度

比列表理解更好的是,使用高阶函数(可以将另一个函数作为参数的函数),例如
max
max
参数将应用于
语句中的每个元素。split()
并根据该参数确定顺序。以下是几个例子:

>>> def find_longest_word(sentence):
...     longest = max(sentence.split(), key=len)
...     print(longest, len(longest))
...
>>> find_longest_word("The quick brown fox jumps over the lazy dog")
quick 5
>>> find_longest_word("The quick brown fox juuuuumps over the lazy dog")
juuuuumps 9
>>>

请注意,
len
是python的内置函数,用于确定对象的长度

刚刚发布了我的版本。。。只是忍不住要问一个列表理解问题——多么像蟒蛇

>>> s = "The quick brown fox jumps over the lazy dog with pneumonia"
>>> w = max([word for word in s.split()], key=len)
>>> print('{} {}'.format(w, len(w)))
pneumonia 9

只是不得不发布我的版本。。。只是忍不住要问一个列表理解问题——多么像蟒蛇

>>> s = "The quick brown fox jumps over the lazy dog with pneumonia"
>>> w = max([word for word in s.split()], key=len)
>>> print('{} {}'.format(w, len(w)))
pneumonia 9

max([len(s)表示句子中的s.split()])
这里几乎没有理由使用列表理解。genexp更好。
max([len(s)表示句子中的s.split()])
这里几乎没有理由使用列表理解。genexp更好。为什么要将
word
作为星型参数传递?如果
word
只有一个元素,这只会让事情变得一团糟。@user2357112已更正,谢谢您为什么要将
word
作为星号参数传递?如果
word
只有一个元素,这只会让事情变得一团糟。@user2357112更正,谢谢Hanks ZWiki。只是为了表示我完全的无知,但为什么这比我写的代码要好呢?我不明白为什么使用列表理解会让代码变得更好(事实上,我发现它更难阅读),所以它不是同样的东西,只是格式不同吗?@Signpost87,看看这篇文章:虽然
list-comprehension
对于循环来说或多或少只是语法上的,但你可以使用生成器函数(对于大型循环更好,因为它将保留下一次循环中的位置索引)@ZWiki:在您的建议中,
max([len(word)表示句子中的单词。split()])
无需使用方括号将列表变为现实,
max(len(word)表示句子中的单词。split())
工作同样良好,而且很可能表现得更好。@PedroMDuarte使用
timeit.timeit
来比较两者的时间,您将surprised@PedroMDuarte您的代码生成一个
生成器
。虽然它更适合大型列表,但
生成器
的初始设置时间比
列表压缩时间更重要hension
用于小循环Hanks ZWiki。只是为了表明我完全无知,但为什么这比我编写的代码要好?我不明白为什么使用列表理解会使代码更好(实际上我发现很难重新编写)