Python | 从另一个列表的指定开始到结束索引创建一个列表

2023-05-16

Given a list, start and end index, we have to create a list from specified index of the list in Python.

给定一个列表,开始和结束索引,我们必须根据Python中列表的指定索引创建一个列表。

Example 1:

范例1:

    Input:
    list : [10, 20, 30, 40, 50, 60]
    start = 1
    end = 4

    Logic to create list with start and end indexes:
    List1 = list[start: end+1]

    Output:
    list1: [20, 30, 40, 50]

Example 2:

范例2:

    Input:
    list : [10, 20, 30, 40, 50, 60]
    start = 1
    end   = 6

    Logic to create list with start and end indexes:
    list1 = list[start: end+1]

    Output:
    Invalid end index

Logic:

逻辑:

  • Take a list, start and end indexes of the list.

    取得列表,列表的开始和结束索引。

  • Check the bounds of start and end index, if start index is less than 0, print the message and quit the program, and if end index is greater than the length-1, print the message and quit the program.

    检查开始和结束索引的界限,如果起始索引小于0,则打印该消息并退出该程序,并且如果结束索引大于长度-1时,打印该消息并退出该程序。

  • To create a list from another list with given start and end indexes, use list[n1:n2] notation, in the program, the indexes are start and end. Thus, the statement to create list is list1 = list[start: end+1].

    要使用给定的开始索引和结束索引从另一个列表创建列表,请使用list [n1:n2]表示法,在程序中,索引为start和end 。 因此,创建列表的语句为list1 = list [start:end + 1] 。

  • Finally, print the lists.

    最后,打印列表。

Program:

程序:

# define list 
list = [10, 20, 30, 40, 50, 60]

start = 1
end = 4

if ( start < 0):
    print "Invalid start index"
    quit()

if( end > len(list)-1):
    print "Invalid end index"
    quit ()

# create another list
list1 = list[start:end+1]

# printth lists 
print "list : ", list
print "list1: ", list1

Output

输出量

    list :  [10, 20, 30, 40, 50, 60]
    list1:  [20, 30, 40, 50]

使用无效索引进行测试 (Test with the invalid index)

Size of the list is 6, and indexes are from 0 to 5, in this example the end index is invalid (which is 6), thus program will print "Invalid end index" and quit.

列表的大小为6,索引从0到5,在此示例中,结束索引无效(为6),因此程序将打印“ Invalid end index”并退出。

Note: Program may give correct output if end index is greater than the length-1 of the list. But, to execute program without any problem, we should validate the start and end index.

注意:如果结束索引大于列表的长度1 ,则程序可能会给出正确的输出。 但是,要执行程序没有任何问题,我们应该验证开始索引和结束索引。

# define list 
list = [10, 20, 30, 40, 50, 60]

start = 1
end = 6

if ( start < 0):
    print "Invalid start index"
    quit()

if( end > len(list)-1):
    print "Invalid end index"
    quit ()

# create another list
list1 = list[start:end+1]

# printth lists 
print "list : ", list
print "list1: ", list1

Output

输出量

    Invalid end index


翻译自: https://www.includehelp.com/python/create-a-list-from-the-specified-start-to-end-index-of-another-list.aspx

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python | 从另一个列表的指定开始到结束索引创建一个列表 的相关文章

