m0_62832806 2021-11-23 07:32 采纳率: 90%
浏览 9
已结题

如何用list和相关函数解决问题

It is oftentimes advantageous to be able to transfer data between multiple lists while rearranging their order. For instance, say that list1 = [1,2,3,4,5,6,7,8,9] and you wish to add the numbers in the index range 4:7 of list1 to another list, list2, in reverse order while simultaneously removing them from list1. If list2 = [100,200], the result will be list2 = [100,200,7,6,5]. Write a function named transform that takes as arguments list1, list2, r1, and r2, that removes items from list1 in the slice r1:r2, appends them onto list2 in reverse order, and returns the resulting list. For example, in this case, the function call will be as the following:

transform(list1, list2, 4,7).

请按功能描述实现transform函数,并设计合适的主程序调用该函数。

  • 写回答

1条回答 默认 最新

  • chuifengde 2021-11-23 08:31
    关注
    def transform(list1, list2, r1, r2):
        res = list2 + list1[r1:r2][::-1]
        list1[r1:r2] = []
        return res
    
    list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    list2 = [100, 200]
    r1 = 4
    r2 = 7
    r = transform(list1, list2, r1, r2)
    print(list1, r)
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月1日
  • 已采纳回答 11月23日
  • 创建了问题 11月23日

悬赏问题

  • ¥20 c语言写的8051单片机存储器mt29的模块程序
  • ¥60 求直线方程 使平面上n个点在直线同侧并且距离总和最小
  • ¥50 java算法,给定试题的难度数量(简单,普通,困难),和试题类型数量(单选,多选,判断),以及题库中各种类型的题有多少道,求能否随机抽题。
  • ¥50 rk3588板端推理
  • ¥250 opencv怎么去掉 数字0中间的斜杠。
  • ¥15 这种情况的伯德图和奈奎斯特曲线怎么分析?
  • ¥250 paddleocr带斜线的0很容易识别成9
  • ¥15 电子档案元素采集(tiff及PDF扫描图片)
  • ¥15 flink-sql-connector-rabbitmq使用
  • ¥15 zynq7015,PCIE读写延时偏大