我需要帮助理解一个给我带来很多麻烦的家庭作业。我尝试了许多不同的方法来获得以下赋值,以产生所需的结果:Create a module named task_05.py
Create a function named flip_keys() that takes one argument:
a. A list named to_flip. This list is assumed to have nested, immutable sequences inside it, eg: [(1, 2, 3), 'hello']
Use a for loop to loop the list and reverse the order of the inner sequence. All operations on the outer list must operate on the
original object, taking advantage of its mutability. Inner elements
are immutable and will require replacement.
The function should return the original list with its inner elements reversed.
我的教授将通过将以下内容输入python shell来评估脚本的结果:>>> LIST = [(1, 2, 3), 'abc']
>>> NEW = flip_keys(LIST)
>>> LIST is NEW
True
>>> print LIST
[(3, 2, 1), 'cba']
我不知道我做错了什么,我的教授也没有回应。学生们也没有回答,我已经复习了很多次材料试图找到答案。我脑子里有点不对劲。在
他提供了以下提示,我相信我已经在我的剧本中体现了这一点:Hint
Consider how to access or change the value of a list. You did it
already in task 2!
Hint
In order to change the value in to_flip you'll need some way to know
which index you're attempting to change. To do-this, create a variable
to act as a counter and increment it within your loop, eg:
counter = 0 for value in iterable_object:
do something counter += 1 Now consider what that counter could represent. At the end of this loop does counter ==
len(iterable_object)
Hint
For an idea on how to reverse a tuple, head back to an earlier
assignment when you reversed a string using the slice syntax.
以下是我最新的没有注释的脚本(因为在脚本运行之前我不会写它们):
^{pr2}$
当我使用上面粘贴的命令测试脚本时,我得到以下结果:
^{3}$
这项作业的目的是体验易变性,我想我能理解。我面临的问题是,假设LIST变量被函数更新,但这从来没有发生过。在
下面的计算结果应该是True,而不是false。然后打印存储在list常量中的反向列表值。在>>>LIST = [(1, 2, 3), 'abc']
>>>NEW = flip_keys(LIST)
>>>LIST is NEW False
请告诉我这是不是足够的信息。我在这方面花了太多的时间,现在我的作业迟到了4天,而且我没有得到教授或学生的支持(我已经通知了我的导师)。在