Python基础(04)——序列类型(列表list、元组tuple、range自定义数字序列)

Python基础(04)——序列类型(列表list、元组tuple、range自定义数字序列)

1. 列表list

列表是一种有序可更改的集合。允许成员元素重复出现

1.1 列表的定义

在Python中,列表用方括号编写,元素可以为任意类型的元素,元素之间使用逗号隔开,第一个元素下标为0。

创建列表:列表的定义有两种方式:

​ 方法一:thislist = [“apple”, “banana”, “cherry”, “orange”, “kiwi”, “melon”, “mango”]

​ 方法二:thislist=list([“apple”, “banana”, “cherry”, “orange”, “kiwi”, “melon”, “mango”])

**索引:**列表可以引用索引号来访问列表项。正索引为从头开始,0表示第一个元素。负索引表示从末尾开始,-1表示最后一个元素,-2表示倒数第二个元素。

**索引范围:**可以用通过指定范围的起点和终点来指定索引范围。返回值为指定范围元素的一个新的列表

​ 切记:无论是正向索引,还是负向索引,均为左闭右开

thislist = list(["1","2"])
thislist = ["apple", "banana", 100, "orange", "kiwi", "melon", "mango"]
print(thislist)
print(thislist[1])
print(thislist[-1])
print(thislist[2:5])    #取2、3、4
print(thislist[-4:-1])  #取-4、-3、-2
#output
# ['apple', 'banana', 100, 'orange', 'kiwi', 'melon', 'mango']
# banana
# mango
# [100, 'orange', 'kiwi']
# ['orange', 'kiwi', 'melon']

1.2 访问列表项

查看列表中的元素有两种方法:

1.使用索引号来获取指定下标的元素。如:thislist[1]

2.通过for循环遍历列表查看列表中的元素。

thislist = ["apple", "banana", 100, "orange", "kiwi", "melon", "mango"]
for x in thislist:
    print(x)

# #output
# apple
# banana
# 100
# orange
# kiwi
# melon
# mango

还可以用关键字**”in“**检查列表中是否存在某元素。

thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
   print("Yes, 'apple' is in the fruits list")

1.3 更改元素值

1.更改特定列表项的值,使用索引号;

2.使用切片的方式,更改列表中一部分的列表项。在切片时也是遵循着左闭右开的原则。

thislist = ["apple", "banana", 100, "orange", "kiwi", "melon", "mango"]
thislist[1] = 200
print(thislist)
thislist[1:3] = [300,500]
print(thislist)
#output
# ['apple', 200, 100, 'orange', 'kiwi', 'melon', 'mango']
# ['apple', 300, 500, 'orange', 'kiwi', 'melon', 'mango']

如果对列表的空切片进行赋值,相当于插入列表项,如果对非空切片赋值为空,相当于删除列表项。

thislist[0:0]=["+","-"]
print(thislist)
thislist[0:1]=[]
print(thislist)
# ['+', '-', 'apple', 300, 500, 'orange', 'kiwi', 'melon', 'mango']
# ['-', 'apple', 300, 500, 'orange', 'kiwi', 'melon', 'mango']

1.4 列表中常用的方法

len(): #用于统计列表的长度    len(thislist)
count():#用于统计指定元素出现的次数  thislist.count("apple")
index(): #用于查看指定元素的下标   thislist.index("apple")
append():#用于在列表末尾添加指定的列表项  thislist.append("orange")
extend(): #用于在列表末尾迭代添加列表项,相当于拼接列表。该位置可以迭代任何可迭代对象,如:字符串,字典,数组等。       thislist.extend(["121","212"])
reverse(): #用于反转列表中的列表项  thislist.reverse()
copy():   #复制列表,  newlist=thislist.copy()
list():  #复制列表,list()是内建方法  newlist=list(thislist)
insert():  #在指定的下标插入指定的列表项。thislizt.insert(1,"orange")
pop():   #用于删除指定下标的列表项 thislist.pop(1)
remove(): #用于从列表中移除指定元素,如果有列表中有多个指定元素,则移除第一个  thislist.remove("orange")
clear(): #清空列表   thislist.clear()
del():  #完整的删除列表,包括定义。  del thislist
## 

