您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

python笔记(2)

2025/2/21 10:24:33发布15次查看
继续list:
删除元素:
代码如下:
a =[1, 2, 3, 4]
a[2:3] = [] #[1, 2, 4]
del a[2] #[1, 2]
清空list
代码如下:
a[ : ] = []
del a[:]
list作为栈使用(后入先出):
代码如下:
stack = [3, 4, 5]
stack.append(6)
stack.append(7)
stack.pop() # 7
stack.pop() # 6
stack.pop() # 5
用负数索引:
代码如下:
b=[1, 2, 3, 4]
b[-2] #3
+组合list:
代码如下:
end = ['st', 'nd'] + 5*['th'] + ['xy'] # ['st', 'nd', 'th', 'th', 'th', 'th', 'th', 'xy']
查出某元素在list中的数量:
代码如下:
lst.('hello') # hello 的数量
list排序:
代码如下:
sort()
#对链表中的元素进行适当的排序。
reverse()
#倒排链表中的元素
函数指针的问题:
代码如下:
def f2(a, l=[])
l.append(a)
return l
print(f2(1)) # 1
print(f2(2)) # 1, 2 l在这次函数调用时是[1]
print(f2(3)) # 1, 2, 3
函数中的参数中有:
*参数名 :表示任意个数的参数
**:表示dictionary参数
控制语句:
if:
代码如下:
if x x = 0
print 'negative changed to zero'
elif x == 0:
print 'zero'
elif x == 1:
print 'single'
else:
print 'more'
for:
代码如下:
a = ['cat', 'window', 'defenestrate']
for x in a:
print x, len(x)
while:
代码如下:
a, b = 0, 1
while b print b,
a, b = b, a+b
#1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
pass :空操作语句
代码如下:
while true:
pass
dictionary: 键值对的数据结构
用list来构造dictionary:
代码如下:
items = [('name', 'dc'), ('age', 78)]
d = dict(items) #{'age': 78, 'name': 'dc'}
有趣的比较:
代码如下:
x = [] #list
x[2] = 'foo' #出错
x = {} #dictionary
x[2] = 'foo' #正确
内容比较杂,学到什么就记下来。完全利用工作中的空闲和业余时间来完成,更加充实了。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product