StackOverflow Python 相关问题精选
# 基础语法
# Python 是否有三元运算符 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# str
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# list
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
def foo(a=[]): a.append(5) return a # out >>> foo() [5] >>> foo() [5, 5] >>> foo() [5, 5, 5] >>> foo() [5, 5, 5, 5] >>> foo() # 改写 def foo(a=None): if a is None: a = list() a.append(5) return a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21# 🔗 (opens new window)
l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] flat_list = [item for sublist in l for item in sublist] print(flat_list)
1
2
3# 🔗 (opens new window)
# 🔗 (opens new window)
tuple(l[i:i+n] for i in xrange(0, len(l), n))
1# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
- 如何将 Python 列表均匀划分? (opens new window)
- join 函数为什么是 string.join(list)而不是 list.join(string) (opens new window)
# dict
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 时间
# 文件
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
#
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 基本数据结构
# 🔗 (opens new window)
list_to_be_sorted = [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}] # >>> [{'name':'Bart', 'age':10}, {'name':'Homer', 'age':39}] newlist = sorted(list_to_be_sorted, key=lambda k: k['name']) # 或者用标准库中的方法: from operator import itemgetter newlist = sorted(list_to_be_sorted, key=itemgetter('name'))
1
2
3
4
5
6# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 进阶函数和类
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 系统
# 数据库
# 其他
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 🔗 (opens new window)
# 来源
Fork from:
# stackoverflow-py-top-qa (opens new window)
# PyZh (opens new window)
# StackOverFlowCn (opens new window)
# stackoverflow_python (opens new window)
编辑 (opens new window)
上次更新: 2024-07-15, 03:27:09