基础2

本节内容

  1. 列表、元组操作
  2. 字符串操作
  3. 字典操作
  4. 集合操作
  5. 文件操作

  1. 字符编码与转码 

1. 列表、元组操作

列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作

定义列表

>>> argv = [1,2,3,4]

通过下标访问列表中的元素,下标从0开始计数
>>> argv[0]
1
>>> argv[1]
2
>>> argv[2]
3
>>> argv[3]
4

切片:取多个元素

>>> names = ['1','2','3','4','5']
>>> names[1:4] #取下标1至下标4之间的数字,包括1,不包括4
['2', '3', '4']
>>> names[1:-1]#取下标1至-1的值,不包括-1
['2', '3', '4']
>>> names[0:3]
['1', '2', '3']
>>> names[:3]
['1', '2', '3']
>>> names[3:]  #如果想取最后一个,必须不能写-1,只能这么写
['4', '5']
>>> names[3:-1]
['4']
>>> names[0::2]#后面的2是代表,每隔一个元素,就取一个
['1', '3', '5']
>>> names[::2]
['1', '3', '5']


追加

>>> names
['1', '2', '3', '4', '5']
>>> names.append("88")
>>> names
['1', '2', '3', '4', '5', '88']

插入

>>> names
['1', '2', '3', '4', '5', '88']
>>> names.insert(0,"-1")
>>> names
['-1', '1', '2', '3', '4', '5', '88']
>>> names.insert(3,"66")
>>> names
['-1', '1', '2', '66', '3', '4', '5', '88']

修改

>>> names
['-1', '1', '2', '66', '3', '4', '5', '88']
>>> names[3] = "55"
>>> names
['-1', '1', '2', '55', '3', '4', '5', '88']

删除

>>> names
['-1', '1', '2', '55', '3', '4', '5', '88']
>>> del names[0]
>>> names
['1', '2', '55', '3', '4', '5', '88']
>>> del names[3]
>>> names
['1', '2', '55', '4', '5', '88']
>>> names.remove("4") #删除指定元素
>>> names
['1', '2', '55', '5', '88']
>>> names.pop()
'88'
>>> names
['1', '2', '55', '5']

扩展

>>> names
['1', '2', '55', '5']
>>> b=["doc","jmg","jbu"]
>>> names.extend(b)
>>> names
['1', '2', '55', '5', 'doc', 'jmg', 'jbu']

拷贝

>>> names
['1', '2', '55', '5', 'doc', 'jmg', 'jbu']
>>> name_copy = names.copy()
>>> name_copy
['1', '2', '55', '5', 'doc', 'jmg', 'jbu']

统计

>>> names
['1', '2', '55', '5', 'doc', 'jmg', 'jbu', 'doc']
>>> names.count("doc")
2

排序&翻转

>>> names
['1', '2', '55', '5', 'doc', 'jmg', 'jbu', 'doc']
>>> names.sort()
>>> names
['1', '2', '5', '55', 'doc', 'doc', 'jbu', 'jmg']
>>> names.reverse()#反转
>>> names
['jmg', 'jbu', 'doc', 'doc', '55', '5', '2', '1']

获取下标

>>> names
['jmg', 'jbu', 'doc', 'doc', '55', '5', '2', '1']
>>> names.index("doc")
2#只返回找到的第一个下标

元组
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表
>>> names = ("1","2","3")

它只有2个方法,一个是count,一个是index,完毕。

2. 字符串操作   

特性:不可修改 

