python用for循环反转列表_使用函数和for循环反转列表项的内部序列。

我需要帮助理解一个给我带来很多麻烦的家庭作业。我尝试了许多不同的方法来获得以下赋值,以产生所需的结果: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天,而且我没有得到教授或学生的支持(我已经通知了我的导师)。在

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值