PHPer 浅谈 Python 的数据结构

背景

最近在学习Python,本文主要记录在学习Python时关于数据结构的一些心得。

数据结构

列表

# 列表 demo
shopList = ['apple', 'mango', 'carrot', 'banana']

# len() 计算列表长度
print(len(shopList))

# 遍历列表
for item in shopList:
    print(item, end=' ')

# 追加元素
shopList.append('rice')
print('My shopping list is now', shopList)

# 排序
shopList.sort()
print('My shopping list is now', shopList)

# 删除元素
del shopList[0]
print('My shopping list is now', shopList)

# 列表中插入元素
shopList.insert(1, 'red')
print('My shopping list is now', shopList)

# 列表尾部移除元素
shopList.pop()
print('My shopping list is now', shopList)

Python中的列表类似于PHP中的数值数组

元组

# 元组 demo
zoo = ('python','elephant','penguin')

# len() 计算长度
print(len(zoo))

# 访问元组中的值
print(zoo[1])

元组和列表非常类似,但是元组初始化后值不可以修改。

字典

# 字典的demo
ab = {
    'Swaroop': 'swaroop@swaroopch.com',
    'Larry': 'larry@wall.org',
    'Matsumoto': 'matz@ruby-lang.org',
    'Spammer': 'spammer@hotmail.com'
}

# 访问字典中的元素
print(ab['Swaroop'])
print(ab.get('Swaroop'))

# 赋值
ab['test'] = 'test'
print(ab)

# 删除元素
ab.pop('Larry')
print(ab)

del ab['Spammer']
print(ab)

# 遍历字典
for name, address in ab.items():
    print('Contact {} at {}'.format(name, address))

Python中的字典类似于PHP的关联数组

集合

# 集合demo
bri = set(['bra','rus','ind'])

# 新增元素
bri.add('test')
print(bri)

# 移除元素
bri.remove('rus')
print(bri)

# 判断元素是否存在集合中
print('bra' in bri)

总结

以上列举了Python中四大数据结构的简单用法。如有错误,欢迎指正。

趁着疫情期间,多学习学习~

文章首发地址:https://tsmliyun.github.io/python/PHPer%E6...

本作品采用《CC 协议》,转载必须注明作者和本文链接
不积跬步,无以至千里;不积小流,无以成江海
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!