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

作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2753

分别定义字符串,列表,元组,字典,集合,并进行遍历。

1
2
3
4
# 字符串
var = 'example'
for in var:
    print(i)

  

1
2
3
4
#  列表
= ['exam','p','le']
for in s :
    print(i)


1
2
3
4
# 元组
tu = ('exam','p','le',1,15)
for in tu:
    print(i)

  

 

1
2
3
4
5
6
# 字典
names = ['Michael','Bob','Tracy']
scores = [95,75,85]
= dict(zip(names,scores))
for i,j in d.items():
    print(i,j)

 

1
2
3
4
# 集合
= set(['exam','p','le',21])
for in a :
    print(i)

 

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

  • 列表:线性表,使用节点/元素来描述里面的单个数据,可增删查改
  • 元祖: 与列表相似,不可修改操作
  • 字典:使用key-value的形式保存数据,使用hash码生成key来确定存放位置
  • 集合:与字典相似,但是没有value,只有key
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#列表
list=['Tt','Aa','Bb',10]
print(list)
for A1 in list:
    print(A1)
#元组
Tuple1=('abcdefg')
print(Tuple1)
for B1 in Tuple1:
    print(B1)
#集合
Set1={2,4,6,8}
print(Set1)
for in Set1:
    print(c)
#字典
dict1 = {'a':55,'b':66,'c':77}
print(dict1)
for in dict1:
    print(d,dict1[d])

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
hh='''You're on the phone with your girlfriend
She's upset
She's going off about something that you said
'Cause she doesn't get your humor like I do
I'm in my room
It's a typical Tuesday night
I'm listening to the kind of music she doesn't like
And she'll never know your story like I do
But she wears short skirts
I wear T-shirts
She's cheer captain
And I'm on the bleachers
Dreaming about the day when you wake up
And find that what you're looking for has been here the whole time
If you can see I'm the one who understands you
Been here all along so why can't you see
You belong with me
You belong with me
Walkin' the streets with you and your worn-out jeans
I can't help thinking this is how it ought to be
Laughing on a park bench thinking to myself
Hey isn't this easy
And you've got a smile that could light up this whole town
I haven't seen it in a while since she brought you down
You say you're fine
I know you better then that
Hey what you doing with a girl like that
She wears high heels
I wear sneakers
She's cheer captain and I'm on the bleachers
Dreaming about the day when you wake up
And find that what what you're looking for has been here the whole time
If you can see that I'm the one who understands you
Been here all along so why can't you see
You belong with me
Standing by and waiting at your back door
All this time how could you not know
Baby you belong with me
You belong with me
Oh I remember you drivin' to my house in the middle of the night
I'm the one who makes you laugh
When you know you're about to cry
And I know your favorite songs
And you tell me about your dreams
I think I know where you belong
I think I know it's with me
Can't you see that I'm the one who understands you
Been here all along so why can't you see
You belong with me
Standing by and waiting at your back door
All this time
How could you not know
Baby you belong with me
You belong with me
You belong with me
Have you ever thought just maybe
You belong with me
You belong with me'''.lower()
aa = ''',."?!'''
for word in aa:
    str2 =hh.replace('word',' ')
hh =hh.replace(' ','')
hh = hh.strip()
hh = hh.split(' ')
print(hh)
 
strSet=set(hh)
for word in strSet:
   print(word,hh.count(word))
dict={}          #单词计数字典
1
for word in hh:<br>  dict[word] = hh.count(word) <br>print(len(dict),dict)

  

 

原文地址:https://www.cnblogs.com/lingzihui/p/10578366.html