记:复制操作,如果用list2=list1进行复制的话,list2只是对list1的引用,当list1发生更改时,list2也会发生更改。

以上方法具体操作代码如下:

thislist = ["apple", "apple","banana", "cherry"]
print(len(thislist))         #4
print(thislist.count("apple"))    #2
print(thislist.index("apple"))    #0
thislist.append("orange")    
print(thislist)        #['apple', 'apple', 'banana', 'cherry', 'orange']
thislist.extend(["banana","212"])  
print(thislist)       #['apple', 'apple', 'banana', 'cherry', 'orange', 'banana', '212']
thislist.reverse()
print(thislist)       #['212', 'banana', 'orange', 'cherry', 'banana', 'apple', 'apple']
newlist = thislist.copy()
print(newlist)        #['212', 'banana', 'orange', 'cherry', 'banana', 'apple', 'apple']
newlist = list(thislist)
print(newlist)        #['212', 'banana', 'orange', 'cherry', 'banana', 'apple', 'apple']
thislist.insert(1, "orange")    
print(thislist)#['212', 'orange', 'banana', 'orange', 'cherry', 'banana', 'apple', 'apple']
thislist.pop(2)
print(thislist)      #['212', 'orange', 'orange', 'cherry', 'banana', 'apple', 'apple']
thislist.remove("orange")
print(thislist)      #['212', 'orange', 'cherry', 'banana', 'apple', 'apple']
thislist.clear()
print(thislist)     #[]
del thislist

1.5 合并两个列表(8种方法)

在python中合并列表中的方法有多个。最简单的是使用“+”运算符合并两个列表

1.使用“+”运算符合并

2.循环使用append()方法合并

3.使用extend()方法合并

4.使用切片方法

5.使用*号合并列表,将列表解压并合到一起

6.使用chain函数进行合并。itertools模块中的chain函数是Python中合并迭代对象的一种特殊方法。它可以对一系列的迭代对象进行分组,并在返回组合后的迭代项。不仅可以合并列表,其余的迭代对象也可以合并。

7.reduce()函数合并列表

8.list列表中函数add方法

切记:append()、extend()和切片方法都是在原来的list1列表中追加元素,所以输出是还是输出的list1。

import itertools
from operator import add
from functools import reduce

list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
#1.使用“+”运算符合并
list3 = list1 + list2
print(list3)
#2.循环使用append()方法合并
for x in list2:
    list1.append(x)
print(list1)
#3.使用extend()方法合并
list1.extend(list2)
print(list1)    
#4.使用切片方法
list1[0:0] = list2
print(list1)
#5.使用*号合并列表(*在python中又名asterisks)
list5 = [*list1,*list2]
print(list5)
#6.使用itertools模块下的chain函数进行合并。
list6 = list(itertools.chain(list1, list2))
print(list6)
#7.reduce()函数合并列表
list7 = reduce(add,(list1,list2))
print(list7)
#list列表中函数add方法
list1.__add__(list2)
print(list1)

2.元组tuple

​ 元组是有序不可更改的集合。

2.1 元组的定义

​ 元组中的元素有序且不可更改,允许成员重复。这里指的是不可在元组类型中更改其值和长度。在 Python 中,元组用圆括号编写,元素可以为任意数据类型。

**创建元组:**定义元组有两种方式

​ 方法一:thistuple = (“apple”, 200, “cherry”,“orange”,“Kiwi”,“melon”,“mango”)

​ 方法二:thistuple = tuple([“apple”, 200, “cherry”,“orange”,“Kiwi”,“melon”,“mango”]) tuple()内用方括号表示
如果元组中仅有一个元素,则要在元素后加上逗号。如thistuple = (“apple”, )

**索引:**可用通过方括号内的索引号来访问元组元素,下标0为第一个元素。正索引为从左侧头开始。负索引从右侧末尾开始,-1表示倒数 第一个元素。

