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()#设置默认值
  

  字典的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)#父集


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

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

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

  ^ 交叉补集
  se^se1
  se.symmetric_difference(se1)

####数据类型需掌握的方法######


# 一、数字
  强制转换
  int() float()、

# 二、字符串
  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)

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

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

# 四、元组
  tu=(1,2,3,4,'f',)
  一级元素不可更改/删除/增加
  索引、切片、循环

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

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

# 六、布尔值
  0 1
  bool(...)
  None "" () [] {} 0 ==> False

 1 #!/usr/bin/env python
 2     # -*- coding: utf-8 -*-
 3     # @Time    : 2018/7/5 10:13
 4     # @Author  : chenxiaowei
 5     # @Email   : [email protected]
 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   : [email protected]
 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   : [email protected]
 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     

 

猜你喜欢

转载自www.cnblogs.com/chenxiaoweiworkinghard/p/9271524.html