python-組合資料類

來源:互聯網
上載者:User

標籤:date   secure   ==   roo   src   got   元祖   ict   over   

列表:由字串建立一個作業評分列表,做增刪改查詢統計遍曆操作。例如,查詢第一個3分的下標,統計1分的同學有多少個,3分的同學有多少個等。

 s=list(‘kkkkk‘)s.insert(3,2)s.pop(3)s[6]=1print(s)print(‘第一個1的位置是:‘,s.index(‘1‘))x=0y=0for i in range(len(s)):    if s[i]==‘3‘:        x=x+1        if x==1:            y=iprint(‘三分個數為:‘,x,‘ ‘,‘第一個三分位置是:‘,y)print(‘二分個數為:‘,s.count(‘2‘))

字典:建立學生學號成績字典,做增刪改查遍曆操作。

>>> dic=dict(zip([1,2,3],[‘98‘,‘100‘,‘88‘]))>>> dic{1: ‘98‘, 2: ‘100‘, 3: ‘88‘}>>> dic2={4:‘95‘}>>> dic.update(dic2)>>> dic{1: ‘98‘, 2: ‘100‘, 3: ‘88‘, 4: ‘95‘}>>> dic.pop(2)‘100‘>>> dic{1: ‘98‘, 3: ‘88‘, 4: ‘95‘}

>>> dic{1: ‘120‘, 3: ‘88‘, 4: ‘95‘}>>> del dic[3]>>> dic{1: ‘120‘, 4: ‘95‘}>>> dic[1]=‘120‘>>> dic{1: ‘120‘, 3: ‘88‘, 4: ‘95‘}>>> dic{1: ‘98‘, 3: ‘88‘, 4: ‘95‘}>>> dic.get(4)‘95‘>>> dic.get(5,‘沒有‘)‘沒有‘

>>> dic[4]‘95‘>>> dic.keys()dict_keys([1, 3, 4])>>> dic.values()dict_values([‘98‘, ‘88‘, ‘95‘])>>> dic.items()dict_items([(1, ‘98‘), (3, ‘88‘), (4, ‘95‘)])dict

列表,元組,字典,集合的遍曆。

s=list(‘456132798‘)t=set(‘4561327489‘)c={‘小明‘:‘1‘,‘小紅‘:‘3‘,‘小華‘:‘2‘}x=(1, 2, 3, 4, 5 )print(‘列表的遍曆為:‘,end=‘‘)for i in s:    print(i,end=‘‘)print()print(‘元祖的遍曆為:‘,end=‘‘)for i in x:    print(i,end=‘‘)print()print(‘字典key的遍曆為:‘,end=‘‘)for i in c:    print(i,end=‘‘)print()print(‘字典value的遍曆為:‘,end=‘‘)for i in c.values():    print(i,end=‘‘)print()print(‘數組的遍曆為:‘,end=‘‘)for i in x:    print(i,end=‘‘)

 

 

‘‘‘下載一首英文的歌詞或文章,統計單詞出現的次數,將所有,.?!替換為空白格,將所有大寫轉換為小寫。‘‘‘sr=‘‘‘You‘re ?insecureDon‘t know what forYou‘re turning heads through the doorDon‘t need make-upto cover upBeing the way that you are is enoughEveryone else in the room can see itEveryone else but youBaby you light up my world like nobody elseThe way that you flip your hair gets me overwhelmedBut when you smile at the ground it ain‘t hard to tellYou don‘t know Oh ohYou don‘t know you‘re beautifulIf only you saw what I can seeYou‘ll understand why I want you so desperatelyRight now I‘m looking at you and I can‘t believeYou don‘t know Oh !ohYou don‘t know you‘re beautiful Oh ohThat‘s what makes you beautifulSo c-come onYou got it wrongTo prove I‘m ,right ‘‘‘print(sr.count(‘you‘))for i in sr:    sr=sr.replace(‘,‘,‘/t ‘)    sr=sr.replace(‘!‘,‘/t ‘)    sr=sr.replace(‘?‘,‘/t ‘)print(sr)for i in sr:    sr=sr.upper()print(sr)

 

python-組合資料類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.