美文网首页Python四期爬虫作业
【Python爬虫】-20170813-列表、元组、集合练习题

【Python爬虫】-20170813-列表、元组、集合练习题

作者: 红小路 | 来源:发表于2017-08-16 13:57 被阅读0次

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

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

#    完成以下几个要求:

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

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

# 1)输出python及其下标

print(list2[2])

number = list2.index('python')

print('下标为:', number)

# 2)在list2后追加 '!' , 在 'short' 后添加 ','   #showing none..

print(list2.append('!'))

print(list1.insert(3, ','))

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

list1.extend(list2)

list1.sort()

print(list1)

print(len(list1))

# 4)将 'python' 改为 'python3'

list2.remove('python')

list2.insert(2, 'python3')

print(list2)

#or list2[2]="python"

# print(list2)

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

list2.remove('!')

list1.remove(',')

print(list1)

print(list2)

#

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

week_tuple=[1,2,3]

print(len(week_tuple))

print(week_tuple[1])

# 三、定义列表:

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

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

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

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

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

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

#    完成以下操作:

# 1)构造集合 list_set1

list_set1 = set(list1)

print(list_set1)

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

list1.extend(list2)

list_set2 = set(list1)

# 3)输出两个集合的长度

print(len(list_set1))

print(len(list_set2))

# 4)将两个集合list_set1、list_set2合并后移除 'python'

list_set1.update(list_set2)

list_set1.remove('python')

print(list_set1)

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

list_set1.add('python3')

print(list_set1)

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

#Output:

python

下标为: 2

None

None

['!', ',', 'is', 'life', 'need', 'python', 'short', 'you']

8

['you', 'need', 'python3', '!']

['!', 'is', 'life', 'need', 'python', 'short', 'you']

['you', 'need', 'python3']

3

2

{'is', 'life', 'short'}

3

6

{'is', 'short', 'life', 'need', 'you'}

{'is', 'python3', 'short', 'life', 'need', 'you'}

相关文章

网友评论

    本文标题:【Python爬虫】-20170813-列表、元组、集合练习题

    本文链接:https://www.haomeiwen.com/subject/rzsirxtx.html