艰苦地学习Python,练习25,没有得到预期的结果
我正在努力学习Python的练习25,但并没有得到预期的结果。当我在参数单词(这是一个列表)上调用print_first_单词时,我在shell中键入: ex25.打印第一个单词(单词) 我被告知我应该看到: 全部 相反,我明白了 等等 值得一提的是,这句话是: [“所有”、“好”、“事物”、“来”、“到”、“那些”、“谁”、“等等”。] 这是我的密码:艰苦地学习Python,练习25,没有得到预期的结果,python,Python,我正在努力学习Python的练习25,但并没有得到预期的结果。当我在参数单词(这是一个列表)上调用print_first_单词时,我在shell中键入: ex25.打印第一个单词(单词) 我被告知我应该看到: 全部 相反,我明白了 等等 值得一提的是,这句话是: [“所有”、“好”、“事物”、“来”、“到”、“那些”、“谁”、“等等”。] 这是我的密码: def break_words(stuff): """This Function will break up words for us
def break_words(stuff):
"""This Function will break up words for us."""
words = stuff.split(' ')
return words
def sort_words(words):
"""Sorts the words."""
return sorted(words)
def print_first_word(words):
"""Prints the first word after popping it off."""
word = words.pop(0)
print word
def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1)
print word
def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)
return sort_words(words)
def print_first_and_last(sentence):
"""Prints the first and last words of the sentence"""
words = break_words(sentence)
print_first_word(words)
print_last_word(words)
def print_first_and_last_sorted(sentence):
"""Sorts the words then prints the first and last one."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)
我的猜测是,您正在空闲(或另一个交互式shell)中执行此操作,并且您以前的“测试运行”影响了您的输入。首先尝试打印
ingwords
,看看它是否符合您的期望。您的代码看起来是正确的
请记住,list.pop
(正如您在函数中所做的那样,print\u first\u words)
实际上会将其目标从列表中删除。也就是说:
words = ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
words.pop(0) # returns 'All', but since I don't assign it anywhere, who cares
print(words)
# ['good', 'things', 'come', 'to', 'those', 'who', 'wait.']
如果您不想从列表中删除某个元素,请不要将其pop
words = ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
words[0] # ALSO returns 'All', though again I'm not doing anything with it
print(words)
# ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
我的猜测是,您正在空闲(或另一个交互式shell)中执行此操作,而您以前的“测试运行”影响了您的输入。请先尝试print
ingwords
,看看它是否符合您的预期。您的代码看起来很正确
请记住,list.pop
(正如您在函数中所做的那样,print\u first\u words)
实际上会将其目标从列表中删除。也就是说:
words = ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
words.pop(0) # returns 'All', but since I don't assign it anywhere, who cares
print(words)
# ['good', 'things', 'come', 'to', 'those', 'who', 'wait.']
如果您不想从列表中删除某个元素,请不要将其pop
words = ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
words[0] # ALSO returns 'All', though again I'm not doing anything with it
print(words)
# ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
我的猜测是,您正在空闲(或另一个交互式shell)中执行此操作,而您以前的“测试运行”影响了您的输入。请先尝试print
ingwords
,看看它是否符合您的预期。您的代码看起来很正确
请记住,list.pop
(正如您在函数中所做的那样,print\u first\u words)
实际上会将其目标从列表中删除。也就是说:
words = ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
words.pop(0) # returns 'All', but since I don't assign it anywhere, who cares
print(words)
# ['good', 'things', 'come', 'to', 'those', 'who', 'wait.']
如果您不想从列表中删除某个元素,请不要将其pop
words = ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
words[0] # ALSO returns 'All', though again I'm not doing anything with it
print(words)
# ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
我的猜测是,您正在空闲(或另一个交互式shell)中执行此操作,而您以前的“测试运行”影响了您的输入。请先尝试print
ingwords
,看看它是否符合您的预期。您的代码看起来很正确
请记住,list.pop
(正如您在函数中所做的那样,print\u first\u words)
实际上会将其目标从列表中删除。也就是说:
words = ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
words.pop(0) # returns 'All', but since I don't assign it anywhere, who cares
print(words)
# ['good', 'things', 'come', 'to', 'those', 'who', 'wait.']
如果您不想从列表中删除某个元素,请不要将其pop
words = ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
words[0] # ALSO returns 'All', though again I'm not doing anything with it
print(words)
# ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
有两件事你应该牢记在心。首先,pop()
方法修改它所使用的列表;一旦你使用pop()
访问一个项目,它就不再在列表中。请看以下命令序列:
>>> words = ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
>>> words.pop(0)
'All'
>>> words.pop(0)
'good'
>>> words.pop(0)
'things'
>>> words.pop(0)
'come'
>>> words.pop(0)
'to'
>>> words.pop(0)
'those'
>>> words.pop(0)
'who'
>>> words.pop(0)
'wait.'
>>> words.pop(0)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
words.pop(0)
IndexError: pop from empty list
>>>
>>words=['All','good','things','come','to','that','who','wait.]
>>>words.pop(0)
“全部”
>>>words.pop(0)
“很好”
>>>words.pop(0)
“事情”
>>>words.pop(0)
“来吧”
>>>words.pop(0)
“到”
>>>words.pop(0)
“那些”
>>>words.pop(0)
“谁
>>>words.pop(0)
“等等。”
>>>words.pop(0)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
words.pop(0)
索引器:从空列表中弹出
>>>
第二,这是相当令人困惑的,列表是通过引用传递的。这意味着当您调用print\u first\u words(words)
,words.pop(0)
不仅仅修改函数中的局部变量。它修改了您的原始列表!因此,如果您调用print\u first\u words(words)
多次,每次的输出都会有所不同,类似于上面的内容
您可以通过使用word=words[0]
来解决这个问题,它只检索0处的值,而不是word=words.pop(0)
有两件事需要记住。首先,pop()
方法修改它所使用的列表;一旦您使用pop()访问项目
,它不再在列表中。请查看以下命令序列:
>>> words = ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
>>> words.pop(0)
'All'
>>> words.pop(0)
'good'
>>> words.pop(0)
'things'
>>> words.pop(0)
'come'
>>> words.pop(0)
'to'
>>> words.pop(0)
'those'
>>> words.pop(0)
'who'
>>> words.pop(0)
'wait.'
>>> words.pop(0)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
words.pop(0)
IndexError: pop from empty list
>>>
>>words=['All','good','things','come','to','that','who','wait.]
>>>words.pop(0)
“全部”
>>>words.pop(0)
“很好”
>>>words.pop(0)
“事情”
>>>words.pop(0)
“来吧”
>>>words.pop(0)
“到”
>>>words.pop(0)
“那些”
>>>words.pop(0)
“谁
>>>words.pop(0)
“等等。”
>>>words.pop(0)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
words.pop(0)
索引器:从空列表中弹出
>>>
第二,这是相当令人困惑的,列表是通过引用传递的。这意味着当您调用print\u first\u words(words)
,words.pop(0)
不仅仅修改函数中的局部变量。它修改了您的原始列表!因此,如果您调用print\u first\u words(words)
多次,每次的输出都会有所不同,类似于上面的内容
您可以通过使用word=words[0]
来解决这个问题,它只检索0处的值,而不是word=words.pop(0)
有两件事需要记住。首先,pop()
方法修改它所使用的列表;一旦您使用pop()访问项目
,它不再在列表中。请查看以下命令序列:
>>> words = ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
>>> words.pop(0)
'All'
>>> words.pop(0)
'good'
>>> words.pop(0)
'things'
>>> words.pop(0)
'come'
>>> words.pop(0)
'to'
>>> words.pop(0)
'those'
>>> words.pop(0)
'who'
>>> words.pop(0)
'wait.'
>>> words.pop(0)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
words.pop(0)
IndexError: pop from empty list
>>>
>>words=['All','good','things','come','to','that','who','wait.]
>>>words.pop(0)
“全部”
>>>words.pop(0)
“很好”
>>>words.pop(0)
“事情”
>>>words.pop(0)
“来吧”
>>>words.pop(0)
“到”
>>>words.pop(0)
“那些”
>>>words.pop(0)
“谁
>>>words.pop(0)
“等等。”
>>>words.pop(0)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
words.pop(0)
索引器:从空列表中弹出
>>>
第二,这是相当令人困惑的,列表是通过引用传递的。这意味着当您调用print\u first\u words(words)
,words.pop(0)
不仅仅修改函数中的局部变量。它修改了您的原始列表!因此,如果您调用print\u first\u words(words)