python筆記(2)

來源:互聯網
上載者:User

標籤:index   [1]   amp   reverse   eval   元組   __str__   指標   sam   

#列表: 列表是可以嵌套的。word=[‘a‘,‘b‘,‘c‘]a=word[2]        #通過索引訪問列表print "a is:"+aa is:cword=[‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘,‘g‘]b=word[1:3]    #取從1開始到3前面的元素print b[‘b‘, ‘c‘]b=word[1:2]print b[‘b‘]print word[1:4]    #取從1開始到4前面的元素[‘b‘, ‘c‘, ‘d‘]print word[2:3]word[-1]        #-1表倒數aa=a[-4:-1]    #倒數截取,結果正排刪除列表的第一個值del sample_list[0]在列表中插入一個值sample_list[0:0] = [‘sample value‘]得到列表的長度list_length = len(sample_list)列表遍曆for element in sample_list:    print(element)list的方法L.append(var)   #追加元素L.insert(index,var)L.pop(var)      #返回最後一個元素,並從list中刪除之L.remove(var)   #刪除第一次出現的該元素L.count(var)    #該元素在列表中出現的個數L.index(var)    #該元素的位置,無則拋異常 L.extend(list)  #追加list,即合并list到L上L.sort()        #排序L.reverse()     #倒序list 操作符:,+,*,關鍵字dela[1:]       #片段操作符,用於子list的提取[1,2]+[3,4] #為[1,2,3,4]。同extend()[2]*4       #為[2,2,2,2]del L[1]    #刪除指定下標的元素del L[1:3]  #刪除指定下標範圍的元素list的複製L1 = L      #L1為L的別名,用C來說就是指標地址相同,對L1操作即對L操作。函數參數就是這樣傳遞的L1 = L[:]   #L1為L的複製,即另一個拷貝。元組:元組是不可變的,元組可以嵌套aTuple=(1,2,3,‘hello‘) aTuple (1, 2, 3, ‘hello‘) aTuple[0] 1 aTuple[-1] ‘hello‘ aTuple[2:] (3, ‘hello‘) aTuple[:2] (1, 2) aTuple[0:3] (1, 2, 3) aTuple[0]=5 #出錯for element in aTuple:    print element#字典:字典中的鍵/值對是沒有順序的x={‘a‘:‘aaa‘,‘b‘:‘bbb‘,‘c‘:12}print x[‘a‘]aaaprint x[‘c‘]12從Python 2.2 版本起 fdict = dict(([‘x‘, 1], [‘y‘, 2])) 可以以數字,元組為鍵,但不可以以列表,字典為鍵,鍵必須是不可變的元素a={1:11}b={2:22}x[a]=11111    #報錯x[b]=22222字典添加:x[‘d‘]=‘ddd‘#字典修改x[‘d‘]=‘dddd‘#字典刪除#del x[‘a‘]#x.clear()b=x.pop(‘b‘)for key in x:    print("%s=%s"%(key,x[key]))#----------------set-----------------------setset就像是把Dict中的key抽出來了一樣,類似於一個List,但是內容又不能重複,通過調用set()方法建立s = set([‘A‘, ‘B‘, ‘C‘])s.add(‘hello‘)s.add(‘ss‘)s.add(‘hello‘)s.remove(‘hello‘)s.update(‘zw‘)    #是把要傳入的元素拆分,做為個體傳入到集合中for key in s:    print key#---------------相互轉換----------------------#1、字典dict = {‘name‘: ‘Zara‘, ‘age‘: 7, ‘class‘: ‘First‘}#字典轉為字串,返回:<type ‘str‘> {‘age‘: 7, ‘name‘: ‘Zara‘, ‘class‘: ‘First‘}print type(str(dict)), str(dict)#字典可以轉為元組,返回:(‘age‘, ‘name‘, ‘class‘)print tuple(dict)#字典可以轉為元組,返回:(7, ‘Zara‘, ‘First‘)print tuple(dict.values())#字典轉為列表,返回:[‘age‘, ‘name‘, ‘class‘]print list(dict)#字典轉為列表print dict.values()#2、元組tup=(1, 2, 3, 4, 5)#元組轉為字串,返回:(1, 2, 3, 4, 5)print tup.__str__()#元組轉為列表,返回:[1, 2, 3, 4, 5]print list(tup)#元組不可以轉為字典#3、列表nums=[1, 3, 5, 7, 8, 13, 20];#列錶轉為字串,返回:[1, 3, 5, 7, 8, 13, 20]print str(nums)#列錶轉為元組,返回:(1, 3, 5, 7, 8, 13, 20)print tuple(nums)#列表不可以轉為字典#4、字串#字串轉為元組,返回:(1, 2, 3)print tuple(eval("(1,2,3)"))#字串轉為列表,返回:[1, 2, 3]print list(eval("(1,2,3)"))#字串轉為字典,返回:<type ‘dict‘>print type(eval("{‘name‘:‘ljq‘, ‘age‘:24}"))

 

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.