python集合例题_【Python爬虫】- 第4天列表、元组、集合练习题

一、定义列表:list1 = ['life','is','short'],

list2 = ['you','need','python']

完成以下几个要求:

1)输出python及其下标

list1 = ['life','is','short']

list2 = ['you','need','python']

print(list2.index('python'))

2)在list2后追加 '!' , 在 'short' 后添加 ','

list1 = ['life','is','short']

list2 = ['you','need','python']

n=list1.index('short')

list1.insert(n+1,',')

print(list1)

3)将两个字符串合并后,排序并输出其长度

list1 = ['life','is','short']

list2 = ['you','need','python']

list1.extend(list2)

print(len(list1))

print(list1,list1.sort())

4)将 'python' 改为 'python3'

list1 = ['life','is','short']

list2 = ['you','need','python']

list2[list2.index('python')]='python3'

print(list2)

4)移除之前添加的 '!' 和 ','

list1 = ['life','is','short',',']

list2 = ['you','need','python','!']

list1.remove(',')

list2.remove(list2[list2.index('!')])

print(list1,list2)

二、自己定义元组并练习常用方法(输出元组长度、指定位置元素等)

tup1 = (1,2,3,4,5,6,7)

tup2 = ('you', 'need', 'python')

print(tup1[0])

print(len(tup2))

三、定义列表:

list1 = ['life','is','short'],

list2 = ['you','need','python']

list3 = [1,2,3,4,5,3,4,2,1,5,7,9]

完成以下操作:

1)构造集合 list_set1

list1 = ['life', 'is', 'short']

list2 = ['you', 'need', 'python']

list3 = [1, 2, 3, 4, 5, 3, 4, 2, 1, 5, 7, 9]

list_set1=set(list1+list2+list3)

print(list_set1)

2)将list1和list2合并构造集合 list_set2

list1 = ['life', 'is', 'short']

list2 = ['you', 'need', 'python']

list3 = [1, 2, 3, 4, 5, 3, 4, 2, 1, 5, 7, 9]

list_set2=set(list1+list2)

print(list_set2)

3)输出两个集合的长度

list_set1=set(list1+list2+list3) list_set2=set(list1+list2) print(len(list_set1),len(list_set2))

4)将两个集合合并后移除 'python'

list_set1.update(list_set2)

list_set1.remove('python')

print(list_set1)

5)在合并后的新列表中添加 'python3'

list_set1.add('python3')

print(list_set1)

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值