Python study notes 2: list, tuple and set related content

1.list list

  • A sequence of ordered data
    • The data is in order
    • Data may not be a type of data
  • Creation of list:
    • Create directly, create with brackets, and separate the content with a comma
    • Create with list
L1 = [1,2,3,4,5]
# list内的数据可以不是一类数据
L2 = [1,2,3,"amy","xiaojing"]
# 创建列表的第二种方式
L3 = list()
print(L3)
print(  type(L3)  )

[]
<class ‘list’>

1.1 The concept of built-in functions

  • help: help function
  • type: display the type of the variable
  • id: Display the id of the variable
  • print: print content
# list创建的特例演示

s = "amyniez"

# 创建一个只包含s一个字符串的列表
L1 = list(s)
print(type(L1))
print(L1)

<class ‘list’>
[‘a’, ‘m’, ‘y’, ‘n’, ‘i’, ‘e’, ‘z’]

1.2 Common operations for lists

  • access
    • Use subscript operations, also called indexes
    • The element index of the list starts from 0
  • Slice operation
L1 = [32,43,22,354,123,75]

# 使用下标访问
print(L1[0])

32

# 切片操作需要注意:范围为左包括,右不包括
L1 = [10,20,30,40,50,60,70,80,90,100]
# 对比打印结果跟下标的值
print(L1[1:6])

# 下面结果说明切片后生成的是一个全新的列表
# 通过内置函数id可以判断出切片是否生成全新了的列表
# id的作用是用来判断两个变量是否是同一个变量
L2 = L1[0:10]
print(id(L1))
print(id(L2))

# 切片的下标可以为空
print(L1[:4])
print(L1[4:])
print(L1[:])

[20, 30, 40, 50, 60]

2204434043912
2204434044104

[10, 20, 30, 40]
[50, 60, 70, 80, 90, 100]
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

# 分片可以控制增长幅度,默认增长幅度为1
print(L1[::1]) #等于print(L1[:])
print(L1[::2])