随机推荐

  • 小米笔试真题一

    小米笔试真题一 第一题第二题第三题第四题第五题第六题第七题第八题第九题第十题第十一题第十二题第十三题总结 第一题 下述算法的时间复杂度为 xff08 xff09 A O log2n B O n C O nlog2n D O n 2 span
  • C语言数组专题训练

    C语言数组专题训练 第一题第二题第三题第四题第五题第六题第七题第八题第九题第十题第十一题 第一题 若有语句char s1 10 s2 10 61 books xff1b 则能将字符串 books 存放到数组 s1 的正确语句是 A strc
  • 逻辑结构与物理结构

    逻辑结构与物理结构 逻辑结构集合结构线性结构树形结构图形结构 物理结构 逻辑结构 简单的来说 xff0c 逻辑结构就是数据之间的关系 逻辑结构常见有四种类型 xff1a 集合结构 xff0c 线性结构 xff0c 树形结构 xff0c 图形
  • 乐鑫面试流程

    乐鑫面试流程 面试岗位笔试技术面试HR面电话聊天发offer 面试岗位 嵌入式软件实习生 笔试 题目分为选择题和编程题 xff0c 选择题二十题 xff0c 编程题两题 xff1b 选择题基本是一些计算机相关基础知识 xff0c 比较简单
  • 全局变量和静态变量的初始化

    全局变量和静态变量的初始化 全局变量 static变量初始化时间静态局部变量全局变量 不要写出和编译顺序相关的程序总结 全局变量 static变量初始化时间 静态局部变量 首先 xff0c 静态局部变量和全局变量一样 xff0c 数据都存放
  • 不同操作系统及CPU字长、寻址能力、指针宽度的理解

    不同操作系统及CPU字长 寻址能力 指针宽度的理解 字长CPU位宽CPU的寻址能力操作系统32bit 64bit指针大小 字长 64位CPU和32位CPU中64和32的含义 xff1a 64和32指的是CPU中的寄存器 通用 的字长 xff
  • new和malloc的区别

    new和malloc的区别 1 new从自由存储区上分配内存 xff0c malloc从堆上分配内存 自由存储区是C 43 43 基于new操作符的一个抽象概念 xff0c 凡是通过new操作符进行内存申请 xff0c 该内存即为自由存储区
  • 程序中的负数存储及类型转换

    程序中的负数存储及类型转换 负数在计算机中怎样存储什么是原码 反码 补码为什么要设置反码 xff0c 补码剖析本质 C语言数据类型转换 xff08 自动类型转换 43 强制类型转换 xff09 自动类型转换强制类型转换类型转换只是临时性的自
  • Java Collections singleton()方法与示例

    集合类singleton 方法 Collections Class singleton method singleton method is available in java util package singleton 方法在java
  • 找素数问题

    span class token macro property span class token directive hash span span class token directive keyword include span spa
  • 嵌入式面试题

    面试题 字符串能直接比较大小吗typedef定义数组类型用法 字符串能直接比较大小吗 C 43 43 中字符串分两种 xff0c 一种是C语言的字符串 xff0c 一种是string字符串 C语言字符串是不可以直接比较大小的 xff0c s
  • 解决Endnote插入参考文献时导致word闪退问题

    问题描述 xff1a 通过endnote插入参考文献时 xff0c 会使得word闪退 原因分析 有像域代码之类的交互 xff0c 与endnote冲突 解决方法把word文档clean下 xff0c 即将域代码删除 解决方法 Ctrl 4
  • 音视频基础

    音视频基础 写在前面基础概念音视频直播推流和拉流什么是推流什么是拉流推流和拉流的区别 协议层 封装格式层 编解码层 像素层RTP RTCP RTMP RTSP区别RTP Real time Transport Protocol 实时传输协议
  • 回车和换行的区别

    回车和换行的区别 回车和换行的概念不同的系统间传递文件会涉及格式的转换Unix gt WindowsUnix lt Windows 回车和换行的概念 首先介绍一下 回车 xff08 carriage return r xff09 和 换行
  • 强大的PubMed插件Scholarscope

    强大的PubMed插件Scholarscope 学术基础 SCI分区什么是Pubmed什么是ScholarscopeScholarscope在不同浏览器下安装指南插件使用 学术基础 SCI分区 SCI是有两个分区 一个是JCR的划分 一般称
  • 反客STM32F4核心板DAP无法下载程序解决

    反客STM32核心板DAP无法下载程序解决 问题解决 问题 反客STM32F407ZGT6核心板使用反客的DAP下载器下载程序 xff0c 无法识别下载器 xff0c 说明下载器没有正常工作 xff08 这里是已经换过杜邦线了 xff0c
  • 有人物联网485转网口模块网口调试助手1035未知错误

    有人物联网485转网口模块网口调试助手1035未知错误 问题解决 问题 项目使用有人物联网485转网口模块USR TCP232 304 xff0c 将模块接入实验室路由器 xff0c IP地址设置为动态IP xff0c 路由器上查得IP为1
  • 1.半导体基础知识

    1 半导体基础知识 本征半导体什么是半导体 xff1f 什么是本征半导体 xff1f 本征半导体的结构本征半导体中的两种载流子为什么将自然界导电性能中等的半导体材料制成本征半导体 杂质半导体N型半导体P型半导体 PN结PN结中的扩散运动漂移
  • 2.半导体二极管

    2 半导体二极管 二极管的组成二极管和PN结伏安特性的区别二极管的伏安特性及电流方程为什么反向饱和电流越小 xff0c 单向导电性能越强 二极管的等效电路二极管的主要参数稳压二极管 xff08 又称齐纳二极管或反向击穿二极管 xff09 稳
  • Python | 从另一个列表的指定开始到结束索引创建一个列表

    Given a list start and end index we have to create a list from specified index of the list in Python 给定一个列表 xff0c 开始和结束索