码迷,mamicode.com
首页 > 其他好文 > 详细

练习三

时间:2018-09-20 23:54:50      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:append   love   技术   alt   cross   print   dict   time   values   

总结:

区别:1.列表。可以用list()函数或者[]创建,元素之间用“,”分隔;列表的元素不需要有相同的类型;使用索引来访问元素,元素是有序的,可重复。

2元组。用()或者tuple()函数来实现,元素之间用“,”分隔;元素的值初始化一旦就不可修改;元组只读不写,可重复,元素是有序的。

3集合。可以用set()函数或者{}创建,元素之间用“,”分隔;与字典相比少了键;不可有重复元素;可以读写,是无序的。

4字典。由键key和值values组成;可以用dict()函数或者{}创建,元素之间用“,”分隔,键与值之间用":"分隔;键必须是唯一的、不可变的,但值不要求;元素是无序的;用key来访问元素。

联系:1.元素之间用“,”分隔。

2.列表和元组:列表是可变序列,元组是不可变序列;列表的值可以修改,而元祖的值初始化后不可修改,两者都是有序的。

集合和字典:两者都是无序的;数据量大时可用集合或列表来创建

 

1.英文词频统计

strBig=(‘‘‘every night in my dreams
i see you, i feel you,
that is how i know you go on
far across the distance
and spaces between us
you have come to show you go on
near, far, wherever you are
i believe that the heart does go on
once more you open the door
and youre here in my heart
and my heart will go on and on
love can touch us one time
and last for a lifetime
and never let go till we‘re one
love was when i loved you
one true time i hold to
in my life we‘ll always go on
near, far, wherever you are
i believe that the heart does go on
once more you open the door
and you‘re here in my heart
and my heart will go on and on
there is some love that will not go away
you‘re here, there‘s nothing i fear,
and i know that my heart will go on
we‘ll stay forever this way
you are safe in my heart
and my heart will go on and on‘‘‘)

strList=strBig.split( )
print(strList)
a=str.upper(strBig)
print("全部大写:"+a)
b=str.lower(strBig)
print("全部小写:"+b)
print(strBig.count(you))
c=str.capitalize(strBig)
print("首字母大写其余小写:"+c)
d=str.title(strBig)
print("首字母大写:"+d)
e=str.swapcase(strBig)
print("大小写互换:"+e)
k=max(strBig)     ##max()
print(k)
m=min(strBig)
print(m)
strSet=set(strList)     ##字母出现的次数
for word in strSet:
    print(word,strList.count(word))

技术分享图片

技术分享图片

技术分享图片

技术分享图片

技术分享图片

 

2.列表

classmates=[Michael,Bob,Tracy,李三,Tracy,56]
a=classmates[1]      ##取元素片段
print("取元素片段:"+a)
b=classmates[-1]        ##取最后一个元素
print(b)
print(classmates[1:3])  ##取元素1:3
f=len(classmates)      ##取属性
print(f)
c=classmates.index(Tracy)   ##取元素的索引
print(c)
d=classmates[1]=Sarah    ##修改
print("修改为:"+d)
e=classmates.count(Tracy)  ##计数
print(e)
print(classmates)
h=classmates.insert(2,"Jack")   ##插入元素
print(h)
print(classmates)
i=classmates.append("Adam")    ##追加元素
print(i)
print(classmates)
j=classmates.pop(1)   ##删除元素
print(j)
print(classmates)

技术分享图片

 

3.元组

class1=(Micheal,Bob,Tracy,李三,Tracy,56)
print(len(class1))    ##取属性
a=tuple(abcde)   ##元组
print(a)

技术分享图片

 

4.集合

s={1,2,3}
a=s.add(4)
print(a)
print(s)
b=s.remove(4)
print(b)
print(s)
s=set([1,2,2,3,4])
s1=set([1,2,3])
s2=set([2,3,4])
g=s1 & s2    ##集合的交运算
print(g)
k=s1| s2     ##集合的并运算
print(k)
g=s2 - s1    ##集合的差运算
print(g)


classmates=[Michael,Bob,Tracy,李三,Tracy,56]
new=[Rose,Jack]
classmates.insert(2,Jack)
classmates.extend(new)    ##扩展
print(classmates)
classmatesSet=set(classmates)
print(classmatesSet)
z=classmatesSet.add(Rose)   ##增加元素
print(z)
print(classmatesSet)
w=classmatesSet.remove(Bob)   ##删除元素
print(w)
print(classmatesSet)

技术分享图片

5.字典

classmates=[Michael,Bob,Tracy]
scores=[95,75,85]
d={Bob:75,"Michael":95,Tracy:85}
print(d[Michael])   ##取值
print(len(d))    ##元素个数
Thomas in d  ##判断Thomas是否在d中
d[Rose]=88   ##添加与修改
print(d.pop(Bob))  ##删除
print(d.keys())    ##输出元素的键
print(d.values())  ##输出元素的值
print(d.items())

 

技术分享图片

 

练习三

标签:append   love   技术   alt   cross   print   dict   time   values   

原文地址:https://www.cnblogs.com/jun11/p/9679944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!