# -*- coding:utf-8 -*- from collections import deque __author__ = ‘hunterhug‘ mylist = [2, 3, 4, 5] print(mylist) mylist.append(3) # 添加元素到链尾 print(mylist) mylist[mylist.__len__():]=[5] print(mylist) mylist.extend(mylist) # 链表叠加 print(mylist) mylist.insert(1, ‘d‘) # 在位置前插入元素 print(mylist) # mylist.remove(‘n‘) # 移除元素,没有该元素则出错 mylist.pop() # 删除尾巴,并返回该元素 print(mylist) mylist.pop(1) # 删除指定位置元素 print(mylist) print(mylist.index(3)) # 找出元素位置 print(mylist.count(3)) # 统计元素出现次数 print(mylist) print(mylist.reverse()) # 倒排链表返回None print(mylist) mylist.sort(reverse=True) # 排序降序 print(mylist) # 链表作为堆栈,先进后出 stack1 = [2, 3, 4, 5] stack1.append(1) print(stack1) stack1.pop() print(stack1) # 队列 queue=deque(stack1) print(queue) queue.append(1) # 从后面插入 print(queue) queue.appendleft(1) # 从前面插入 print(queue) queue.pop() print(queue) # 从后面删除 queue.popleft() print(queue) # 从前面删除 # 列表推导式 s = [] for i in range(10): s.append(i**2) print(s) s1 = [x**2 for x in range(10)] print(s1) s2 = [(x, y) for x in [1, 2, 3] for y in [3, 1, 4] if x != y] print(s2) freshfruit = [‘ banana‘, ‘ loganberry ‘, ‘passion fruit ‘] s3 = [weapon.strip() for weapon in freshfruit] print(s3) s4 = [(x, x**2) for x in range(6)] print(s4) vec = [[1,2,3], [4,5,6], [7,8,9]] s5 = [num for elem in vec for num in elem] print(s5) from math import pi s6 = [str(round(pi, i)) for i in range(100,102)] print(s6) # 矩阵转置 matrix = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ] s7 = [[row[i] for row in matrix] for i in range(4)] print(s7) # 等价于下面 transposed = [] for i in range(4): transposed.append([row[i] for row in matrix]) print(transposed) print(list(zip(*matrix))) # 删除利器 j = [3, 4] del j[0] print(j) j.extend([3, 3]) print(j) del j[0:2] print(j) # 元组,不可变 t = 12345, 54321, ‘hello!‘ print(t[0]) u = t, [33, 22] print(u) u[1].pop() print(u) # 序列拆封 a, b = u print(a) print(b) empty = () print(len(empty)) print(empty.count(2)) # 找不到2这个元素 c = "dd", # 单元素元组 print(c) # 集合,无序不重复 basket = {‘apple‘, ‘orange‘, ‘apple‘, ‘pear‘, ‘orange‘, ‘banana‘} # 字典 # 大括号或 set() 函数可以用来创建集合。 # 注意:想要创建空集合,你必须使用 set() 而不是 {} 。 # 后者用于创建空字典, print(basket) print(‘crabgrass‘ in basket) a = set(‘abracadabra‘) b = set(‘alacazam‘) print(‘a:‘, a) print(‘b:‘, b) print(a - b) # 在a中但不在b中元素 print(a | b) # 在a或在b中元素 print(a & b) # 在a且在b中元素 print(a ^ b) # 或减去且 a = {x for x in ‘abracadabra‘ if x not in ‘abc‘} print(a) # 字典 tel = {‘jack‘: 4098, ‘sape‘: 4139} print(list(tel.keys())) print(sorted(tel.keys(),reverse=True)) print("h" in tel) tel1 = dict([(‘sape‘, 4139), (‘guido‘, 4127), (‘jack‘, 4098)]) print(tel1) tel2 = {x: x**2 for x in (2, 4, 6)} print(tel2) tel3 = dict(sape=4139, guido=4127, jack=4098) print(tel3) for k, v in tel3.items(): print(k, ",", v) # 序列中循环 for i, v in enumerate([‘tic‘, ‘tac‘, ‘toe‘]): print(i, v) for i, v in enumerate((‘tic‘, ‘tac‘, ‘toe‘)): print(i, v) for i, v in enumerate({‘tic‘:"j", ‘tac‘:"", ‘toe‘:"g"}): print(i, v) # 整体打包 questions = [‘name‘, ‘quest‘, ‘favorite color‘] answers = [‘lancelot‘, ‘the holy grail‘, ‘blue‘] for q, a in zip(questions, answers): print(‘What is your {0}? It is {1}.‘.format(q, a)) for i in reversed(range(1, 10, 2)): print(i) asket = [‘apple‘, ‘orange‘, ‘apple‘, ‘pear‘, ‘orange‘, ‘banana‘] for f in sorted(set(basket)): print(f) print((1, 2, 3) < (1, 2, 4)) string1, string2, string3 = ‘‘, ‘Trondheim‘, ‘Hammer Dance‘ non_null = string1 or string2 or string3 print(non_null) #http://docs.pythontab.com/python/python3.4/modules.html
时间: 2024-12-26 20:28:01