Python精选200+Tips:1-5

news/2024/9/25 3:39:17/

Pick a flower, You will have a world

  • 001 list
  • 002 set
  • 003 dict
  • 004 tuple
  • 005 str

运行系统:macOS Sonoma 14.6.1
Python编译器:PyCharm 2024.1.4 (Community Edition)
Python版本:3.12

001 list

列表(list)是 Python 中一种用于存储有序元素的数据结构。以下是列表的基本特性:

  • 有序性:列表中的元素保持插入顺序,可以通过索引访问。
  • 可变性:列表是可变的,可以随时修改、添加或删除元素。
  • 可以包含不同类型的元素:列表可以存储不同类型的数据,包括数字、字符串和其他对象。
  1. list的交、并、差
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]# 交集:两个列表中都存在的元素
intersection = list(set(list1) & set(list2))
# 输出: [3, 4]# 并集:两个列表中所有不同元素的集合
union = list(set(list1) | set(list2))
# 输出: [1, 2, 3, 4, 5, 6]# 差集(补集):第一个列表中有而第二个列表中没有的元素
difference = list(set(list1) - set(list2))
# 输出: [1, 2]# 对称差集:两个列表中不重复的元素
symmetric_difference = list(set(list1) ^ set(list2))
# 输出: [1, 2, 5, 6]
  1. 嵌套list展平
# 使用递归
nested_list = [1, [2, [3, 4], 5], 6, [7, 8]]def flatten(nested_list):flat_list = []for item in nested_list:if isinstance(item, list):# 递归flat_list.extend(flatten(item))else:flat_list.append(item)return flat_listflattened = flatten(nested_list)
# 输出: [1, 2, 3, 4, 5, 6, 7, 8]#  生成器递归
def flatten_generator(nested_list):for item in nested_list:if isinstance(item, list):yield from flatten_generator(item)else:yield itemflattened = list(flatten_generator(nested_list))
# 输出: [1, 2, 3, 4, 5, 6, 7, 8]
  1. 遍历多list
list1 = ['a', 'b', 'c']
list2 = [1, 2, 3]
for index, (char, number) in enumerate(zip(list1, list2)):print(index, char, number)# 输出
# 0 a 1
# 1 b 2
# 2 c 3
  1. list拷贝
list1 = ['a', 'b', 'd']
list1_copy = list1  # 浅拷贝,和原list一样
list_deepcopy = list1.copy()  # 深拷贝,和原list独立。等价于list_deepcopy = list1[:]
list1[-1] = 88# 输出
print(list1) # ['a', 'b', 88]
print(list1_copy) # ['a', 'b', 88]
print(list_deepcopy) # ['a', 'b', 'd']
  1. 条件筛选
# 有else,for写后面
numbers = [-10, -5, 0, 5, 10]
result = ["Negative" if num < 0 else "Zero" if num == 0 else "Positive" for num in numbers]print(result)
# 输出 ['Negative', 'Negative', 'Zero', 'Positive', 'Positive']# 没有else,for写前面
def is_prime(n):if n <= 1:return Falsefor i in range(2, int(n ** 0.5) + 1):if n % i == 0:return Falsereturn Truenumbers = [1, 2, 3, 4, 5, 11, 12, 13, 14, 15, 17, 18, 19, 20]
filtered_numbers = [num for num in numbers if num > 10 and is_prime(num)]print(filtered_numbers)
# 输出 [11, 13, 17, 19]

http://www.ppmy.cn/news/1519699.html

相关文章

前端框架大比拼:谁才是未来的主流选择?

前端框架包括Bootstrap、React、Vue.js等。在现代 Web 开发中&#xff0c;使用前端框架可以显著提升开发效率和页面性能&#xff0c;并简化响应式设计的实现过程。不同的框架提供了不同的功能和优势&#xff0c;因此选择适合项目需求的框架至关重要。下面将详细探讨这些主流前端…

Javascript实现笛卡儿积算法

在根据商品属性计算SKU时&#xff0c;通常会对商品不同选项的不同属性进行笛卡儿积运算。 function cartesian(elements) {if (!Array.isArray(elements))throw new TypeError();var end elements.length - 1,result [];function addTo(curr, start) {var first elements[s…

AI:引领未来的科技浪潮

《AI&#xff1a;引领未来的科技浪潮》 在科技飞速发展的今天&#xff0c;人工智能&#xff08;AI&#xff09;无疑是最耀眼的明星之一。它正以惊人的速度改变着我们的生活、工作和社会&#xff0c;成为推动人类进步的强大动力。 人工智能的发展历程可谓波澜壮阔。早在 20 世…

『功能项目』怪物受击后显示受击状态UI【12】

本专栏每10章会做一次项目优化&#xff0c;但不影响具体功能 我们可以打开优化前的项目10也可以打开优化后的项目11 双击King或者怪物熊预制体 - 进入预制体空间后创建一个Image改名为StateUI01 代表第一个受击状态 修改Canvas的渲染模式 - 改为世界WorldSpace 调节Image&…

心理咨询展示型网站渠道拓展服务

心理问题长期以来都受到关注&#xff0c;每个城市里也都有相关服务商家&#xff0c;除了进店外&#xff0c;线上也可以开展咨询服务&#xff0c;对需求者来说需要找到靠谱的品牌&#xff0c;而商家也需要触达到更多客户获取转化。 网站是品牌线上工具&#xff0c;利于商家通过…

英伟达最新论文解析:剪枝与知识蒸馏 —— 可穿戴AI时代即将到来

最近&#xff0c;英伟达的研究团队发布了一篇名为《通过剪枝和知识蒸馏压缩语言模型》的论文&#xff0c;深入探讨了如何在保持模型性能的前提下&#xff0c;有效压缩大语言模型&#xff08;LLM&#xff09;。 这篇论文为AI领域带来了新的突破&#xff0c;提出了多种创新技术&a…

vue按钮弹框

在Vue中实现按钮点击后弹出对话框&#xff08;弹框&#xff09;的功能&#xff0c;通常可以使用一些Vue的UI组件库&#xff0c;如Element UI、Vuetify、BootstrapVue等&#xff0c;这些库提供了丰富的组件&#xff0c;包括对话框&#xff08;Dialog&#xff09;、模态框&#x…

Python爬虫所需的技术及其原理(简单易懂)

导言 随着互联网的发展&#xff0c;大量的数据被存储在网络上&#xff0c;而我们需要从中获取有用的信息。Python作为一种功能强大且易于学习的编程语言&#xff0c;被广泛用于网络爬虫的开发。本文将详细介绍Python爬虫所需的技术及其原理&#xff0c;并提供相关的代码案例。…