python基礎訓練題2-元組,字典

來源:互聯網
上載者:User

標籤:.so   字典   對象   span   mat   實現   添加   class   lis   

1,判斷值在元組中

>>> a = ( 1, 2, 3, 4, 10 )>>> 10 in aTrue>>> ‘10‘ in a False

2,修改元組中的值,由於元組不能被直接修改,可以先把他轉成列表,在通過列表修改之後,賦給一個新的元組對象

>>> a = ( 10, 20, 30, 40 )>>> l = list( a )>>> l[0] = 100>>> t = tuple( l )>>> t(100, 20, 30, 40)>>> id( a )139920488716168>>> id( t )139920488447152>>> type( a )<type ‘tuple‘>>>> type( t )<type ‘tuple‘>>>> 

3,向集合添加一個值,刪除一個值,求交集和並集

>>> aset([‘a‘, ‘c‘, ‘b‘, ‘e‘, ‘d‘, ‘g‘, ‘f‘])>>> a.add( ‘ghostwu‘ )>>> aset([‘a‘, ‘c‘, ‘b‘, ‘e‘, ‘d‘, ‘g‘, ‘f‘, ‘ghostwu‘])>>> a.remove( ‘g‘ )>>> aset([‘a‘, ‘c‘, ‘b‘, ‘e‘, ‘d‘, ‘f‘, ‘ghostwu‘])>>> b = set( "abcdlmn" )>>> a & bset([‘a‘, ‘c‘, ‘b‘, ‘d‘])>>> a | bset([‘a‘, ‘c‘, ‘b‘, ‘e‘, ‘d‘, ‘f‘, ‘m‘, ‘l‘, ‘n‘, ‘ghostwu‘])>>> 

4,用字典實現一個學產生績小系統,之後進行添加,修改,刪除,排序等操作

>>> student = { ‘ghostwu‘ : { ‘name‘ : ‘ghostwu‘, ‘age‘ : 20, ‘score‘ : { ‘math‘ : 78, ‘english‘ : 66, ‘python‘ : 75 } } }>>> student{‘ghostwu‘: {‘age‘: 20, ‘score‘: {‘python‘: 75, ‘math‘: 78, ‘english‘: 66}, ‘name‘: ‘ghostwu‘}}>>> student[‘tom‘] = { ‘name‘ : ‘tom‘, ‘age‘ : 21, ‘score‘ : { ‘math‘ : 60, ‘english‘ : 80, ‘python‘ : 90 } }>>> student{‘ghostwu‘: {‘age‘: 20, ‘score‘: {‘python‘: 75, ‘math‘: 78, ‘english‘: 66}, ‘name‘: ‘ghostwu‘}, ‘tom‘: {‘age‘: 21, ‘score‘: {‘python‘: 90, ‘math‘: 60, ‘english‘: 80}, ‘name‘: ‘tom‘}}>>> student[‘ghostwu‘][‘score‘][‘php‘] = 90>>> student[‘tom‘][‘score‘][‘php‘] = 50>>> student[‘ghostwu‘][‘score‘][‘math‘] = 30>>> del student[‘ghostwu‘][‘age‘]>>> score1 = student[‘ghostwu‘][‘score‘].values()>>> score1[75, 90, 30, 66]>>> score1.sort()>>> score1[30, 66, 75, 90]>>> student.pop( ‘address‘, ‘shenzhen‘ )‘shenzhen‘>>> student{‘ghostwu‘: {‘score‘: {‘python‘: 75, ‘php‘: 90, ‘math‘: 30, ‘english‘: 66}, ‘name‘: ‘ghostwu‘}, ‘tom‘: {‘age‘: 21, ‘score‘: {‘python‘: 90, ‘php‘: 50, ‘math‘: 60, ‘english‘: 80}, ‘name‘: ‘tom‘}}>>> 

 

python基礎訓練題2-元組,字典

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.