python基本数据类型-python3.0学习笔记
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python基本数据类型-python3.0学习笔记相关的知识,希望对你有一定的参考价值。
python基本数据类型
序列类型的自带方法
1.列表的常用方法
2.元祖的常用方法
3.字符串的常用方法
1.列表常用的方法
L.append(obj) #在列表末尾添加新的对象
L.clear() #清空列表
L.copy() #复制列表,不是同一个对象,内容相同,有返回值。id不同(内存中的地址不同)
L.count(obj) #统计某个元素在列表中出现的次数
L.extend(obj) #用obj扩展原来的列表
L.index(obj) #默认值返回这个元素最先出现的索引位置;需要出现下一个位置的,添加可选参数start,stop。
Linsert(index,obj) #插入元素,可以指定位置
L.pop(index) #出栈,可以指定位置。index,默认是L[-1],按索引删除
L.remove(obj) #移除指定元素从左边开始第一个
L.reverse() #反向列表元素
L.sort() #对列表进行排序(默认ACSII)。列表中的元素要类型相同(key=len)
内置函数:
sorted()和reversed()
>>> li = [1,2,3]
>>> dir(li) #查看li列表的属性方法,带下划线的为魔法方法和私有方法,不用管。学习其它的。
[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__‘, ‘__hash__‘, ‘__iadd__‘, ‘__imul__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__reversed__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘append‘, ‘clear‘, ‘copy‘, ‘count‘, ‘extend‘, ‘index‘, ‘insert‘, ‘pop‘, ‘remove‘, ‘reverse‘, ‘sort‘]
#那些方法对列表做了不可描述的方法
>>> help(li) #列出所有方法
>>> help(li.append) #查看li的append的用法
Help on built-in function append:
append(...) method of builtins.list instance
L.append(object) -> None -- append object to end #添加对象返回空值,在原列表的末尾添加
>>> li.append(4)
>>> li
[1, 2, 3, 4]
>>> li.clear()
>>> li
[]
>>> li = [1,2,3]
>>> id(li)
54036680
>>> li2 = li.copy()
>>> li2
[1, 2, 3]
>>> id(li2)
54636232
>>> li.count(2)
1
>>> li.append(2)
>>> li.count(2)
2
>>> li
[1, 2, 3, 2]
>>> li.extend("456")
>>> li
[1, 2, 3, 2, ‘4‘, ‘5‘, ‘6‘]
>>> li = [1,2,3]
>>> li.extend([4,5,6])
>>> li
[1, 2, 3, 4, 5, 6]
>>> li.extend((7,8))
>>> li
[1, 2, 3, 4, 5, 6, 7, 8]
>>> li
[1, 2, 3, 4, 5, 6, 7, 8]
>>> li.index(7)
6
>>> li = [‘a‘,‘b‘,‘c‘,‘d‘]
>>> li.index(‘c‘)
2
>>> help(li.index)
Help on built-in function index:
index(...) method of builtins.list instance
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
>>> li = [‘a‘,‘b‘,‘c‘,‘d‘,‘c‘]
>>> li.index(‘c‘)
2
>>> li.index(‘c‘,3)
4
>>> li.index(‘c‘,3,5)
4
>>> li.index(‘c‘,3,6)
4
>>> help(li.insert)
Help on built-in function insert:
insert(...) method of builtins.list instance
L.insert(index, object) -- insert object before index
>>> li
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘c‘]
>>> li.insert(1,‘lucky‘)
>>> li
[‘a‘, ‘lucky‘, ‘b‘, ‘c‘, ‘d‘, ‘c‘]
>>> li.insert(-1,‘qq‘)
>>> li
[‘a‘, ‘lucky‘, ‘b‘, ‘c‘, ‘d‘, ‘qq‘, ‘c‘]
>>> help(li.pop)
Help on built-in function pop:
pop(...) method of builtins.list instance
L.pop([index]) -> item -- remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
>>> li.pop(1)
‘lucky‘
>>> li
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘qq‘, ‘c‘]
>>> li.pop()
‘c‘
>>> li
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘qq‘]
>>> help(li.remove)
Help on built-in function remove:
remove(...) method of builtins.list instance
L.remove(value) -> None -- remove first occurrence of value.
Raises ValueError if the value is not present.
>>> li
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘qq‘]
>>> li.remove(‘c‘)
>>> li
[‘a‘, ‘b‘, ‘d‘, ‘qq‘]
>>> help(li.reverse)
Help on built-in function reverse:
reverse(...) method of builtins.list instance
L.reverse() -- reverse *IN PLACE*
>>> li.reverse()
>>> li
[‘c‘, ‘d‘, ‘b‘, ‘a‘]
>>> help(li.sort)
Help on built-in function sort:
sort(...) method of builtins.list instance
L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
>>> li.sort()
>>> li
[‘a‘, ‘b‘, ‘c‘, ‘d‘]
>>> li = [‘a‘,‘c‘,‘ab‘]
>>> li.sort()
>>> li
[‘a‘, ‘ab‘, ‘c‘]
>>> li.sort(key=len) #按照长度相等的排序然后再按不等的排序
>>> li
[‘a‘, ‘c‘, ‘ab‘]
>>> li.sort(key=len,reverse=True)
>>> li
[‘ab‘, ‘a‘, ‘c‘]
函数reversed和sorted:
>>> reversed(li) #返回一个迭代器
<list_reverseiterator object at 0x00000000033C2978>
>>> list(reversed(li))
[‘c‘, ‘a‘, ‘ab‘]
>>> sorted(li)
[‘a‘, ‘ab‘, ‘c‘]
可变特点:
>>> li = [2,3]
>>> id(li)
53901000
>>> li.append(4)
>>> id(li)
53901000
>>> li.pop()
4
>>> id(li)
53901000
2.元祖的常用方法
count(obj) #统计某个元素在元祖中出现的次数
index(obj) #从列表中找到某个值第一个匹配项的索引位置
注意:生命只有一个元素的元组时要加逗号
特点:不可变
举例:
>>> tu = 1,2
>>> dir(tu)
[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getnewargs__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘count‘, ‘index‘]
>>> tu.count(1)
1
>>> tu.count(3)
0
>>> help(tu.index)
Help on built-in function index:
index(...) method of builtins.tuple instance
T.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
>>> tu.index(1)
0
3.字符串的常用方法
s.count(x) #返回字符串x在s中出现的次数,可带参数
s.endswith(x) #如果字符串s以x结尾,返回True
s.startswith(x) #如果字符串s以x开头,返回True
s.find(x) #返回字符串中出现x的最左端字符的索引值,如不在则返回-1
s.index(x) #返回字符串中出现x的最左端的索引值,如不在则抛出valueError异常
s.isalpha() #测试是否全为字母,都是则True;否则False
s.isdigit() #测试是否全是数字,都是则True;否则False
s.islower() #测试全是小写
s.isupper() #测试全是大写
s.lower() #将字符串转为小写
s.upper() #将字符串转为大写
s.replace(x,y) #字串替换,在字符串s中出现字符串x的任意位置都用y进行替换
s.split() #返回一系列用空格分割的字符串列表
s.split(a,b) #a,b为可选参数,a是将要分割的字符串,b是说明最多分割几个。
举例:
>>> s=‘‘
>>> dir(s)
[‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getnewargs__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mod__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘capitalize‘, ‘casefold‘, ‘center‘, ‘count‘, ‘encode‘, ‘endswith‘, ‘expandtabs‘, ‘find‘, ‘format‘, ‘format_map‘, ‘index‘, ‘isalnum‘, ‘isalpha‘, ‘isdecimal‘, ‘isdigit‘, ‘isidentifier‘, ‘islower‘, ‘isnumeric‘, ‘isprintable‘, ‘isspace‘, ‘istitle‘, ‘isupper‘, ‘join‘, ‘ljust‘, ‘lower‘, ‘lstrip‘, ‘maketrans‘, ‘partition‘, ‘replace‘, ‘rfind‘, ‘rindex‘, ‘rjust‘, ‘rpartition‘, ‘rsplit‘, ‘rstrip‘, ‘split‘, ‘splitlines‘, ‘startswith‘, ‘strip‘, ‘swapcase‘, ‘title‘, ‘translate‘, ‘upper‘, ‘zfill‘]
>>> s.count(‘3‘)
0
>>> help(s.endswith)
Help on built-in function endswith:
endswith(...) method of builtins.str instance
S.endswith(suffix[, start[, end]]) -> bool
Return True if S ends with the specified suffix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
suffix can also be a tuple of strings to try.
>>> s=‘abcde‘
>>> s.endswith(‘a‘)
False
>>> s.endswith(‘e‘)
True
>>> help(s.startswith)
Help on built-in function startswith:
startswith(...) method of builtins.str instance
S.startswith(prefix[, start[, end]]) -> bool
Return True if S starts with the specified prefix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
prefix can also be a tuple of strings to try.
>>> s.startswith(‘a‘)
True
>>> help(s.find)
Help on built-in function find:
find(...) method of builtins.str instance
S.find(sub[, start[, end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
>>> s.find(‘v‘) #不存在的值
-1
>>> s.find(‘b‘)
1
>>> help(s.index)
Help on built-in function index:
index(...) method of builtins.str instance
S.index(sub[, start[, end]]) -> int
Like S.find() but raise ValueError when the substring is not found.
>>> s.index(‘e‘) #默认返回最先出现的位置
4
>>> s=‘abcde‘
>>> s.isalpha() #测试全是字母
True
>>> s.isdigit() #测试全是数字,只能测试正整数,负数返回-1
False
>>> s.islower() #测试全是小写
True
>>> s.isupper() #测试全是大写
False
>>> s1 = ‘12AA‘
>>> s1.isupper()
True
>>> s1.upper() #全部改成大写
‘12AA‘
>>> s1.lower() #全部改成小写
‘12aa
>>> help(s.replace)
Help on built-in function replace:
replace(...) method of builtins.str instance
S.replace(old, new[, count]) -> str
Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
>>> s1.replace(‘12‘,‘bb‘) #将旧元素替换成新的元素。替换次数的参数可选
‘bbAA‘
>>> s1.replace(‘A‘,‘b‘,1)
‘12bA‘
>>> help(s.split)
Help on built-in function split:
split(...) method of builtins.str instance
S.split(sep=None, maxsplit=-1) -> list of strings
Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.
>>> s.split(sep=‘o‘)
[‘i l‘, ‘ve pyth‘, ‘n‘]
>>> s.split(maxsplit=1)
[‘i‘, ‘love python‘]
>>> s.split(‘1‘)
[‘i love python‘]
>>> s.split(sep=‘o‘,maxsplit=1)
[‘i l‘, ‘ve python‘]
>>> s=‘123‘
>>> s.split(sep=‘1‘)
[‘‘, ‘23‘]
以上是关于python基本数据类型-python3.0学习笔记的主要内容,如果未能解决你的问题,请参考以下文章