【超简单】python第六章教学

这一章开始就要到python的几个最主要的储存数据的容器了,这一章呢就先来列表与元组。

六.列表与元组

1.列表的操作

(1).元素赋值

x = [1,2,3]
x[0] = 10
print(x)

[10,2,3]

------------------------------

注意:当索引编号不存在时,会包出IndexError的错误。

(2).元素删除

list  = ['y','y','x',123]

del list[1]
print[list]

['y','x',123]

当然:用del list 也可以直接删除列表

(3).分片赋值

(1)替换赋值

name = list('Perl')
name[2:] = list('ar')
print(name)

['P','e','a','r']
name = list('Perl')
name[1:] = list('ython')
print(name)

['P','y','t','h','o','n']

(2)插入赋值

number= [1,5]
number[1:1] = [2,3,4]
print(number)

[1,2,3,4,5]

(3)删除

number = [1,2,3,4,5]
number[1:4] = []
print(number)

[1,5]

(4)解析列表

list = [(x,y) for x in range(5) if x % 2==0 for y in range(5) if y % 2==0]
print(list)

rect = [[1,2,3],[3,4,5]]
r = [row[1] for row in rect]
print(r)
print(rect[1])

----------------------------------------------------------

[(0, 0), (0, 2), (0, 4), (2, 0), (2, 2), (2, 4), (4, 0), (4, 2), (4, 4)]
[2, 4]
[3, 4, 5]

 (5)下面是列表的常用方法:

  • s.count(x):返回x在列表中出现的次数
  • s.index(x):返回x在s中第一次出现的下标
  • s.append(x):在列表s的末尾附加x元素
  • s.extend(s1):在列表s的末尾添加列表s1的所有元素
  • s.sort():对列表s中的元素排序
  • s.reverse():将列表s中的元素逆序排序
  • s.pop([i]):删除并返回列表s中指定位置i的元素,默认是最后一个元素
  • s.insert(i,x):在列表s的i位置处插入x,如果i大于列表长度,则插入到列表最后

s.remove(x):从列表s中删除x,若x不存在,则报错

list1 = ['y','y','x',123,456,123,123]
list2 = ['1','2',3]
list3 = [2,4,2,3,1]

print(list1.count(123))

print(list1.index(123))

list1.append('13')
print(list1)

list1.extend(list2)
print(list1)

list3.sort()
print(list3)

list3.reverse()
print(list3)

print(list1.pop(2))

list1.insert(10,'yyx')
print(list1)

list1.remove(123)
print(list1)

------------------------------------------------


3
3
['y', 'y', 'x', 123, 456, 123, 123, '13']
['y', 'y', 'x', 123, 456, 123, 123, '13', '1', '2', 3]
[1, 2, 2, 3, 4]
[4, 3, 2, 2, 1]
x
['y', 'y', 123, 456, 123, 123, '13', '1', '2', 3, 'yyx']
['y', 'y', 456, 123, 123, '13', '1', '2', 3, 'yyx']

2.元组

元组与列表的区别:

(1)元组是不可变的序列类型,元组能对不需要改变的数据进行写保护,使数据更安全。列表是可变的序列类型,可以添加、删除或搜索列表中的元素。

(2)元组使用小括号定义用逗号分隔的元素,而列表中的元素应该包括在中括号中。虽然元组使用小括号,但访问元组元素时,要使用中括号按索引或分片来获得对应元素的值。

(3)元组可以在字典中作为关键字使用,而列表不能作为字典关键字使用,因为列表不是不可改变的。

(4)只要不尝试修改元组,那么大多数情况下把他们作为列表来进行操作。

列表与元组的转换:

tup = ([1,2],3,4)

lst = list(tup)
lst[0] = [10,20]
print(lst)

tup = tuple(lst)
print(tup)

------------------------------------

[[10, 20], 3, 4]
([10, 20], 3, 4)

3.序列的应用

1.数据排序:

(1)冒泡排序

def bubble_sort(alist):
    for passnum in range(len(alist) - 1,0,-1):
        for i in range(passnum):
            if alist[i] > alist[i + 1]:
                alist[i],alist[i + 1] = alist[i + 1],alist[i]
    return alist

(2)希尔排序

def shellSort(alist):
    sublistcount = len(alist) // 2
    while sublistcount > 0:
        for startpoint in range(sublistcount):
            InsertSort(alist,startpoint,sublistcount)
    
    sublistcount = sublistcount // 2

    return alist    

def InsertSort(alist,start,gap):
    for index in range(start + gap,len(alist),gap):
        currentvalue = alist[index]
        position = index

        while position > gap and alist[i + 1] > currentvalue:
            alist[position] = alist[position - gap]
            position = position - gap

        
        alist[position] = currentvalue

2.数据查找:

(1)顺序查找

def Search(alist,item):
    pos = 0
    found = False
    
    while pos < len(alist) and not found:
        if alist[pos] = item:
            found = True
        else:
            pos = pos + 1

    return found

(2)二分法查找

def binarySearch(alist,item):
    if len(alist) == 0:
        return False
    else:
        midpoint = len(alist) // 2
        if alist[midpoint] == item:
            return Ture
        else:
            if alist[midpoint] < item:
                return binarySearch(alist[midpoint:],item)
            else:
                return binarySearch(alist[:midpoint],item)

3.矩阵运算

(1)矩阵加法 A 是 mxn B 是 mxn 试求 C = A + B

A = [[2,1],[3,5],[1,4]]
B = [[3,2],[1,4],[2,4]]

C = [[0] * len(A[0]) for i in range(len(A))]

for i in range(len(A)):
    for j in range(len(A[0])):
        C[i][j] = A[i][j] + B[i][j]

(2)矩阵乘法 A 是 mxn B 是 nxp 试求 C = A x B

A = [[2,1],[3,5],[1,4]]
B = [[3,2,1,4],[0,7,2,6]]

C = [[0]*len(B[0]) for i in range(len(A))]

k = 0
for i in range(len(A)):
    for j in range(len(B[0])):
        C[i][j] = A[i][k] * B[k][j] + A[i][k + 1] * B[k + 1][j]

好了,这就是本章的全部内容了。。。


版权声明:本文为weixin_51365324原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。