practice_5(列表函数)
【摘要】 append()和extend()和insert()
a = [1,2,3,4,5]
a.append([6,7,8])
print(a)
b = [1,2,3,4,5]
b.extend([6,7,8])
print(b)
c = [1,2,3,4,5]
c.insert(3,10)
print(c)
1234567891011
[1, 2, 3, 4, 5...
append()和extend()和insert()
a = [1,2,3,4,5]
a.append([6,7,8])
print(a)
b = [1,2,3,4,5]
b.extend([6,7,8])
print(b)
c = [1,2,3,4,5]
c.insert(3,10)
print(c)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
[1, 2, 3, 4, 5, [6, 7, 8]]
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 10, 4, 5]
- 1
- 2
- 3
假定给出一个列表 member = [“图灵”, “的”, “周老师”, “是最帅的”]
想要把member变成 member = [“图灵”, 99, “的”,88, “周老师”,77, “是最帅的”, 666]应该怎么做?
member = ["图灵", "的", "周老师", "是最帅的"]
member.insert(1,99)
member.insert(3,88)
member.insert(5,77)
member.append(666)
print(member)
- 1
- 2
- 3
- 4
- 5
- 6
['图灵', 99, '的', 88, '周老师', 77, '是最帅的', 666]
- 1
请问如何将下边这个列表的"图灵学院"修改为"周老师"?
ls = [1, [1, 2, ['图灵学院']], 3, 5, 8, 13, 18]
- 1
ls = [1, [1, 2, ['图灵学院']], 3, 5, 8, 13, 18]
ls[1][2][0] = "周老师"
print(ls)
- 1
- 2
- 3
- 4
[1, [1, 2, ['周老师']], 3, 5, 8, 13, 18]
- 1
将列表推导式还原出来
ls = [(x, y) for x in range(10) for y in range(10) if x%2 == 0 if y %2 == 0]
- 1
ls = [(x, y) for x in range(10) for y in range(10) if x%2 == 0 if y %2 == 0]
print(ls)
- 1
- 2
[(0, 0), (0, 2), (0, 4), (0, 6), (0, 8), (2, 0), (2, 2), (2, 4), (2, 6), (2, 8), (4, 0), (4, 2), (4, 4), (4, 6), (4, 8), (6, 0), (6, 2), (6, 4), (6, 6), (6, 8), (8, 0), (8, 2), (8, 4), (8, 6), (8, 8)]
- 1
ls = []
for x in range(10): for y in range(10): if x%2 == 0 and y %2 == 0: ls.append((x,y)) print(ls)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
[(0, 0), (0, 2), (0, 4), (0, 6), (0, 8), (2, 0), (2, 2), (2, 4), (2, 6), (2, 8), (4, 0), (4, 2), (4, 4), (4, 6), (4, 8), (6, 0), (6, 2), (6, 4), (6, 6), (6, 8), (8, 0), (8, 2), (8, 4), (8, 6), (8, 8)]
- 1
文章来源: ruochen.blog.csdn.net,作者:若尘,版权归原作者所有,如需转载,请联系作者。
原文链接:ruochen.blog.csdn.net/article/details/90299641
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)