day07列表的相关运算和方法

1. 列表相关运算

1.1 数学运算: +, *

列表1+列表2 - 将两个列表中的元素合并产生一个新的列表(原列表不会发生改变)
列表*N - 列表中的元素重复N次产生新的列表

list1 = [1, 2, 3]
list2 = ['name', 'age']
new_list = list1 + list2
print(new_list, list1, list2)
print(list1*3)
list3 = [1]*100
print(list3)

1.2 比较运算:

==, !=

list4 = [1, 2]
print([1, 2] == [2, 1])  # False
print([1, 2] != [2, 1])  # True
print([1, 2] == list4)   # True

>, <, >=, <= 只支持相同位置上元素类型相同的两个列表 (了解)

print([1, '2', 3] > [100, 'z'])
# print([1, 2, 3] > ['a', 'b'])   #TypeError: '>' not supported between instances of 'int' and 'str'

2.内置函数

  • len(列表)、sum(列表) 要求列表中的元素必须是数字、
  • max(列表)、min(列表)
  • list(数据) - 将指定数据转换成列表, 所有的序列都可以转换成列表,比如: 字符串,列表、字典、集合、元祖、range、迭代器、生成器等。
    转换的时候是将序列中的元素作为列表元素
# print(sum(['1', 'abc', 'hj']))
# print(max([1, 'a']))

print(list('abcd'))
print(list(range(5)))
print(list(str(123)))

nums = ['10', '23', '4']   # 10234
print(''.join(nums))

3.相关方法

3.1 列表.count(元素) - 获取指定元素在列表中出现的次数, 结果是整数

print([1, 2, 3, 1, 4].count(1))

3.2 列表.extend(序列) - 将序列中的元素添加到列表中, 结果None

list1 = []
list1.extend('abc')
print(list1)
list1.extend(range(3))
print(list1)
list1.extend(['张飞', '关羽', '赵云'])
print(list1)

list1.extend(['吕布'])
print(list1)

3.3 列表.index(元素) - 获取指定元素在列表中的下标,如果这个元素有多个只获取第一个,如果元素不存在会报错

list2 = [1, 0, 1, 2, '张飞', '关羽', '赵云', '吕布']
print(list2.index('张飞'))
# print(list2.index('诸葛亮'))    # ValueError: '诸葛亮' is not in list

3.4 列表.reverse() - 将列表中的元素倒序, 不会产生新列表

list2.reverse()
print(list2)

补充: 内置函数: reversed(序列) - 将序列中的元素倒序产生一个新的迭代器, 原序列不会修改

list1 = [1, 2, 3]
result = reversed('abc')
# for item in result:
#     print(item)
print(list(result))
print(list1)

3.5 列表.sort() - 对列表中的元素从小到大排序(列表中的元素类型一致并且支持比较运算), 不会产生新的列表

列表.sort(reverse=True) - 从大到小排序

scores = [10, 100, 89, 20, 67, 34, 9]
# scores.sort()
# print(scores)      # python cookbook/ 流畅的python

scores.sort(reverse=True)
print(scores)

3.6 列表.copy() - 产生一个新的列表,列表中的元素和原列表一样,相当于: 列表[:]

list1 = [1, 2, 3]
list2 = list1
list3 = list1.copy()
print(list2, list3)

list1.append(10)
print(list2, list3)

3.7 id(变量) - 查看变量中实质存储的地址

python所有变量实质都是直接保存的数据在内存中的地址

print(id(list1), id(list2), id(list3))

4. 列表中删除数据

删除列表中所有小于60的数字

4.1 问题1: 通过元素删除的时候可能出现删不干净的问题**

scores = [10, 50, 90, 89, 45, 70]
for score in scores:
    if score < 60:
        scores.remove(score)

print(scores, score)    # [50, 90, 89, 20, 70]
"""
scores = [10, 50, 90, 89, 45, 70]
score = 10   if 10 < 60    scores.remove(10)   scores = [50, 90, 89, 45, 70]  
score = 90   if 90 < 60
score = 89   if 89 < 60
score = 45   if 45 < 60    scores.remove(45)   scores = [50, 90, 89, 70] 
"""

解决问题1:

scores = [10, 50, 90, 89, 45, 70]
scores2 = scores[:]
for score in scores2:
    if score < 60:
        scores.remove(score)
del scores2    # score2只是提供遍历用的,用完后没有其他用处,可以直接删除
print(scores, score)

上面的简写

scores = [10, 50, 90, 89, 45, 70]
for score in scores[:]:
    if score < 60:
        scores.remove(score)
print(scores, score)

"""
程序过程:
scores = [10, 50, 90, 89, 45, 70]
scores2 = [10, 50, 90, 89, 45, 70]
score = 10   if 10<60  scores.remove(10)   scores = [50, 90, 89, 45, 70]
score = 50   if 50<60  scores.remove(50)   scores = [90, 89, 45, 70]
score = 90   if 90<60
score = 89   if 89<60
score = 45   if 45<60  scores.remove(45)   scores = [90, 89,70]
score = 70   if 70<60
"""

4.2 问题2:通过下标删除满足要求的元素的时候,出现下标越界的错误

scores = [10, 50, 90, 89, 45, 70]
for index in range(len(scores)):
     if scores[index] < 60:
         del scores[index]

print(scores)

"""
scores = [10, 50, 90, 89, 45, 70]
for index in range(6)  
index = range(6)  index = (0,1,2,3,4,5)
index = 0   if scores[0]<60   if 10<60   del scores[0]  scores = [50, 90, 89, 45, 70]
index = 1   if scores[1]<60   if 90<60   
index = 2   if scores[2]<60   if 89<60
index = 3   if scores[3]<60   if 45<60   del scores[3]  scores = [50, 90, 89, 70]
index = 4   if scores[4]<60  (out of range)
"""

解决问题2:

scores = [10, 50, 90, 89, 45, 70]
index = 0
while index < len(scores):
    if scores[index] < 60:
        del scores[index]
        continue
    index += 1

print(scores)
"""
scores = [10, 50, 90, 89, 45, 70]   
index = 0
while 0 < 6    if 10<60   del scores[0]   scores = [50, 90, 89, 45, 70] 
while 0 < 5    if 50<60   del scores[0]   scores = [90, 89, 45, 70]
while 0 < 4    if 90<60   index += 1  index = 1
while 1 < 4    if 89<60   index += 1  index = 2
while 2 < 4    if 45<60   del scores[2]   scores = [90, 89,70]
while 2 < 3    if 70<60   index += 1  index = 3  
while 3 < 3 
print(scores)  -  [90, 89,70] 
"""
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 225,641评论 6 525
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 96,754评论 3 408
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 173,075评论 0 370
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 61,369评论 1 303
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 70,386评论 6 402
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 53,800评论 1 317
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 42,122评论 3 431
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 41,130评论 0 281
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 47,689评论 1 327
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 39,693评论 3 348
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,804评论 1 356
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 37,399评论 5 351
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 43,128评论 3 341
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 33,528评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,705评论 1 278
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 50,409评论 3 383
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,878评论 2 368

推荐阅读更多精彩内容