Python list of sections of the four common operations

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/lovenankai/article/details/102548379

640?wx_fmt=png

Original white series for the first  7  Pian
White: small yards brother, you help me with a few questions? I want to extract some or all of the elements of an even positions of the list? This information is often encountered in the data analysis.

White: In addition, through the first two articles

Small yards brother: Today, we talk together about the slice list. It is very easy to use a point, certainly can help you solve the problem.


01
What is a slice?


Slice is a list of some elements of the processing of the list, the entire list is cut.

It is the focus of the entire contents of the list, frequently used in future Python projects.

grammar:

     [start:end:step]

     start: starting index from 0 to start

     End : the end of the index , but the end-1 is the actual index value

     the STEP : step, step is positive, the value from left to right. Step is negative, the reverse value

    Note sliced end result does not contain an index that does not include the last one, -1 last position in the list of index represents

640

02
Four slices of common operations

《扶摇》是去年很火的一个电视剧。咱们今天使用列表存储里面的角色,然后用切片把它分开。具体如下?

name_fuyao = ['扶摇','周叔','国公','无级太子','医圣','非烟殿主','穹苍']

接下来,咱们使用上面的列表信息,盘点一下列表切片的常见用法:

1. 利用切片,根据位置信息提取列表中的元素

# 指定开始和结束位置,注意不包括最后的位置元素

print('扶摇电视列表中第三个到第五个人物的名字:',name_fuyao[2:5])

# 不指定开始的位置,则默认从头开始

print('扶摇电视列表中前5个人物名字:',name_fuyao[:5])

# 不指定结束的位置,则从开始位置到结束

print('扶摇电视列表从6个人物的名字:',name_fuyao[5:])

# 开始和结束位置都不指定

print('扶摇电视列表中的名字:',name_fuyao[:])

# 负数索引表示返回距离列表末位相应距离的元素,也就是取列表中后半部分的元素

print('扶摇电视列表中最后三个人物的名字:',name_fuyao[-3:])

# 取偶数位置的元素

print('扶摇电视列表中偶数位置的人物是:',name_fuyao[::2])

# 取奇数位置的元素

print('扶摇电视列表中数位置的人物是:',name_fuyao[1::2])

以上代码输出结果是:

扶摇电视列表中第三个到第五个人物的名字: ['国公', '无级太子', '医圣']

扶摇电视列表中前5个人物名字: ['扶摇', '周叔', '国公', '无级太子', '医圣']

扶摇电视列表从6个人物的名字: ['非烟殿主', '穹苍']

扶摇电视列表中的名字: ['扶摇', '周叔', '国公', '无级太子', '医圣', '非烟殿主', '穹苍']

扶摇电视列表中最后三个人物的名字: ['医圣', '非烟殿主', '穹苍']

扶摇电视列表中偶数位置的人物是: ['扶摇', '国公', '医圣', '穹苍']

扶摇电视列表中奇数位置的人物是: ['周叔', '无级太子', '非烟殿主']

640

2. 利用切片逆序列表

# 逆序列表,相当于reversed(list)

print('扶摇电视列表中人物颠倒顺序:',name_fuyao[::-1])

以上代码输出结果是:

扶摇电视列表中人物颠倒顺序: ['穹苍', '非烟殿主', '医圣', '无级太子', '国公', '周叔', '扶摇']

3. 利用切片插入多个元素

# 在某个位置插入多个元素

# 也可以用同样的方法插入或者删除多个元素

name_fuyao[3:3]=['玄机','太渊','天煞']

print('扶摇电视列表中人物变为:',name_fuyao)

以上代码输出结果是:

扶摇电视列表中人物变为: ['扶摇', '周叔', '国公', '玄机', '太渊', '天煞', '无级太子', '医圣', '非烟殿主', '穹苍']

640

4. 利用切片复制列表

# 复制列表,相当于copy(),复制以后的新的列表是一个新的,可以对其操作

# Note that if new_name_fuyao = name_fuyao is variable assignment, which is the same value for the two variables, a change in the value, followed by the other two values changed.

new_name_fuyao = name_fuyao[:]

print ( ' new list element : {}'. format (new_name_fuyao ))

The above code is output:

The new list element : [ ' Spread upward ', ' peripheral t ', ' State public ', ' tricky ', ' very deep ', ' Independence Day "," stepless Prince ', ' Medical St ', ' non-tobacco Dianzhu ',' expanse ']

Above excerpt from "zero-based Easy Python"

640?wx_fmt=png

Guess you like

Origin blog.csdn.net/lovenankai/article/details/102548379