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发布软件
这个抖音批量发布工具包含三个主要模块:主上传程序、配置文件和视频预处理工具。主程序
|
2月前
|
存储 监控 安全
企业上网监控系统中红黑树数据结构的 Python 算法实现与应用研究
企业上网监控系统需高效处理海量数据,传统数据结构存在性能瓶颈。红黑树通过自平衡机制,确保查找、插入、删除操作的时间复杂度稳定在 O(log n),适用于网络记录存储、设备信息维护及安全事件排序等场景。本文分析红黑树的理论基础、应用场景及 Python 实现,并探讨其在企业监控系统中的实践价值,提升系统性能与稳定性。
51 1
|
2月前
|
API 数据安全/隐私保护 Python
小红书批量发布协议, 抖音自动批量发布软件脚本,笔记作品视频自动发布工具【python】
这个工具框架包含了小红书和抖音的批量发布功能,支持图片和视频处理、定时发布等功能
|
2月前
|
Web App开发 数据安全/隐私保护 Python
抖音快手小红书哔哩哔哩,批量发布作品笔记视频工具,自动发布作品上传笔记视频【python】
这个工具实现了四大平台的视频批量上传功能,包含完整的异常处理和日志记录。使用时需要配置
|
2月前
|
存储 JSON API
小红书批量发布笔记工具,小红书批量上传软件,python框架分享
这个框架包含了配置文件、工具函数、API封装和主程序四个模块。使用时需要先配置账号信息,
|
4月前
|
人工智能 Ruby Python
python__init__方法笔记
本文总结了Python中`__init__`方法的使用要点,包括子类对父类构造方法的调用规则。当子类未重写`__init__`时,实例化会自动调用父类的构造方法;若重写,则需通过`super()`或直接调用父类名称来显式继承父类初始化逻辑。文中通过具体代码示例展示了不同场景下的行为及输出结果,帮助理解类属性与成员变量的关系,以及如何正确使用`super()`实现构造方法的继承。
150 9
|
5月前
|
数据采集 JSON API
Python 实战:用 API 接口批量抓取小红书笔记评论,解锁数据采集新姿势
小红书作为社交电商的重要平台,其笔记评论蕴含丰富市场洞察与用户反馈。本文介绍的小红书笔记评论API,可获取指定笔记的评论详情(如内容、点赞数等),支持分页与身份认证。开发者可通过HTTP请求提取数据,以JSON格式返回。附Python调用示例代码,帮助快速上手分析用户互动数据,优化品牌策略与用户体验。
|
6月前
|
存储 人工智能 索引
Python数据结构:列表、元组、字典、集合
Python 中的列表、元组、字典和集合是常用数据结构。列表(List)是有序可变集合,支持增删改查操作;元组(Tuple)与列表类似但不可变,适合存储固定数据;字典(Dictionary)以键值对形式存储,无序可变,便于快速查找和修改;集合(Set)为无序不重复集合,支持高效集合运算如并集、交集等。根据需求选择合适的数据结构,可提升代码效率与可读性。
|
5月前
|
数据采集 JSON API
Python 实战!利用 API 接口获取小红书笔记详情的完整攻略
小红书笔记详情API接口帮助商家和数据分析人员获取笔记的详细信息,如标题、内容、作者信息、点赞数等,支持市场趋势与用户反馈分析。接口通过HTTP GET/POST方式请求,需提供`note_id`和`access_token`参数,返回JSON格式数据。以下是Python示例代码,展示如何调用该接口获取数据。使用时请遵守平台规范与法律法规。
|
9月前
|
存储 缓存 监控
局域网屏幕监控系统中的Python数据结构与算法实现
局域网屏幕监控系统用于实时捕获和监控局域网内多台设备的屏幕内容。本文介绍了一种基于Python双端队列(Deque)实现的滑动窗口数据缓存机制,以处理连续的屏幕帧数据流。通过固定长度的窗口,高效增删数据,确保低延迟显示和存储。该算法适用于数据压缩、异常检测等场景,保证系统在高负载下稳定运行。 本文转载自:https://www.vipshare.com
237 66

推荐镜像

更多