一起零基础学Python

列表是由一系列按特定顺序排列的元素组成。Python中是由中括号( [ ] )表示,里面的元素用逗号隔开。

因为列表是有序的,所以可以使用索引

1、列表索引

list1 = ['you','are','my','just','and','only','one','!']
list2 = ['11',3,4,'66',00]
#类似于字符串,列表的排序编号从左到右以 0 开始,从右往左以 -1 开始。
#左闭右开
print(list[0])
print(list[1:3])
print(list[0:7:2])
print("-------------------------------------")
print(list[-1])
print(list[0:-1])
print(list[3:-2])
print(list[0:-1:2])
print("-------------------------------------")
print(list2)
print(list2[0])
you
['are', 'my']
['you', 'my', 'and', 'one']
-------------------------------------
!
['you', 'are', 'my', 'just', 'and', 'only', 'one']
['just', 'and', 'only']
['you', 'my', 'and', 'one']
-------------------------------------
['11', 3, 4, '66', 0]
11

Process finished with exit code 0

2、列表元素替换

list1 = ['you','are','my','just','and','only','one','!']
print(list1[0])
print("元素替换后:")
list1[0] = "your"
print(list1[0])
print(list1)
you
元素替换后:
your
['your', 'are', 'my', 'just', 'and', 'only', 'one', '!']

Process finished with exit code 0

3、向列表添加元素

append()、extend()、insert()  这三种方法均没有返回值,是直接修改了原数据对象。
list = ['a','b','c','d']
print(list)

#使用方法append()添加元素,每次只能在末尾处添加1个元素
list.append('e')
print(list)

#使用方法extend()添加元素:将 一个列表 中每个元素 分别 添加到 另一个列表 中,只接受一个参数。
list2 = [1,2,3,4,5]
list.extend(list2)
print(list)

#使用方法insert() 添加元素,将指定元素 插入到 指定位置。
list.insert(1,'G')
print(list)

#将两个list相加,需要创建新的list对象,需要消耗额外的内存。因此,当list较大时,尽量不要使用“+”来添加list,而应该尽可能使用List的append()方法。
x = [1,2,3,4,5]
y = ['a','b','c','d']
lis3 = x + y
print(lis3)
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd', 'e']
['a', 'b', 'c', 'd', 'e', 1, 2, 3, 4, 5]
['a', 'G', 'b', 'c', 'd', 'e', 1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 'a', 'b', 'c', 'd']

Process finished with exit code 0

4、将一个列表复制到另外一个列表

法一:元素逐个复制,法二:循环+range函数

#法一:将list1元素逐个复制给list2
list1 = [1,2,3,4,5]
list2 = list1[0:]
print(list2)

#法二:使用for i in 循环+range()函数
#range()函数语法:range(start, stop [,step]) ;start 指的是计数起始值,默认是 0;stop 指的是计数结束值,但不包括 stop ;step 是步长,默认为 1,不可以为 0 。
#range() 方法生成一段左闭右开的整数范围。
list3 = []
for i in range(5):
    list3.append(list1[i])
print(list3)
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]

Process finished with exit code 0

5、列表元素删除

remove、pop、clear、del (语句)

#方法remove可以删除列表内指定元素值。(无返回值)
list = ['1','2',3,4,5,6]
list.remove(6)
print(list)

#方法pop可以删除指定下标的元素值,默认为末尾元素值;
#pop()删除元素值后,并返回已删除的元素值。
list2 = ['a','b','c','d','e','f']
print(list2.pop(1))  #打印返回值
print(list2)

#方法clear可清除列表内的所有元素值
list3 = ['a','b','c','d','e','f']
list3.clear()
print(list3)

# del语句 可以删除整个列表,或者进行切片式删除
list4 = ['a','b','c','d','e','f']
#切片式删除
del list4[0:3]
print(list4)
#删除整个列表
del list4

['1', '2', 3, 4, 5]
b
['a', 'c', 'd', 'e', 'f']
[]
['d', 'e', 'f']

Process finished with exit code 0

ps:  pop()方法应用场景一

#pop()方法应用一:
#若列表元素值是按照时间顺序存储的,想输出最新的元素值:
list = ['张三','李四','王五','小明','mariya']
x = list.pop()
print(f"Welcome {x.title()} to join us !")
print(list)   #注:原列表中的值将被弹出
Welcome Mariya to join us !
['张三', '李四', '王五', '小明']

Process finished with exit code 0

6、列表元素倒叙

reverse、倒叙遍历


#方法reverse可以将列表中的元素倒叙,且在原列表上进行操作,不返回新的列表。
list1 = ['a','b','b','d','e','f']
list1.reverse()
print(list1)
print(list1.reverse())

#倒叙遍历也可实现列表倒叙
list2 = [1,2,3,4,5,6,7]
x = list2[-1::-1]
print(x)
['f', 'e', 'd', 'b', 'b', 'a']
None
[7, 6, 5, 4, 3, 2, 1]

Process finished with exit code 0

7、list方法将字符串、序列转化为列表

注:如果定义了list这个变量,再用 list()函数时会报错。

#方法list将字符串转化为列表
x = ("abcdef",'123')
y = ("qazxsw")

#方法list将序列转化为列表
z = "plmkoi"

print(list(x))
print(list(y))
print(list(z))
['abcdef', '123']
['q', 'a', 'z', 'x', 's', 'w']
['p', 'l', 'm', 'k', 'o', 'i']

Process finished with exit code 0

8、确定列表长度

len

#使用方法len,计算列表长度
list2 = ['a','b','c','d','e','f']
x = len(list2)
print(x)
6

Process finished with exit code 0

9、列表元素排序

sort()、sorted()

注:1、sort()是方法,sorted()是函数;

        2、sort()是永久改变列表顺序,sorted()是临时改变列表顺序。

#方法sort()实现列表元素按照字母顺序排序
#注:sort方法是永久的改变原始列表顺序
list = ['one','two','three','four','five']
list.sort()
print(list)

#向sort传递参数(reverse=True),实现与字母表相反的顺序排序。
list.sort(reverse=True)
print(list)

print("----------------------------------------")

#函数sorted()实现元素临时排序,不影响原始列表元素顺序
list2 = ['one','two','three','four','five']
print(sorted(list2))
print(list2)

#向sorted传递参数(reverse=True),实现与字母表相反的顺序排序。
print(sorted(list2,reverse=True))
['five', 'four', 'one', 'three', 'two']
['two', 'three', 'one', 'four', 'five']
----------------------------------------
['five', 'four', 'one', 'three', 'two']
['one', 'two', 'three', 'four', 'five']
['two', 'three', 'one', 'four', 'five']

Process finished with exit code 0

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值