索引范围:还可以通过指定起点和终点来指定索引范围,返回带有指定范围元素的新元组。索引号范围遵循左闭右开原则。

thistuple = tuple(["1","2"])
thistuple = ("apple", 200, "cherry","orange","Kiwi","melon","mango")
print(thistuple)
print(thistuple[1])
print(thistuple[-1])
print(thistuple[2:5])
print(thistuple[-4:-1])
#----output-----
# ('apple', 200, 'cherry', 'orange', 'Kiwi', 'melon', 'mango')
# 200
# mango
# ('cherry', 'orange', 'Kiwi')
# ('orange', 'Kiwi', 'melon')

2.2 访问元组项

查看元组中的元素有两种方法:

1.使用索引号来获取指定下标的元素。如:thistuple[1]

2.通过for循环遍历列表查看元组中的元素。

thistuple = ("apple", "banana", "cherry")
for x in thistuple:
     print(x)

还可以用“in”关键字检查元组是否在元组中。使用方法和列表相同。

if "apple" in thistuple:
    print("Yes, 'apple' is in the fruits tuple")

2.3 “更改”元组项(借助列表变相更改元组)

​ 元组一旦创建,无法更改其元组中添加项目,元组是不可改变的

​ 虽然不可以更改元组的元组项,但是列表可以更改其列表项,我们可以将元组转化为列表,然后通多列表更改列表项的方法进行更改。达到更改元组项的目的。

​ 通过列表和元组之间转换更改元组值的方法和列表更改列表项的方法一样。只不过是加了转换语句。

list(元组)可以将元组转为列表; tuple(列表)可以将列表转为元组;

1.通过索引更改

thistuple = ("apple","banana","cherry")
thislist = list(thistuple)
thislist[1] = "Kiwi"
thistuple = tuple(thislist)
print(thistuple)     #('apple', 'Kiwi', 'cherry')

2.通过切片更改,切片法的使用方式和列表中切片法的使用方式如出一辙。

thistuple = ("apple","banana","cherry")
thislist = list(thistuple)
thislist[0:2] = ["Kiwi","Tim"]
thistuple = tuple(thislist)
print(thistuple)     #('Kiwi', 'Tim', 'cherry')

2.4 常用的方法

count()返回元组中指定值出现的次数。
index()在元组中搜索指定的值并返回它被找到的第一个位置。
len()求元组长度
del删除元组
thistuple = tuple(("apple", "apple","banana", "cherry")) # 请注意双括号
print(thistuple.count("apple"))     #2 
print(thistuple.index("apple"))     #0
print(len(thistuple))               #4
del thistuple

2.5 合并两个元组

如果想要链接两个元组可以使用“+”运算符。因为元组是不可被更改的,所以需要将合并语句赋值给一个新的元组。

也可以用过将元组转换为列表,进行列表中的方式进行合并,但是不如直接使用加号运算符简单。

1.使用“+”运算符合并元组

2.使用*合并元组

tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
#1.使用“+”运算符合并元组
tuple3 = tuple1 + tuple2
print(tuple3)    #('a', 'b', 'c', 1, 2, 3)
#2.使用*合并元组
tuple4 = (*tuple1,*tuple2)
print(tuple4)

3.range自定义数字序列

range数据类型表示不可变的数字序列。通常在for循环中循环指定的次数

3.1 range定义

class range(start,stop,step);一定范围不可变的数字序列;

startt参数,默认为0;

step参数,默认为1;如果step为0则会引发ValueError错误。如果step为正表示正索引,从小到大。为负则表示负索引,从大到小。

stop参数,数字范围不包含stop本身;

list1 = list(range(10))
print(list1)   #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list1 = list(range(0,10,2))
print(list1)   #[0, 2, 4, 6, 8]

3.2 range作为循环次数

range所定义的范围是一个左边右开的范围。顾头不顾尾。

for i in range(10):
    print(i)
#----output----
0
1
2
3
4
5
6
7
8
9
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值