课堂练习 组合数据练习

成绩表增删改查遍历操作
 

classmate = list("123321123321")
print(classmate)

classmate.append('3')
print(classmate[-1])

classmate.pop()
print(classmate[-1])

classmate[-1] = 2
print(classmate[-1])

print(classmate.index('3'))
print(classmate.count('1'))
print(classmate.count('3'))
================ RESTART: C:/Users/Administrator/Desktop/1.py ================
['1', '2', '3', '3', '2', '1', '1', '2', '3', '3', '2', '1']
3
1
2
2
3
4
>>> 
建立学生学号成绩字典

score = {10:'41', 20:'64', 30:'71', 40:'81', 50:'91' } print(score) score[6] = '90' print(score) score.pop(6) print(score) for i in score: print("{:>2} : {:<2}".format(i,score.get(i)))
================ RESTART: C:/Users/Administrator/Desktop/1.py ================
{10: '41', 20: '64', 30: '71', 40: '81', 50: '91'}
{10: '41', 20: '64', 30: '71', 40: '81', 50: '91', 6: '90'}
{10: '41', 20: '64', 30: '71', 40: '81', 50: '91'}
10 : 41
20 : 64
30 : 71
40 : 81
50 : 91
>>> 
列表,元组,字典,集合。


ls=list("4613125646")
tu=tuple("4613125646")
se=set("4613125646")
d={'阿一':93,'阿二':74,'阿三':45,'阿四':66}
print("列表:",ls)
for i in ls:
    print(i,end=' ')
print("\n")
print("元组:",tu)
for i in tu:
    print(i,end=' ')
print("\n")
print("集合:",se)
for i in se:
    print(i,end=' ')
print("\n")
print("字典:",d)
for i in d:
    print(i,end='\t')
    print(d[i],end='\n')
================ RESTART: C:/Users/Administrator/Desktop/1.py ================
列表: ['4', '6', '1', '3', '1', '2', '5', '6', '4', '6']
4 6 1 3 1 2 5 6 4 6 

元组: ('4', '6', '1', '3', '1', '2', '5', '6', '4', '6')
4 6 1 3 1 2 5 6 4 6 

集合: {'4', '2', '6', '3', '5', '1'}
4 2 6 3 5 1 

字典: {'阿一': 93, '阿二': 74, '阿三': 45, '阿四': 66}
阿一	93
阿二	74
阿三	45
阿四	66
歌词统计 

song = '''Vincent,Don McLean. Starry starry night 
 paint your palette blue and grey 
 look out on a summer\'s day 
 with eyes that know the darkness in my soul. 
 Shadows on the hills 
 sketch the trees and the daffodils 
 catch the breeze and the winter chills 
 in colors on the snowy linen land. 
 And now I understand 
 what you tried to say to me 
 and how you suffered for your sanity 
 and how you tried to set them free. 
 They would not listen they did not know how 
 perhaps they\'ll listen now. 
 Starry starry night 
 flaming flowers that brightly blaze 
 swirling clouds in violet haze 
 reflect in Vincent\'s eyes of China blue. 
 Colors changing hue 
 morning fields of amber grain 
 weathered faces lined in pain 
 are smoothed beneath the artist\'s loving hand. 
 And now I understand 
 what you tried to say to me 
 and how you suffered for your sanity 
 and how you tried to set them free. 
 They would not listen they did not know how 
 perhaps they\'ll listen now. 
 For they could not love you 
 but still your love was true 
 and when no hope was left in sight on that 
 starry starry night. 
 You took your life as lovers often do, 
 But I could have told you Vincent 
 this world was never meant for one as beautiful as you. 
 Starry starry night 
 portraits hung in empty halls 
 frameless heads on nameless walls 
 with eyes that watch the world and can\'t forget. 
 Like the stranger that you\'ve met 
 the ragged men in ragged clothes 
 the silver thorn of bloddy rose 
 lie crushed and broken on the virgin snow. 
 And now I think I know 
 what you tried to say to me 
 and how you suffered for your sanity 
 and how you tried to set them free. 
 They would not listen they\'re not listening still 
 perhaps they never will.'''

song = song.lower()
for i in '?!,.\'\n':
    song = song.replace(i,' ')
words = song.split(' ')

dic={}
keys = set(words)
for i in keys:
    dic[i]=words.count(i)

c = list(dic.items())
c.sort(key=lambda x:x[1],reverse=True)

for i in range(10):
    print(c[i])
================ RESTART: C:/Users/Administrator/Desktop/1.py ================
('', 112)
('and', 15)
('you', 14)
('the', 13)
('they', 10)
('to', 9)
('starry', 8)
('how', 8)
('in', 8)
('not', 7)
>>> 

  

  

  

  

  

  

  

转载于:https://www.cnblogs.com/55lsk/p/7567232.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值