>>> name = "my \tname is {old} and i am {day} year"
>>> print(name.capitalize())#开头字母大写
My name is {old} and i am {day} year
>>> print(name.count("a"))#统计多少个
5
>>> print(name.center(50,"+"))#居中不够就填充
++++++my name is {old} and i am {day} year+++++++
>>> print(name.endswith("ar"))#以什么结尾
True
>>> print(name.expandtabs(tabsize=40))#设置制表符大小
my                                      name is {old} and i am {day} year
>>> print(name[name.find("name"):])#查找
name is {old} and i am {day} year
>>> print(name.format(old='boy',day='11'))#格式化
my name is boy and i am 11 year
>>> name
'my \tname is {old} and i am {day} year'
>>> print(name.format_map( {'old':'goril','day':13}))#字典格式化
my name is goril and i am 13 year
>>> print("123".isalnum())#是否数子
True
>>> print('abA'.isalpha())#是否是字母
True
>>> print('1A'.isdecimal())#是否是十进制数
False
>>> print('1A'.isdigit())#是否是数字
False
>>> print('a_1aA'.isidentifier())#判读是不是一个合法的标识符
True
>>> print('33A'.isnumeric())#是否是数字
False
>>> print('My Name Is  '.istitle())#标题
True
>>> print('My Name Is  '.isprintable())
True
>>> print('My Name Is  '.isupper())#大学
False
>>> print('+'.join(['1','2','3']))
1+2+3
>>> print(name.ljust(50,"-"))#左对齐
my name is {old} and i am {day} year-------------
>>> print(name.rjust(50,'+'))#右对齐
+++++++++++++my name is {old} and i am {day} year
>>> print('Abcde'.lower())#小写
abcde
>>> print('Abcde'.upper())#大写
ABCDE
>>> print('\nAbece'.lstrip())#去左边空格
Abece
>>> print('Abced    '.rstrip())#去右边空格
Abced
>>> print('   abcde\n'.strip())#去两边
abcde
>>> p=str.maketrans('abcdef','123$@4')
>>> print('year li'.translate(p))
y@1r li
>>> print('adcd lil'.replace('l','L',1))#替换
adcd Lil
>>> print('abcd lil'.rfind('l'))
7
>>> print('1-2-3-4'.split('\n'))
['1-2-3-4']
>>> print('1+2+3+4'.split('\n'))
['1+2+3+4']
>>> print('abcd li'.swapcase())#大小写互换
ABCD LI
>>> print('lex ai'.title())
Lex Ai
>>> print('lex ai'.zfill(50))
00000000000000000000000000000000000000000000lex ai

format:

>>> msg = 'my name is {},and age is {}'
>>> msg.format('abc',12)
'my name is abc,and age is 12'
>>> msg = 'my name is {1},and age is {0}'
>>> msg.format('abc',12)
'my name is 12,and age is abc'
>>> msg = "my name is {name},and age is {age}"
>>> msg.format(age=22,name='abc')
'my name is abc,and age is 22'

maketrans:

>>> intab = 'aeiou'
>>> outtab = '12345'
>>> trantab=str.maketrans(intab,outtab)
>>> str = 'this is string example.....wow!!!'
>>> str.translate(trantab)
'th3s 3s str3ng 2x1mpl2.....w4w!!!'

3. 字典操作

字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划、字母来查对应页的详细内容。

>>> info = {
'stu01':'aa',
'stu02':'bb',
'stu03':'cc'
}

字典的特性:

  • dict是无序的
  • key必须是唯一的,so 天生去重
增加

>>> info['stu04']='苍老师'
>>> info
{'stu01': 'aa', 'stu02': 'bb', 'stu03': 'cc', 'stu04': '苍老师'}

修改

>>> info['stu01']='$$'
>>> info
{'stu01': '$$', 'stu02': 'bb', 'stu03': 'cc', 'stu04': '苍老师'}

删除

>>> del info['stu01']
>>> info
{'stu02': 'bb', 'stu03': 'cc', 'stu04': '苍老师'}

>>> info.popitem()#随机删除
('stu04', '苍老师')

>>> info.pop('stu02')#标准删除
'bb'

查找

>>> info
{'stu01': 'aa', 'stu02': 'bb', 'stu03': 'cc'}
>>> 'stu01' in info#标准用法
True
>>> info.get('stu02')#获取
'bb'
>>> info['stu02']
'bb'
>>> info['stu04']#如果key不存在,就会报错,get不会,不存在只返回None
Traceback (most recent call last):
  File "<pyshell#91>", line 1, in <module>
    info['stu04']
KeyError: 'stu04'
>>> info.get('stu05')
>>> print(info.get('stu05'))
None


多级字典嵌套及操作

 >>> age = {
    "北京":{
        "1": ["世界最大的","质量一般"],
        "2": ["也很大","高点"],
        "3": ["图片很多","更新慢"],
        "4":["真的很高","全部收费"]
    },
    "上海":{
        "a":["质量不清楚","收费的"]
    },
    "广东":{
        "520":["真好,好人一生平安","国外,"]
    }
}
>>> age['广东']['520'][0] +=",一路顺风"
>>> print(age['广东']["520"])
['真好,好人一生平安,一路顺风', '国外,']

