组合数据类型,英文词频统计

#字典
classmates=['Michael','Bob','Tracy']
scores=[95,75,85]
cs=dict(zip(classmates,scores))
print(cs)
#取值
print(cs['Michael'])
#长度
print(len('Michael'))
#删除
cs.pop('Bob')
print(cs)
#遍历
print(cs.keys())
print((cs.items()))
print(cs.values())
#判断Thomas是否在d中
print('Thomas' in classmates)
#添加或修改
cs['Rose']=88
print(cs)

 

 

 

classmates=['Michael','Bob','Tracy','李三','Tracy','56']
#取元素
print(classmates[1])
#取片段
print(classmates[1:3])
#取属性
print(len(classmates))
print((max(classmates)))
print((min(classmates)))
#取元素的索引
print((classmates.index('Tracy')))
#修改
classmates[1]='Sarah'
print(classmates)
#排序
classmates.sort()
print(classmates)
#计数
print(classmates.count('Tracy'))
#追加
classmates.append('Adam')
print(classmates)
#弹出
classmates.pop()
print(classmates)
#删除
classmates.pop(1)
print(classmates)
del classmates[-1]
print(classmates)
#插入
classmates.insert(1,'Jack')
print(classmates)
#扩展
new=['yuli','huating']
classmates.extend(new)
print(classmates)
#排序
classmates.sort()
print(classmates)

 

 

 

#集合
classmates=['Michael','Bob','Tracy']
classmatesSet=set(classmates)
print(classmatesSet)
#集合增加元素
classmatesSet.add('Jack')
print(classmatesSet)
#集合删除元素
classmatesSet.remove('Jack')
print(classmatesSet)
#生成集合
s=set([1,1,2,2,3,3])
print(s)
#集合交运算
s1=set([1,2,3])
s2=set([2,3,4])
s1&s2
print(s1&s2)
#集合差运算
s1-s2
print(s1-s2)
#集合并运算
s1|s2
print(s1|s2)

 

 

#元组
tuple=(1,2,3,4,5,6,7,8)
print(tuple)
tuple=('a,b,c,d')
print(tuple)
#取属性
print(len(tuple))

 

 

 

 

strLines=('''Another weekend, oh
I was with my friends that is true
But you were sneaking, oh
You forgot you said you'd drive me home
So took a taxi home
Cold and alone and in the dark on my own
So we're not speaking, no
 Read between the silence on the phone
I'm not the kind of girl that would go fucking you off
But you're the kind of guy that get's nothing when taking it off
You'll be acting like you don't know what you did that was wrong
So I hope you pay attention when I'm singing this song
 Lines, oh oh oh, there's a thing called lines, oh oh oh
And I said don't care, but maybe I might
Baby there's a thing called lines
 Oh oh oh, but yours aren't white, oh oh oh
 I wrote your sorry soul, and I crossed them out
 A thing called lines, oh oh oh, lines, oh oh oh
Lines, oh oh oh, there's a thing called lines
 My girl was swiping, through
 Funny how she came across you
So she swiped right on, you
Didn't think you'd swipe right too
 I've had about enough
Losing trust and there's nothing left to do
 You put an X on us, and you've got a lot to undo
I'm not the kind of girl that would go fucking you off
But you're the kind of guy that get's nothing when taking it off
 You'll be acting like you don't know what you did that was wrong
 So I hope you pay attention when I'm singing this song
 Lines, oh oh oh, there's a thing called lines, oh oh oh
 And I said don't care, but maybe I might
Baby there's a thing called lines
 Oh oh oh, but yours aren't white, oh oh oh
I wrote your sorry soul, and I crossed them out
 A thing called lines, oh oh oh, lines, oh oh oh
Lines, oh oh oh, there's a thing called lines
 So this is it, your final strike
 I've given you chances, you're a lucky guy
 Love is pure, as cure as seen
Love is pure, as cure as seen
Neither of us are perfect, but you're just being mean
So cross my heart, and made to die
Stick a needle, in my eye
If only once, if only twice
But one more time, and you've crossed the
Line, oh oh oh, there's a thing called lines, oh oh oh
 And I said don't care, but maybe I might
 Baby there's a thing called lines
 Oh oh oh, but yours aren't white, oh oh oh
I wrote your sorry soul, and I crossed them out
A thing called lines, oh oh oh, lines, oh oh oh
 
 A thing called lines, oh oh oh, lines, oh oh oh
Lines, oh oh oh, there's a thing called lines''')
#转化为列表
strList=strLines.split()
print(strList)
# 全部转化为大写
h1=str.upper(strLines)
print(h1)
#全部转化为小写
h2=str.lower(strLines)
print(h2)
#首字母大写其余小写
h3=str.capitalize(strLines)
print(h3)
#首字母大写
h4=str.title(strLines)
print(h4)
#大小写互换
h5=str.swapcase(strLines)
print(h5)

print('constellation'.center(46,'-'))
print('开始执行'.center(50,'-'))
#计数
print(strLines.count('oh'))
#替换
print(strLines.replace('sorry','love'))
#字母出现的字数
print(len(strList),strList)
strSet=set(strList)
print(len(strSet),strSet)
strDict={}
for word in strSet:
    strDict[word]=strList.count(word)
print(len(strDict),strDict)

h6=max(strLines)
print(h6)
h7=min(strLines)
print(h7)

 

 

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

列表

1.可以用list()函数或者方括号[]创建,元素之间用逗号’,‘’分隔。 
2.列表的元素不需要具有相同的类型 
3.使用索引来访问元素

 

元组

元组跟列表很像,只不过元组用小括号来实现()

具有以下特点: 
1.可以用tuple()函数或者方括号()创建,元素之间用逗号’,‘’分隔。 
2.元组的元素不需要具有相同的类型 
3.使用索引来访问元素 
4.元素的值一旦创建就不可修改!!!!!(这是区别与列表的一个特征)

 

字典

具有以下特点:

1.元素由键(key)和值(value)组成 
2.可以用dict()函数或者方括号()创建,元素之间用逗号’,‘’分隔,键与值之间用冒号”:”隔开 
3.键必须是唯一的,但值则不必。值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组 
4.使用键(key)来访问元素

 

集合

具有以下特点:

1.可以用set()函数或者方括号{}创建,元素之间用逗号”,”分隔。 
2.与字典相比少了键 
3.不可索引
4.不可以有重复元素

 

联系

(1)列表和元组有很多相似的地方,操作也差不多。不过列表是可变序列,元组为不可变序列。也就是说列表主要用于对象长度不可知的情况下,而元组用于对象长度已知的情况下,而且元组元素一旦创建变就不可修改。 
例如我们在打开一个文本时,并不知道里面有多少行文字,所以用列表来保存。

(2)字典主要应用于需要对元素进行标记的对象,这样在使用的时候便不必记住元素列表中或者元组中的位置,只需要利用键来进行访问对象中相应的值。

(3)在海量数据中查找元素时,最好将数据创建为字典,或者是集合

 

 

 

列表,元组,字典,集合的遍历:

 

元组的遍历 : 元组的遍历借助 range() 函数,基本思想是通过元组的长度使用for循环进行遍历

列表的遍历:列表的遍历可以直接使用for循环,也可以借助 range() 函数

字典的遍历:字典的遍历主要借助于字典中的key值

list集合有三种遍历方式:for循环,迭代器,foreach

 

 

 

转载于:https://www.cnblogs.com/zwlh/p/9686016.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值