Python学习 2day__基础知识

列表
  del 删除


    列表连接
1 a = [1, 2, 3]
2 b = [4, 5, 6]
3 c = [7, 8, 9]
4 d = a +b + c
5 print(d)


判断元素是否在list中
1 z = [1, 2, 3]
2 x = 8
3 print(x in z)      # True表示在list中,False表示不在list中

遍历列表

  for循环遍历
  while循环遍历

1 # for循环遍历列表
2 f = [1, 2, 3, 'r']
3 
4 for i in f:
5     print(i)

1 # while循环遍历列表, 一般不用while
2 r = ['q', 'w', 'e']
3 length = len(r)
4 indx = 0
5 while indx < length:
6     print(r[indx])
7     indx += 1

  列表操作
 1 #for 创建
 2 a = ['a', 'b', 'c']
 3 b = [i for i in a ]
 4 print(b)
 5 
 6 #对a中所有元素乘以十生成一个新的list
 7 a = [1, 2, 3, 4, 5]
 8 b = [i*10 for i in a ]
 9 print(b)
10 
11 # 过滤元原来列表并生成新列表
12 a = [x for x in range(1,11)]
13 b = [m for m in a if m %2 ==0]
14 print(b)
15 
16 # 列表生成式可以嵌套
17 a = [i for i in range(1,4)]
18 b = [i for i in range(100,400) if i % 100 == 0]
19 c = [m+n for m in a for n in b]
20 print(c)

  列表常用函数
 1 a = [x for x in range(1, 100)]
 2 print(len(a))   # 求列表长度
 3 print(max(a))   # 求列表最大值
 4 b = "I Love Python"
 5 print(list(b))  # 把其他格式数据转换为列表 但被转换目标必须是可迭代的
 6 c = [i for i in range(1, 4)]
 7 c.append(10)    # 末尾添加元素
 8 print(c)
 9 c.insert(1, 66) # 在下标为1的前面插入
10 print(c)
11 l = [1, 2, 3, 4]
12 del l[2]        # 删除下标为2的元素
13 print(l)
14 xxxx = l.pop()  # 把最后一个元素拿出来赋值给xxxx
15 print(l)
16 print(xxxx)
17 d = [8, 9, 6, 4, 7]
18 d.remove(8)     # 删除元素'8',使用remove()时需要确定元素在列表内,否则报错
19 print(d)
20 d.clear()       # 清空列表,但是列表依然存在,地址不变
21 print(d)
22 a = [1, 2, 3, 4, 5]
23 a.reverse()     # 原地反转列表
24 print(a)
25 b = [6, 7, 8, 9, 10]
26 a.extend(b)     # 原地拼接列表
27 print(a)
28 g = [1,5,6,7,8,9,5,4,56489,6112,84,6,12,6,2,3,5,8]
29 g_l = g.count(6) #查找列表中指定元素的个数
30 print(g_l)

copy:深拷贝,浅拷贝
  浅拷贝,拷贝指向的地址
1 a = [1,2,3,4,5,6,7,8,9]
2 print(a)
3 b = a
4 b[5] = 789
5 print(a)
6 print("a的id:"+str(id(a)))
7 print(b)                    # 与a的值相同
8 print("b的id:"+str(id(b)))   # 与a的地址相同

  深拷贝 另外开辟内存空间拷贝内容
1 a = [1,2,3,4,5,6,7,8,9]
2 b = a.copy()
3 b[5] = 987
4 print(a)
5 print("a的id:"+str(id(a)))
6 print(b)                    # 与a的值不同
7 print("b的id:"+str(id(b)))  # 与a的地址不用

元组-tuple
  元组可以看成是一个不可更改的list

 创建元组
 1 # 创建空元组
 2 t = ()
 3 print(type(t))
 4 # 创建有一个值的元组
 5 t = (1,)
 6 print(type(t))
 7 print(t)
 8 t = 1,
 9 print(type(t))
10 print(t)
11 # 创建多个值的元组
12 t = (1, 2, 3, 4, 5)
13 print(type(t))
14 print(t)
15 # 使用其他结构创建
16 l = [1, 2, 3, 4]
17 t = tuple(l)
18 print(type(t))
19 print(t)


 元组的特性
  有序
  元组数据可以访问,不能修改
  数据可以是任意类型
  除了不可修改,具有list的其他特性

 1 t1 = (1, 2, 3)
 2 t2 = (4, 5, 6)
 3 print("t1的id:"+str(id(t1)))
 4 print("t2的id:"+str(id(t2)))
 5 t1 = t1 + t2       # tuple不可改变,这里默认是重新开辟空间建立t1,实际是传址操作
 6 print("t1的id:"+str(id(t1)))
 7 print(t1)
 8 # 双层元组的遍历
 9 t = ((1, 2, 3), (3, 4, 5), ("ss", 'qwe', '9745'))
10 for i in t:
11     print(i)
12 for k,m,n in t:
13     print(k, m, n)


汉诺塔问题

  每次移动一个盘子
  任何时候大盘子在下面,小盘子在上面


    n=1 # 直接把A上的盘子移动到C上 A->C
    n=2 # 把小盘子从A放到B上, A->B
     再把大盘子从A移动到C, A->C
     然后小盘子从B到C B->C
    n=n # 把A上的n-1个盘子借助C移动到B
     把B上的第n个盘子移动到C
     把B上的n-1个盘子借助于A移动到C

 1 def hano(n, a, b, c):
 2     if n == 1:
 3         print(a, "->", c)
 4         return  None
 5     if n ==2:
 6         print(a, "->", b)
 7         print(a, "->", c)
 8         print(b, "->", c)
 9         return None
10     hano(n-1, a, c, b)  # A 借助C把盘子给了B
11     print("------------")
12     print(a, "->", c)
13     print("*******")
14     hano(n-1, b, a, c)  # B 借助A把盘子给了C
15     print("-*-*-*-*-*-*")
16 
17 a, b, c = 'A', 'B', 'C'
18 n = 3
19 hano(n, a, b, c)



原文地址:https://www.cnblogs.com/Burtit/p/9311265.html