第三周作业

总结列表,元组,字典,集合的联系与区别:list列表,有序的项目,通过索引进行查找,定义括号使用“[]”;tuple元组,元组可以将多样的对象集合在一起,但是不能修改,元组元素指向的元素不可修改,可修改指向的内容,通过索引号查找元素,定义使用“()”括号;dict字典,是一组键值对组合{key,value},通过键(key)进行查找,没有顺序,定义使用“{}”;set集合,无顺序,元素具有不可重复性,自动去重,使用“([])”;列表随着插入的增加,查询速度会变慢,而字典的查询效率较高,但占用的内存会较多,集合、字典没有下标,无法通过下标访问。

1.

lis = [1,2,3,4]     #定义一个列表
lis.append(8)  #列表lis增加数值8
print(lis)
d = (1,2,lis,4)     #定义一个元组,注元组里面lis是一个列表
print(d)
print(d[2])
d[2][2] = 0  #不修改元组元素指向的元素,修改指向的内容
print(d)
dic = {'a':1,'b':3,'c':7}   #定义一个字典
print(dic)
a=dict(one=1,two=3)
b=dict([('a',1),('b',3),('c',7)])
c=dict({'a':1,'b':3,'c':7})
del dic['a']  #删除元素a
print(dic)

 运行结果:

2.

s = {2,4,6,87,7,8,9,'Tom'}
print(s)
s.add('Nancy')   #集合的增加
print(s)
s.remove(87)    #集合的删除
print(s)
s1 = {1,2,4,7,5,0,'Tom',3}
print(s1)
set1 = s & s1  #集合的交运算
print(set1)
set2 = s| s1    #集合的并运算
print(set2)
set3 = s - s1  #集合的差运算
print(set3)

 

3.

strSuddenly = '''suddenlywe make our pacts
wearing the pendant
we dont borrow boyfriends
and we do our hair anyway we would like.
we figured out 
that we are attractive
and we look around
and now we loved to live the single life
and then we tell ourselves we'll never fall in love again
but then he comes around and suddenly we understand
that we've never been living in love before
and suddenly you know what all the love songs that they write are all about
and suddenly you dont care if its right or wrong as long as he's around
and suddenly the things that used to sound clishe are perfectly right in your eyes
perfectly right with this sky
i know its shrewd
but we are connected
and in some strange and crazy way i think
that we have always been
and now he's here
and he says he loves me
and it feels so right
and i could feel so good that i cant sleep at night
but i just told myself i will not fall in love again
but he just came around and then he made me understand
that i have never been living in love before
and suddenly you know what all the love songs that they write are all about
and suddenly you dont care if its right or wrong as long as he's around
and suddenly the things that used to sound clishe are perfectly right in my eyes
perfectly right when he's here
and yes
i know you might get impatient
but look around
he might be walking right in front of you
and if he touches you and you feel your skin is burning
kisses you and you feel your stomach turning
he's the one
he is the one
and suddenly you know what all the love songs that they write are all about
and suddenly you dont care if its right or wrong as long as your baby's around
and suddenly the things that used to sound clishe are perfectly right in your ears
perfectly right when he's there
perfectly right when he's there
perfectly right with this sky there '''

strList = strSuddenly.split(' ')
print(strList)

strSet = set(strList)
for word in strList:
    print(word,strList.count(word))

 运行结果:

posted @ 2018-09-22 01:55  MISTanglijuan  阅读(144)  评论(0编辑  收藏  举报