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


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

目录
相关文章
|
2天前
|
存储 缓存 监控
局域网屏幕监控系统中的Python数据结构与算法实现
局域网屏幕监控系统用于实时捕获和监控局域网内多台设备的屏幕内容。本文介绍了一种基于Python双端队列(Deque)实现的滑动窗口数据缓存机制,以处理连续的屏幕帧数据流。通过固定长度的窗口,高效增删数据,确保低延迟显示和存储。该算法适用于数据压缩、异常检测等场景,保证系统在高负载下稳定运行。 本文转载自:https://www.vipshare.com
90 66
|
29天前
|
存储 索引 Python
Python编程数据结构的深入理解
深入理解 Python 中的数据结构是提高编程能力的重要途径。通过合理选择和使用数据结构,可以提高程序的效率和质量
142 59
|
29天前
|
存储 开发者 Python
Python 中的数据结构与其他编程语言数据结构的区别
不同编程语言都有其设计理念和应用场景,开发者需要根据具体需求和语言特点来选择合适的数据结构
|
6天前
|
存储 运维 监控
探索局域网电脑监控软件:Python算法与数据结构的巧妙结合
在数字化时代,局域网电脑监控软件成为企业管理和IT运维的重要工具,确保数据安全和网络稳定。本文探讨其背后的关键技术——Python中的算法与数据结构,如字典用于高效存储设备信息,以及数据收集、异常检测和聚合算法提升监控效率。通过Python代码示例,展示了如何实现基本监控功能,帮助读者理解其工作原理并激发技术兴趣。
46 20
|
29天前
|
存储 开发者 索引
Python 中常见的数据结构
这些数据结构各有特点和适用场景,在不同的编程任务中发挥着重要作用。开发者需要根据具体需求选择合适的数据结构,以提高程序的效率和性能
|
29天前
|
存储 算法 搜索推荐
Python 中数据结构和算法的关系
数据结构是算法的载体,算法是对数据结构的操作和运用。它们共同构成了计算机程序的核心,对于提高程序的质量和性能具有至关重要的作用
|
29天前
|
数据采集 存储 算法
Python 中的数据结构和算法优化策略
Python中的数据结构和算法如何进行优化?
|
2月前
|
搜索推荐 Python
Leecode 101刷题笔记之第五章:和你一起你轻松刷题(Python)
这篇文章是关于LeetCode第101章的刷题笔记,涵盖了多种排序算法的Python实现和两个中等难度的编程练习题的解法。
27 3
|
2月前
|
Python
Python 中常见的数据结构(二)
Python 中常见的数据结构(二)
26 4
|
2月前
|
存储 索引 Python
Python 中常见的数据结构(一)
Python 中常见的数据结构(一)
45 3

热门文章

最新文章