[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
[10, 30, 50, 70, 90]

# 下标可以超出范围,超出范围后不再考虑多余下标内容
print(L1[:100])

[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

# 下标值,可以为负数,表示从右向左数
# 规定: 数组最后一个数字的下标为 -1

# 下面例子为空, 因为默认是从左向右移动
print(L1[-2:-5])

# 如果想利用负数下标打印90,80,70,则
print(L1[-2:-5:-1])

# 如果想利用负数下标打印70,80,90,则
print(L1[-4:-1:1])

[]

[90, 80, 70]

[70, 80, 90]

2.tuple (tuple)

  • Can be understood as a list that is not allowed to be changed
# tuple的创建

# 1.直接用小括号
ta = ()
print(type(ta))

# 当用小括号创建一个元素的tuple时
tb = (100)
print(type(tb))# 这是一个特例
tc = (100,)
print(type(tc))
td = (100, 200, 300, 400)
print(td)

# 2.直接用逗号(逗号后面其实有一个空格的)
ta = 6, 
print(type(ta))
tb = 6, 7, 8, 9, 
print(tb)

# 3.使用tuple定义
ta = tuple()
print(ta)

li = [1, 2, 3, 4, "xiaojing"]
tb = tuple(li) # 要求tuple参数必须可迭代
print(tb)
print(li)

<class ‘tuple’>

<class ‘int’>
<class ‘tuple’>
(100, 200, 300, 400)

<class ‘tuple’>
(6, 7, 8, 9)

()

(1, 2, 3, 4,'xiaojing')
[1, 2, 3, 4,'xiaojing']

2.1 The rest of the tuple is basically the same as the list

  • Orderly
  • Can be accessed but cannot be modified
  • Elements can be of any type
# tuple索引操作
la = ["i", "love", "xiaojing"]
print(la)
ta = tuple(la)
print(ta[2])

[‘i’, ‘love’, ‘xiaojing’]
xiaojing

# tuple 分片操作
print(ta[:])
print(ta[:2])
print(ta[-1::-1])

(‘i’, ‘love’, ‘xiaojing’)
(‘i’, ‘love’)
(‘xiaojing’, ‘love’, ‘i’)

# 元组的相加
ta = 100, 200, 300, 
tb = ("i", "love", "xiaojing")
tc = ta + tb
print(tc)

(100, 200, 300, ‘i’, ‘love’, ‘xiaojing’)

# tuple 乘法

tc = tb * 2
print(tc)

(‘i’, ‘love’, ‘xiaojing’, ‘i’, ‘love’, ‘xiaojing’)

# tuple 成员检测
print(tb)

if "love" in tb:
    print("yes")
else:
    print("不爱了")

(‘i’, ‘love’, ‘xiaojing’)
yes

# 元组的遍历

for i in tb:
    print(i)

i
love
xiaojing

ta = ((10, 20, 30), ("i", "love", "you"),(2, 3, 4))
#嵌套元组的访问
#双层循环访问
for i in ta:
    print(i)
    for j in i:
        print(j)

# 2.使用单层循环
for i,j,k in ta:
    print(i,j,k)
    
#上面访问有一个特殊的规定:i,j,k要跟元组的元素个数相对应

(10, 20, 30)
10
20
30
(‘i’, ‘love’, ‘you’)
i
love
you
(2, 3, 4)
2
3
4
10 20 30
i love you
2 3 4

# 常用元组函数
ta = (1,34,5,23,2,1,6)
# len:长度
print(len(ta))
#max:最大值
print(max(ta))

tb = (1, 2, 3, "love")
print(max(tb))

7
34

# count : 对某元素计数
ta = (1,2,3,45,6,78,9,9,9,9)
print(ta.count(9))

#index: 某元素所在位置
print(ta.index(1))

4
0

# tuple的特殊用法
a = 100
b = " for you "
#要求对a,b值进行互换
#此用法只有python有
print(a, b)
a, b = b, a
print(a, b)

3.set (collection)

  • Consistent with the concept of sets in mathematics
  • Unordered content + non-repetitive content
# 集合的定义

# 1.通过set关键字
sa = set()
print(sa)

li = [1,2,3,4,5,6,7,8,9,12,12,5,2,6,7]
sb = set(li)
print(sb)

# 2.使用大括号
sc = {4,5,6,7,12,12,5,2,6,7}
print(sc)

set()
{1, 2, 3, 4, 5, 6, 7, 8, 9, 12}
{2, 4, 5, 6, 7, 12}

# in操作
if 2 in sc:
    print(222222)
    
if 10 in sc:
    print(444444)
    
for i in sc:
    print(i)

222222
2
4
5
6
7
12

# 集合的另一种遍历

sa = {(1,2,3), (4,5,6), ("a", "b", "c")}

for i,j,k in sa:
    print(i,j,k)

a b c
4 5 6
1 2 3

# 集合的生成式
sa = {1,2,3,4,5,6,7,8,9,10}

#利用sa生成一个sb
sb = {i for i in sa}
print(sb)

sc = {i for i in sa if i%2 == 0}
print(sc)

# 双重for循环
# 把sa中的每一个元素的平方生成一个新的集合
# 1. 用一个for
sd = {i**2 for i in sa}
print(sd)

#2.使用两个for
se = {m*n for m in sa for n in sa}
print(se)

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{2, 4, 6, 8, 10}
{64, 1, 4, 36, 100, 9, 16, 49, 81, 25}
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, 28, 30, 32, 35, 36, 40, 42, 45, 48, 49, 50, 54, 56, 60, 63, 64, 70, 72, 80, 81, 90, 100}

# 集合的内置函数
# len:长度
print(len(se))
# max/min:最值
# add:向集合中添加元素
sa = {1,2,3,4,5,6,5,4,3,2,1}
print(sa)

#打印结果不是sa?
print(sa.add(7))
print(sa)

#clear:清空

42
{1, 2, 3, 4, 5, 6}
None
{1, 2, 3, 4, 5, 6, 7

# 删除操作
# remove 和 discard的区别
sa = {1,2,3,4,5,6,5,4,3,2,1}
print(sa)
sa.remove(2)
print(sa)
# remove删除的值如果不在集合中,就会报错
sa.remove(2)

{1, 2, 3, 4, 5, 6}
{1, 3, 4, 5, 6}

# pop弹出集合的一个内容
# 删除的内容是随机的
# 删除的内容是随机的
sa = {1,2,3,4,5,6,7}
print(sa)
sa.pop()
print(sa)

{1, 2, 3, 4, 5, 6, 7}
{2, 3, 4, 5, 6, 7}

# 集合的数学操作(交叉并补)

# intersection:交集
sa = {1,2,3,4,5,6}
sb = {4,5,6,7,8,9}
# sa 和 sb 的交集
print(sa.intersection(sb))

# difference: 差集
print(sa.difference(sb)) 
# 差集的另一种表达方式:sa-sb
print(sa - sb)

# union:并集
print(sa.union(sb))
# 并集用 + 操作是不可以的

{4, 5, 6}
{1, 2, 3}
{1, 2, 3}
{1, 2, 3, 4, 5, 6, 7, 8, 9}

3.1 frozenset frozen set

  • Collection not allowed to be modified
#案例
print(sa)
sb = frozenset(sa)
print(sb)

{1, 2, 3, 4, 5, 6}
frozenset({1, 2, 3, 4, 5, 6})

Guess you like

Origin blog.csdn.net/amyniez/article/details/104344842