其它姿势

#values

{'stu01': 'aa', 'stu02': 'bb', 'stu03': 'cc'}
>>> info.values()
dict_values(['aa', 'bb', 'cc'])

#keys

>>> info.keys()
dict_keys(['stu01', 'stu02', 'stu03'])

#setdefault

>>> info.setdefault('stu06','ff')
'ff'
>>> info
{'stu01': 'aa', 'stu02': 'bb', 'stu03': 'cc', 'stu06': 'ff'}
>>> info.setdefault('stu01','ee')
'aa'
>>> info
{'stu01': 'aa', 'stu02': 'bb', 'stu03': 'cc', 'stu06': 'ff'}


#update 
>>> info
{'stu01': 'aa', 'stu02': 'bb', 'stu03': 'cc', 'stu06': 'ff'}
>>> b = {1:2,3:4,"stu05":"苍井空"}
>>> info.update(b)
>>> info
{'stu01': 'aa', 'stu02': 'bb', 'stu03': 'cc', 'stu06': 'ff', 1: 2, 3: 4, 'stu05': '苍井空'}

#items
>>> info.items()
dict_items([('stu01', 'aa'), ('stu02', 'bb'), ('stu03', 'cc'), ('stu06', 'ff'), (1, 2), (3, 4), ('stu05', '苍井空')])

#通过一个列表生成默认dict,有个没办法解释的坑,少用吧这个
>>> dict.fromkeys([1,2,3],'testd')
{1: 'testd', 2: 'testd', 3: 'testd'}


循环dict 

#方法1
for key in info:
	print(key,info[key])
#方法2
>>> dict = dict.fromkeys([1,2,3],'testd')
>>> dict
{1: 'testd', 2: 'testd', 3: 'testd'}
>>> for k,v in dict.items():
print(k,v)



1 testd
2 testd
3 testd

4.集合操作

集合是一个无序的,不重复的数据组合,它的主要作用如下:

  • 去重,把一个列表变成集合,就自动去重了
  • 关系测试,测试两组数据之前的交集、差集、并集等关系
s = set([3,5,9,10])      #创建一个数值集合  
  
t = set("Hello")         #创建一个唯一字符的集合  


a = t | s          # t 和 s的并集  
  
b = t & s          # t 和 s的交集  
  
c = t – s          # 求差集(项在t中,但不在s中)  
  
d = t ^ s          # 对称差集(项在t或s中,但不会同时出现在二者中)  
  
   
  
基本操作:  
  
t.add('x')            # 添加一项  
  
s.update([10,37,42])  # 在s中添加多项  
  
   
  
使用remove()可以删除一项:  
  
t.remove('H')  
  
  
len(s)  
set 的长度  
  
x in s  
测试 x 是否是 s 的成员  
  
x not in s  
测试 x 是否不是 s 的成员  
  
s.issubset(t)  
s <= t  
测试是否 s 中的每一个元素都在 t 中  
  
s.issuperset(t)  
s >= t  
测试是否 t 中的每一个元素都在 s 中  
  
s.union(t)  
s | t  
返回一个新的 set 包含 s 和 t 中的每一个元素  
  
s.intersection(t)  
s & t  
返回一个新的 set 包含 s 和 t 中的公共元素  
  
s.difference(t)  
s - t  
返回一个新的 set 包含 s 中有但是 t 中没有的元素  
  
s.symmetric_difference(t)  
s ^ t  
返回一个新的 set 包含 s 和 t 中不重复的元素  
  
s.copy()  
返回 set “s”的一个浅复制

5. 文件操作

对文件操作流程

  1. 打开文件,得到文件句柄并赋值给一个变量
  2. 通过句柄对文件进行操作
  3. 关闭文件

基本操作  

