Python之路 day2 字串/元組/列表/字典互轉

來源:互聯網
上載者:User

標籤:str   返回   log   span   __str__   eval   color   values   字典   

 1 #-*-coding:utf-8-*-  2  3 #1、字典 4 dict = {‘name‘: ‘Zara‘, ‘age‘: 7, ‘class‘: ‘First‘} 5  6 #字典轉為字串,返回:<type ‘str‘> {‘age‘: 7, ‘name‘: ‘Zara‘, ‘class‘: ‘First‘} 7 print type(str(dict)), str(dict) 8  9 #字典可以轉為元組,返回:(‘age‘, ‘name‘, ‘class‘)10 print tuple(dict)11 #字典可以轉為元組,返回:(7, ‘Zara‘, ‘First‘)12 print tuple(dict.values())13 14 #字典轉為列表,返回:[‘age‘, ‘name‘, ‘class‘]15 print list(dict)16 #字典轉為列表17 print dict.values18 19 #2、元組20 tup=(1, 2, 3, 4, 5)21 22 #元組轉為字串,返回:(1, 2, 3, 4, 5)23 print tup.__str__()24 25 #元組轉為列表,返回:[1, 2, 3, 4, 5]26 print list(tup)27 28 #元組不可以轉為字典29 30 #3、列表31 nums=[1, 3, 5, 7, 8, 13, 20];32 33 #列錶轉為字串,返回:[1, 3, 5, 7, 8, 13, 20]34 print str(nums)35 36 #列錶轉為元組,返回:(1, 3, 5, 7, 8, 13, 20)37 print tuple(nums)38 39 #列表不可以轉為字典40 41 #4、字串42 43 #字串轉為元組,返回:(1, 2, 3)44 print tuple(eval("(1,2,3)"))45 #字串轉為列表,返回:[1, 2, 3]46 print list(eval("(1,2,3)"))47 #字串轉為字典,返回:<type ‘dict‘>48 print type(eval("{‘name‘:‘ljq‘, ‘age‘:24}"))

 

Python之路 day2 字串/元組/列表/字典互轉

聯繫我們

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