作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2753
分别定义字符串,列表,元组,字典,集合,并进行遍历。
|
1
2
3
4
|
# 字符串var = 'example'for i in var: print(i) |

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

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

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

|
1
2
3
4
|
# 集合a = set(['exam','p','le',21])for i 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 c in Set1: print(c)#字典dict1 = {'a':55,'b':66,'c':77}print(dict1)for d 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 girlfriendShe's upsetShe's going off about something that you said'Cause she doesn't get your humor like I doI'm in my roomIt's a typical Tuesday nightI'm listening to the kind of music she doesn't likeAnd she'll never know your story like I doBut she wears short skirtsI wear T-shirtsShe's cheer captainAnd I'm on the bleachersDreaming about the day when you wake upAnd find that what you're looking for has been here the whole timeIf you can see I'm the one who understands youBeen here all along so why can't you seeYou belong with meYou belong with meWalkin' the streets with you and your worn-out jeansI can't help thinking this is how it ought to beLaughing on a park bench thinking to myselfHey isn't this easyAnd you've got a smile that could light up this whole townI haven't seen it in a while since she brought you downYou say you're fineI know you better then thatHey what you doing with a girl like thatShe wears high heelsI wear sneakersShe's cheer captain and I'm on the bleachersDreaming about the day when you wake upAnd find that what what you're looking for has been here the whole timeIf you can see that I'm the one who understands youBeen here all along so why can't you seeYou belong with meStanding by and waiting at your back doorAll this time how could you not knowBaby you belong with meYou belong with meOh I remember you drivin' to my house in the middle of the nightI'm the one who makes you laughWhen you know you're about to cryAnd I know your favorite songsAnd you tell me about your dreamsI think I know where you belongI think I know it's with meCan't you see that I'm the one who understands youBeen here all along so why can't you seeYou belong with meStanding by and waiting at your back doorAll this timeHow could you not knowBaby you belong with meYou belong with meYou belong with meHave you ever thought just maybeYou belong with meYou 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) |