1
2
3
4
5
6
7
8
=  open ( 'find' #打开文件
first_line  =  f.readline()
print ( 'first line:' ,first_line)  #读一行
print ( '我是分隔线' .center( 50 , '-' ))
data  =  f.read() # 读取剩下的所有内容,文件大时不要用
print (data)  #打印文件
 
f.close()  #关闭文件

打开文件的模式有:

  • r,只读模式(默认)。
  • w,只写模式。【不可读;不存在则创建;存在则删除内容;】
  • a,追加模式。【可读;   不存在则创建;存在则只追加内容;】

"+" 表示可以同时读写某个文件

  • r+,可读写文件。【可读;可写;可追加】
  • w+,写读
  • a+,同a

"U"表示在读取时,可以将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用)

  • rU
  • r+U

"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)

  • rb
  • wb
  • ab

其它语法

复制代码
    def close(self): # real signature unknown; restored from __doc__
        """
        Close the file.
        
        A closed file cannot be used for further I/O operations.  close() may be
        called more than once without error.
        """
        pass

    def fileno(self, *args, **kwargs): # real signature unknown
        """ Return the underlying file descriptor (an integer). """
        pass

    def isatty(self, *args, **kwargs): # real signature unknown
        """ True if the file is connected to a TTY device. """
        pass

    def read(self, size=-1): # known case of _io.FileIO.read
        """
        注意,不一定能全读回来
        Read at most size bytes, returned as bytes.
        
        Only makes one system call, so less data may be returned than requested.
        In non-blocking mode, returns None if no data is available.
        Return an empty bytes object at EOF.
        """
        return ""

    def readable(self, *args, **kwargs): # real signature unknown
        """ True if file was opened in a read mode. """
        pass

    def readall(self, *args, **kwargs): # real signature unknown
        """
        Read all data from the file, returned as bytes.
        
        In non-blocking mode, returns as much as is immediately available,
        or None if no data is available.  Return an empty bytes object at EOF.
        """
        pass

    def readinto(self): # real signature unknown; restored from __doc__
        """ Same as RawIOBase.readinto(). """
        pass #不要用,没人知道它是干嘛用的

    def seek(self, *args, **kwargs): # real signature unknown
        """
        Move to new file position and return the file position.
        
        Argument offset is a byte count.  Optional argument whence defaults to
        SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values
        are SEEK_CUR or 1 (move relative to current position, positive or negative),
        and SEEK_END or 2 (move relative to end of file, usually negative, although
        many platforms allow seeking beyond the end of a file).
        
        Note that not all file objects are seekable.
        """
        pass

    def seekable(self, *args, **kwargs): # real signature unknown
        """ True if file supports random-access. """
        pass

    def tell(self, *args, **kwargs): # real signature unknown
        """
        Current file position.
        
        Can raise OSError for non seekable files.
        """
        pass

    def truncate(self, *args, **kwargs): # real signature unknown
        """
        Truncate the file to at most size bytes and return the truncated size.
        
        Size defaults to the current file position, as returned by tell().
        The current file position is changed to the value of size.
        """
        pass

    def writable(self, *args, **kwargs): # real signature unknown
        """ True if file was opened in a write mode. """
        pass

    def write(self, *args, **kwargs): # real signature unknown
        """
        Write bytes b to file, return number written.
        
        Only makes one system call, so not all of the data may be written.
        The number of bytes actually written is returned.  In non-blocking mode,
        returns None if the write would block.
        """
        pass
复制代码

with语句

为了避免打开文件后忘记关闭,可以通过管理上下文,即:

1
2
3
with  open ( 'log' , 'r' ) as f:
     
     ...

如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。

在Python 2.7 后,with又支持同时对多个文件的上下文进行管理,即:

1
2
with  open ( 'log1' ) as obj1,  open ( 'log2' ) as obj2:
     pass

6. 字符编码与转码

详细文章:

http://www.cnblogs.com/yuanchenqi/articles/5956943.html

http://www.diveintopython3.net/strings.html

需知:

1.在python2默认编码是ASCII, python3里默认是unicode

2.unicode 分为 utf-32(占4个字节),utf-16(占两个字节),utf-8(占1-4个字节), so utf-16就是现在最常用的unicode版本, 不过在文件里存的还是utf-8,因为utf8省空间

3.在py3中encode,在转码的同时还会把string 变成bytes类型,decode在解码的同时还会把bytes变回string

 

 上图仅适用于py2







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值