列表,元组,字典,集合

练习三

1.列表、元组、集合、字典区别:列表的数据项不需要具有相同的类型,创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可


Python 的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。



字典是另一种可变容器模型,且可存储任意类型对象。字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,
 
集合(set)是一个无序不重复元素的序列。可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。 



2.列表、元组、集合和字典遍历:列表是可变序列,元组是不可变序列;列表的值可以修改,而元祖的值初始化后不可修改,两者都是有序的。字典和集合 两者都是无序的,数据量大时可用集合或列表来创建

英文词频统计:

  • 下载一首英文的歌词或文章str
  • 分隔出一个一个的单词 list
  • 统计每个单词出现的次数 dict
strRun='''Well looky here looky here
Ah what do we have?
Another pretty thang ready for me to grab
But little does she know
That I'm a wolf in sheeps clothing
'Cause at the end of the night
It is her I'll be holding

I love you so
That's what you'll say
You'll tell me
Baby baby please don't go away
But when I play, I never stay

To every girl that I meet here this is what I say:
Run run run away, run away baby
Before I put my spell on you
You better get get get away get away darling
'Cause everything you heard is true
Your poor little heart will end up alone
'Cause lord knows I'm a rolling stone
So you better run run run away run away baby

Well let me think let me think
Ah what should I do?
So many eager young bunny's
That I'd like to pursue
Now even now they eating out the palm of my hand
There's only one carrot and they all gotta share it

I love you so
That's what you'll say
You'll tell me
Baby baby please don't go away
But when I play, I never stay
To every girl that I meet here this is what I say:

Run run run away, run away baby
Before I put my spell on you
You better get get get away get away darling
'Cause everything you heard is true
Your poor little heart will end up alone
'Cause lord knows I'm a rolling stone
So you better run run run away run away baby'''

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

strListSort=strList.sort()
print(strListSort)
print(strList)

a=str.upper(strRun)
print("全部大写:"+a)

b=str.lower(strRun)
print("全部小写:"+b)

print(strRun.count('Run'))

f=str.capitalize(strRun)
print("首字母大写其余小写"+f)

h=str.swapcase(strRun)
print("大小写互换"+h)

m=max(strRun)
print("最大:"+m)

n=min(strRun)
print("最小:"+n)

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

 

转载于:https://www.cnblogs.com/h000/p/9685681.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值