Python學習第一天20180701--Python列表、元組、字典和集合經常使用方法

list[] #列表內容有序,內容可修改
  li.sort() #必須同爲字符串或數字
  li.reverse #反轉序列
  li.append() #後面追加列表內容
  li.clear()
  li.copy() #拷貝一個list
  li.count(object) #計算某個元素在list內出現的次數
  li.index(object) #查找某個元素在list內出現的位置
  li.extend(iterable object)#後面追加列表內容
  li.insert(index,object)#在特定的位置增長列表內容
  li.pop()#隨機刪除元素
  li.remove(object)#刪除特定的元素

  li[0]#索引
  li[0:1]#切片
  for循環
  in判斷
l  ist內容可轉換爲字符串
  只有字符串
    ''.join(list)
  有數字有字符串
    s = ""
    for i in list:
      s = s + str(i)
    print(s)

tuple() #元組內一級元素不能夠被修改/刪除/增長,元素有序
  tuple.count(object)#次數
  tuple.index(object)#位置

  tu.[0]#索引
  tu[0:1]#切片
  for循環
  字符串、列表、元組可相互轉換

dict{} #字典內容無序,默認循環爲key值
  dict={'1':1,'2':3}
  dict.fromkeys()#根據序列建立字段,而且指定默認值
  di.keys()#獲取字典關鍵值
  di.values()#獲取字典值
  di.items()#獲取字典鍵值對
  di.get()
  di.update()#更新
    di.update(a=25)
    di.update({"222":222})
  di.pop()#刪除字典的元素
  di.popitems#隨機刪除字典的元素
  di.setdefault()#設置默認值
  python

  字典的value能夠爲任何值
  布爾值(1,0)、列表、字典不能做爲字典的key
  di{'keys'}索引
  del di[keys] 支持del刪除
  for循環

set{} #集合內容無序且不得重複
  se=set('abc')#建立集合
  se1=frozenset('acfg')#建立不可變的集合
  se.update(iterable object)#更新多個值
  se.add()#更新一個值 需能夠hash的
  se.copy()
  se.clear()
  se.pop()#隨機刪除集合內元素
  se.discard()#刪除指定元素,若刪除元素不存在,不報錯
  se.remove()#刪除指定元素,若刪除元素不存在,則報錯
  se.issubset(se1)#子集
  se.issuperset(se1)#父集app


  | 並集
  se|se1
  se.union(se1)

  & 交集
  se&se1
  se.intersection(se1)

  - 差集
  se-se1
  se.difference(se1)

  ^ 交叉補集
  se^se1
  se.symmetric_difference(se1)
spa

####數據類型需掌握的方法######code


# 1、數字
  強制轉換
  int() float()、

# 2、字符串
  str=' acfgggbsfg2{}43t56g{}y4gfsf'
  replace()
    str.replace'1','b')
  find()
    str.find('g')
  join
    ''.join(str)
  strip
    str.strip()
  startswith()
    str.startswith('2')
  split()
    split('c')
  upper()
  lower()
  format()
    str.format('acv','陳維')
  tempalte = "i am {name}, age : {age}"
  v = tempalte.format(name='alex',age=19)
  v = tempalte.format(**{"name": 'alex','age': 19})
  print(v)

# 3、列表
  li=[1,'cc',2,'gfgg',[1,2]]
  append()
    li.append('fff')

  extend()
    li.expend(['a','n'])
    li. insert(2,['a','n'])
索引、切片、循環

# 4、元組
  tu=(1,2,3,4,'f',)
  一級元素不可更改/刪除/增長
  索引、切片、循環

5、字典
  di={'1':2,'key':'v'}
  get()
    di.get('1')
    di.update({'1':4})
    di.update(1=4)

  di.keys()
  di.values()
  di.items()
  for,索引orm

# 6、布爾值
  0 1
  bool(...)
  None "" () [] {} 0 ==> False blog

 1 #!/usr/bin/env python
 2     # -*- coding: utf-8 -*-
 3     # @Time : 2018/7/5 10:13
 4     # @Author : chenxiaowei
 5     # @Email : chen1020xiaowei@163.com
 6     # @File : 列表、元祖、字典的方法.py
 7 
 8     li = ['a', 'c', 'True', 'leibie', '列表']  9  li.sort() 10     li.append(1) 11     c = li.index('c') 12     li.extend('afeascca') 13     li.insert(1, 8) 14     li.pop(3) 15     print(li) 16     s = ""
17     for i in li: 18         s = s + str(i) 19     print(s) 20     print('####################################################') 21 
22     tu = ('a', 'c', 'True', 'leibie', '列表',) 23     d = tu.index('c') 24     print(tu, d) 25     l1 = tuple(li) 26     s1 = tuple(s) 27     print(s1, l1) 28     print('####################################################') 29 
30     di = {'a': 23, 'c': 'fd', 'True': 'chenxiaowei', 'leibie': tu, '列表': li} 31     di1=dict.fromkeys('111122345656787','g') 32     di.get('111',22) 33     v=di.keys() 34     v2=di.values() 35     v1=di.items() 36     v3=di.pop('c') 37     v4=di.popitem() 38     di.setdefault('222',22) 39     di.update(a=25) 40     di.update({"222":222}) 41     print(di)

 

 1 #!/usr/bin/env python
 2         # -*- coding: utf-8 -*-
 3         # @Time : 2018/7/5 22:18
 4         # @Author : chenxiaowei
 5         # @Email : chen1020xiaowei@163.com
 6         # @File : 集合的方法.py
 7         se=set('abc')  8         se1=frozenset('acfg')  9         se.update('ag',[1,2,354]) 10         se.add((23,456,777)) 11  se.pop() 12 
13         print(se) 14         print(se1) 15         print('#################################') 16 
17         並集=se|se1 18         print(並集) 19         print(se.union(se1)) 20 
21         交集=se&se1 22         print(交集) 23         print(se.intersection(se1)) 24 
25         差集=se-se1 26         print(差集) 27         print(se-se1) 28 
29         交叉補集=se^se1 30         print(交叉補集) 31         print(se.symmetric_difference(se1))
 1 ####九九乘法表
 2     #!/usr/bin/env python
 3     # -*- coding: utf-8 -*-
 4     # @Time : 2018/7/5 13:13
 5     # @Author : chenxiaowei
 6     # @Email : chen1020xiaowei@163.com
 7     # @File : 九九乘法表.py
 8     for i in range(1,10):  9         string=''
10         for j in range(1,i+1): 11             string +=str(j) +'*'+str(i)+'='+str(i*j)+'\t'
12         print(string) 13     
14     print(string,sep='',end='') #sep分割符,end結束 15     
相關文章
相關標籤/搜索