列表,元组,字典,集合

练习三

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))
    

 

posted @ 2018-09-21 11:26  何美玲  阅读(805)  评论(0编辑  收藏  举报