Python训练营笔记 数据结构大汇总 Day5

简介: 学习笔记 - 天池龙珠计划 - Python 训练营 Task2 Day5(字符串、字典)

天池龙珠计划 Python训练营

所记录的知识点

  1. isnumeric
  2. ljust rjust
  3. split splitlines
  4. maketrans translate
  5. 不可变类型 与 hash
  6. 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


欢迎各位同学一起来交流学习心得!

目录
相关文章
|
27天前
|
算法 开发者 计算机视觉
燃爆全场!Python并查集:数据结构界的网红,让你的代码炫酷无比!
在编程的世界里,总有一些数据结构以其独特的魅力和高效的性能脱颖而出,成为众多开发者追捧的“网红”。今天,我们要介绍的这位明星,就是Python中的并查集(Union-Find)——它不仅在解决特定问题上大放异彩,更以其优雅的设计和强大的功能,让你的代码炫酷无比,燃爆全场!
28 0
|
18天前
|
搜索推荐 Python
Leecode 101刷题笔记之第五章:和你一起你轻松刷题(Python)
这篇文章是关于LeetCode第101章的刷题笔记,涵盖了多种排序算法的Python实现和两个中等难度的编程练习题的解法。
16 3
|
22天前
|
Python
Python 中常见的数据结构(二)
Python 中常见的数据结构(二)
16 4
|
22天前
|
存储 索引 Python
Python 中常见的数据结构(一)
Python 中常见的数据结构(一)
30 3
|
22天前
|
开发者 Python
Python 常用的数据结构
Python 常用的数据结构
20 3
|
24天前
|
存储 开发工具 Python
【Python项目】外星人入侵项目笔记
【Python项目】外星人入侵项目笔记
33 3
|
24天前
|
存储 Python
【免费分享编程笔记】Python学习笔记(二)
【免费分享编程笔记】Python学习笔记(二)
38 0
【免费分享编程笔记】Python学习笔记(二)
|
2月前
|
存储 索引 Python
Python常用数据结构——集合
Python常用数据结构——集合
41 3
|
15天前
|
存储 索引 Python
python数据结构之列表详解
列表是Python中极为灵活和强大的数据结构,适合于存储和操作有序数据集合。掌握其基本操作和高级特性对于编写高效、清晰的Python代码至关重要。通过本回答,希望能帮助你全面理解Python列表的使用方法,从而在实际编程中更加游刃有余。
13 0
|
18天前
|
算法 C++ Python
Leecode 101刷题笔记之第四章:和你一起你轻松刷题(Python)
这篇博客是关于LeetCode上使用Python语言解决二分查找问题的刷题笔记,涵盖了从基础到进阶难度的多个题目及其解法。
12 0