代码改变世界

作业3

2018-09-21 13:57  cqchenqin  阅读(302)  评论(0编辑  收藏  举报
# 1.列表的遍历
list = ['Jackson','Yee','hunan','huaihua','shufa','你说']
for i in range(len(list)):
    print("编号: %s  名字:%s"% (i+1,list[i]))
# 定义 #
classmates = ['Michael', 'Bob', 'Tracy','李三' ,'Tracy','56']
new = ['Rose','Jack']
classmates.insert(2,'Tom')
classmates.append('Jack')
classmates.extend(new)
classmates.pop(1)
print(classmates)
print(classmates)
# 取元素或片段 #
classmates[1],classmates[-1], classmates[1:3]
print(classmates)
# 取属性len #
len(classmates),max(classmates), min(classmates)
print(classmates)
# 取元素的索引 #
classmates.index('Tracy')
print(classmates)
# 修改 #
classmates[1] = 'Sarah'
print(classmates)
# 排序 #
classmates.sort()
print(classmates) #没有返回值,改变列表本身
# 计数 #
classmates.count('Tracy')
print(classmates)

# 插入
classmates.insert(1, 'Jack')
print(classmates)
# 追加
classmates.append('Adam')
print(classmates)
# 扩展 list.extend(ls)
# 弹出
classmates.pop()
print(classmates)
# 删除
classmates.pop(1)
del classmates[-1]
print(classmates)

# 元组的遍历
t = ('a','b',['A','B'])
t [2][0] = 'X'
t [2][1] = 'Y'
for i in range(len(t)):
    print(t[i])

# 集合的遍历
s = {1,2,3}
print(s)
# 增加
s.add(4)
print(s)
# 删除
s.remove(4)
print(s)
# 生成
s = set({1,1,2,2,3,3})
print(s)
# 集合运算
s1 = set([1,2,3])
print(s1)
s2 = set([2,3,4])
print(s2)
s1 & s2
print(s1 & s2)
s1 | s2
print(s1 | s2)
s2 - s1
print(s2 - s1)
for i1 in s1:
    print(s1)
for i2 in s2:
    print(s2)

# 字典的遍历
dict1= {'Bob': 75, 'Michael': 95, 'Tracy': 85}
for key in dict1:
    print(dict1[key])
classmates= ['Michael', 'Bob', 'Tracy']
score = [95, 75, 85]
cs = dict(zip(classmates,score))
print(cs)
print(list('ababcd'),tuple('ababcd'))
d = {'Bob': 75, 'Michael': 95, 'Tracy': 85}
# 取值
d['Michael']
len(d)
# 判断key有否
'Thomas' in d
# 添加与修改
d['Rose'] = 88
# 删除
d.pop('Bob')
d.keys()
d.values()
d.items()
d={}

# 1.下载已收英文歌词或文章str
strYee = '''I know exactly what to do
You make these dreams come true
And you've been there through and through
So that's why I sing to you like
(Ah ah) I won't I won't I won't stop now
(Ah ah) I know I know I want that crown
I'm steady making these moves
I'm steady making these moves
I got nothing nothing nothing to lose
I'm steady making these moves
I'm steady making these moves
I got nothing nothing nothing to lose
My heart only beats for you
I got a lot of things to prove
I would travel to the moon
If you be there with me too like
(Ah ah) I won't I won't I won't stop now
(Ah ah) I know I know I want that crown
I'm steady making these moves
I'm steady making these moves
I got nothing nothing nothing to lose
I'm steady making these moves
I'm steady making these moves
I got nothing nothing nothing to lose
Oooooo
Shooting for the stars shooting for the stars
Oooooo
We made it this far we made it this far
(Ah ah) I won't I won't I won't stop now
(Ah ah) I know I know I want that crown
I'm steady making these moves
I'm steady making these moves
I got nothing nothing nothing to lose
I'm steady making these moves
I'm steady making these moves
I got nothing nothing nothing to lose
I'm steady making these moves
I'm steady making these moves
I got nothing nothing nothing to lose
I'm steady making these moves
I'm steady making these moves
I got nothing nothing nothing to lose'''
str.lower(strYee)
strList = strYee.split()aaa
print(strList)
# 排序
strList.sort()
print(strList)
# 2.分隔出一个一个的单词list
strList = strYee.split()
print(strList)
print(len(strList),strList)
# 3.统计每个单词出现的次数dict
strSet=set(strList)
print(len(strSet),strSet)
strDic={ }
for word in strSet:
    strDic[word]=strList.count(word)
print(len(strDic),strDic)

列表,元组,字典,集合的联系与区别

一、列表

1.任意对象的有序集合:列表是一组任意类型的值,按照一定顺序组合而成的

2.通过偏移读取 :组成列表的值叫做元素(Elements)。每一个元素被标识一个索引,第一个索引是0,序列的功能都能实现 

3.可变长度,异构以及任意嵌套 :列表中的元素可以是任意类型,甚至是列表类型,也就是说列表可以嵌套 

4.可变的序列 :支持索引、切片、合并、删除等等操作,它们都是在原处进行修改列表 

5.对象引用数组 :列表可以当成普通的数组,每当用到引用时,Python总是会将这个引用指向一个对象,所以程序只需处理对象的操作。当把一个对象赋给一个数据结构元素或变量名时,Pythoy总是会存储对象的引用,而不是对象的一个拷贝

二、元组

1.任意对象的有序集合:与列表相同 

2.通过偏移存取 :与列表相同 

3.属于不可变序列类型 :类似于字符串,但元组是不可变的,不支持在列表中任何原处修改操作,不支持任何方法调用 

4.固定长度、异构、任意嵌套 :固定长度即元组不可变,在不被拷贝的情况下长度固定,其他同列表 

5.对象引用的数组 :与列表相似,元祖是对象引用的数组,list相比 ①比列表操作速度快 ,②对数据“写保护“ ,③可用于字符串格式化中 ,④可作为字典的key。

三、字典

1.通过键而不是偏移量来读取 :字典就是一个关联数组,是一个通过关键字索引的对象的集合,使用键-值(key-value)进行存储,查找速度快 

2.任意对象的无序集合 :字典中的项没有特定顺序,以“键”为象征 

3.可变长、异构、任意嵌套 :同列表,嵌套可以包含列表和其他的字典等 

4.属于可变映射类型 :因为是无序,故不能进行序列操作,但可以在远处修改,通过键映射到值。字典是唯一内置的映射类型(键映射到值的对象) 

5.对象引用表 :字典存储的是对象引用,不是拷贝,和列表一样。字典的key是不能变的,list不能作为key,字符串、元祖、整数等都可以list比较,dict有以下几个特点: ①查找和插入的速度极快,不会随着key的增加而增加 ;②需要占用大量的内存,内存浪费多;list相反: ①查找和插入的时间随着元素的增加而增加 ;②占用空间小,浪费内存很少 ,所以,dict是用空间来换取时间的一种方法

四、集合

1.是一组key的集合,但不存储value,并且key不能重复 ,创建一个set,需要提供一个list作为输入集合,s = set([1,2,3]),注意,传入的参数 [1, 2, 3] 是一个list,而显示的 set([1, 2, 3]) 只是告诉你这个set内部有1,2,3这3个元素,显示的[ ]不表示这是一个list 

2.重复元素在set中自动被过滤 :set可以看成数学意义上的无序和无重复元素的集合,因此,两个set可以做数学意义上的交集、并集等操作,还有一种集合是forzenset( ),是冻结的集合,它是不可变的,存在哈希值,好处是它可以作为字典的key,也可以作为其它集合的元素。缺点是一旦创建便不能更改,没有addremove方法和dict对比 ①set和dict的唯一区别仅在于没有存储对应的value ;②set的原理和dict一样,同样不可以放入可变对象,因为无法判断两个可变对象是否相等,也就无法保证set内部“不会有重复元素”。