天池龙珠计划 Python训练营
所记录的知识点
- isnumeric
- ljust rjust
- split splitlines
- maketrans translate
- 不可变类型 与 hash
- dict(mapping) 与 dict.items
1、isnumeric
isnumeric:判断字符串中是否只包含数字
In [3]: '123124'.isnumeric()
Out[3]: True
In [4]: '123124a'.isnumeric()
Out[4]: False
In [5]: '123124..'.isnumeric()
Out[5]: False
In [6]: help(str().isnumeric)
Help on built-in function isnumeric:
isnumeric() method of builtins.str instance
Return True if the string is a numeric string, False otherwise.
A string is numeric if all characters in the string are numeric and there is at
least one character in the string.
2、ljust rjust
ljust:左对齐,填充右边
rjust:右对齐,填充左边
In [9]: '1111'.ljust(8,'0')
Out[9]: '11110000'
In [10]: '1111'.rjust(8,'0')
Out[10]: '00001111'
In [11]: help(str().ljust)
Help on built-in function ljust:
ljust(width, fillchar=' ', /) method of builtins.str instance
Return a left-justified string of length width.
Padding is done using the specified fill character (default is a space).
In [4]: help(str().rjust)
Help on built-in function rjust:
rjust(width, fillchar=' ', /) method of builtins.str instance
Return a right-justified string of length width.
Padding is done using the specified fill character (default is a space).
3、split splitlines
split:分割两次,将三个部分变量分别保存
split splitlines:去除换行符
In [16]: url="www.baidu.com"
In [17]: a,b,*rest = url.split('.',2)
In [18]: a
Out[18]: 'www'
In [19]: b
Out[19]: 'baidu'
In [20]: rest
Out[20]: ['com']
In [21]:
In [21]:
In [21]: c = '''hello
...: world
...: ha
...: ha
...: ha'''
In [22]: c.split("\n")
Out[22]: ['hello', 'world', 'ha', 'ha', 'ha']
In [23]: c.splitlines()
Out[23]: ['hello', 'world', 'ha', 'ha', 'ha']
In [24]: c.splitlines(False)
Out[24]: ['hello', 'world', 'ha', 'ha', 'ha']
In [25]: c.splitlines(True)
Out[25]: ['hello\n', 'world\n', 'ha\n', 'ha\n', 'ha']
In [26]: help(str().split)
Help on built-in function split:
split(sep=None, maxsplit=-1) method of builtins.str instance
Return a list of the words in the string, using sep as the delimiter string.
sep
The delimiter according which to split the string.
None (the default value) means split according to any whitespace,
and discard empty strings from the result.
maxsplit
Maximum number of splits to do.
-1 (the default value) means no limit.
In [27]: help(str().splitlines)
Help on built-in function splitlines:
splitlines(keepends=False) method of builtins.str instance
Return a list of the lines in the string, breaking at line boundaries.
Line breaks are not included in the resulting list unless keepends is given and
true.
4、maketrans translate
maketrans:创建字符映射转换表
translate:根据字符映射转换表,转换字符串中的字符元素
In [6]: table = str.maketrans("hd","az")
In [7]: table
Out[7]: {104: 97, 100: 122}
In [8]: "hello world".translate(table)
Out[8]: 'aello worlz'
In [9]: help(str().maketrans)
Help on built-in function maketrans:
maketrans(...)
Return a translation table usable for str.translate().
If there is only one argument, it must be a dictionary mapping Unicode
ordinals (integers) or characters to Unicode ordinals, strings or None.
Character keys will be then converted to ordinals.
If there are two arguments, they must be strings of equal length, and
in the resulting dictionary, each character in x will be mapped to the
character at the same position in y. If there is a third argument, it
must be a string, whose characters will be mapped to None in the result.
In [10]: help(str().translate)
Help on built-in function translate:
translate(table, /) method of builtins.str instance
Replace each character in the string using the given translation table.
table
Translation table, which must be a mapping of Unicode ordinals to
Unicode ordinals, strings, or None.
The table must implement lookup/indexing via __getitem__, for instance a
dictionary or list. If this operation raises LookupError, the character is
left untouched. Characters mapped to None are deleted.
5、不可变类型 与 hash
用hash(x),只要不报错,证明X可被哈希,即为不可变类型
数值、字符、元组 都能被hash,因此它们是不可变类型
列表、集合、字典不能被hash,因此它们是可变类型
In [15]: hash(1),hash("hello"),hash((1,2,3))
Out[15]: (1, 5141752251584472646, 2528502973977326415)
In [16]: hash(list())
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-16-3e2eb619e4e4> in <module>
----> 1 hash(list())
TypeError: unhashable type: 'list'
In [17]: hash(set())
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-17-2699417ebeac> in <module>
----> 1 hash(set())
TypeError: unhashable type: 'set'
In [18]: hash(dict())
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-18-7762fff637c6> in <module>
----> 1 hash(dict())
TypeError: unhashable type: 'dict'
In [19]: help(hash)
Help on built-in function hash in module builtins:
hash(obj, /)
Return the hash value for the given object.
Two objects that compare equal must also have the same hash value, but the
reverse is not necessarily true.
6、dict(mapping) 与 dict.items
dict(mapping) 方式创建字典
In [23]: a= [('apple',123),('banana',456)]
In [24]: dict(a)
Out[24]: {'apple': 123, 'banana': 456}
In [25]: b=(('peach',789),('cherry',901))
In [26]: b
Out[26]: (('peach', 789), ('cherry', 901))
In [27]: dict(b)
Out[27]: {'peach': 789, 'cherry': 901}
In [28]: dict(b).items()
Out[28]: dict_items([('peach', 789), ('cherry', 901)])
In [29]: help(dict().items)
Help on built-in function items:
items(...) method of builtins.dict instance
D.items() -> a set-like object providing a view on D's items
欢迎各位同学一起来交流学习心得!