这个馒头有点小

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1.列表是一个数据的集合,集合内可以放任何数据类型,可以对集合进行方便的增删改查操作

1)       创建列表,如图

 

2)       查询列表,如图

 

 

 

 

3)       切片,如图

 

 

4)       增加和修改

 

5)       删除

 

6)       循环

 

7)       排序

8)列表代码

 1 #生产一个随机数列表
 2 print(range(10))
 3 
 4 #遍历列表的每个元素
 5 for i in range(10):
 6     print(i)
 7 
 8 n =['a','b','c','A','B','C']
 9 #根据ASCII码表进行正向排序
10 n.sort()
11 print(n)
12 #根据ASCII码表进行反向排序
13 n.reverse()
14 print(n)
15 
16 #如果是单纯的列表之间进行赋值,赋值后它们都指向同一块内存地址,所以其中任意一个列表发生变化,其它的列表也同样变化
17 n =['a','b','c','A','B','C']
18 n2 = n
19 print(n2)
20 del n[0]
21 print(n)
22 print(n2)
23 #如上,如果要解决这个问题,可以使用列表的copy函数,这样复制出来的列表与原列表不互相干扰
24 n =['a','b','c','A','B','C']
25 n2 = n.copy()
26 del n[0]
27 print(n)
28 print(n2)
29 
30 #enumerate()枚举函数
31 #用这个函数可以打印索引
32 n =['a','b','c','A','B','C']
33 for i in enumerate(n):
34     print(i)
35 
36 #加入购物车小程序
37 
38 products = [['Ipone',8888],["小米",2222],["华为",20000],["联想笔记本",4000]]
39 shopping_cart = []
40 while True:
41     print("--------------- 商品列表--------------")
42     for index,p in enumerate(products) :
43         print("%s  %s       %s"%(index,p[0],p[1]))
44     choice = input("请输入要购买商品的序号(退出请按q):")
45     if choice.isdigit() == True:
46         choice = int(choice)
47         if choice>=0 and choice<len(products):
48             shopping_cart.append(products[choice])
49         else:
50             print("请输入列表中有的商品序号!")
51     elif choice == "q":
52         if len(shopping_cart)>0:
53             print("------------ 购买q商品列表 ------------")
54             for index,p in enumerate(shopping_cart):
55                 print(index,p[0],p[1])
56         break
57 #商品列表
58 products = [['Ipone',8888],["小米",2222],["华为",20000],["联想笔记本",4000]]
59 #创建空的购物车列表
60 shopping_cart = []
61 #设置循环开关
62 '''
63 run_flag = True
64 while run_flag:
65     print("--------------  商品列表 -------------")
66     #遍历商品列表,得到各个序列的商品名字
67     for index,p in enumerate(products):
68         print("%s   %s     %s"%(index,p[0],p[1]))
69     #获取选择的商品序列号
70     choice = input("请输入要购买的商品序列号(退出请按q):")
71     if choice.isdigit() == True:
72         choice = int(choice)
73         if choice>= 0 and choice < len(products):
74             shopping_cart.append(products[choice])
75     elif choice == "q":
76         if len(shopping_cart)> 0:
77             print("------------ 已购买商品列表 -----------")
78             for index,p in enumerate(shopping_cart):
79                 print(index,p[0],p[1])
80         run_flag = False
81         #break
82 
83 '''
84 #浅copy,这种状态下,对列表下的子列表的元素进行修改,所有关联的列表都会跟着变
85 names = ['龙庭','ErDang',"WangEr",['QQ',15]]
86 n1 = names.copy()
87 names[-1][0] = "qq"
88 print(names)
89 print(n1)
90 
91 #深copy,对列表下的子列表的元素进行修改,所有关联的列表都不会跟着改变
92 names = ['龙庭','ErDang',"WangEr",['QQ',15]]
93 import copy
94 n2 = copy.deepcopy(names)
95 names[-1][0] = "qq"
96 print(names)
97 print(n2)
list

老师的代码:

 

2.字符串的函数们

 

  1 str = "Hello Word!"
  2 #统计 o 在字符串中的数量
  3 print(str.count("o"))
  4 #也可以设置起始位置
  5 print(str.count("o",0,3))
  6 #设置字符串的首字母大写,其它小写
  7 print(str.capitalize())
  8 #设置字符串所有字母都小写
  9 print(str.casefold())
 10 #设置字符串两边有多少个相同的字符
 11 print(str.center(50,"*"))
 12 #转码
 13 print(str.encode(encoding="utf-8",errors="strict"))
 14 #检查是否以某个字符结尾
 15 print(str.endswith("!"))
 16 #\t代表Tab键,这里指定Tab键所占用空格数
 17 s1 = "a\tb"
 18 print(s1.expandtabs(tabsize=10))
 19 #查找指定字符在字符串中的位置
 20 print(str.find("o"))
 21 #也可以指定查找范围,找不到返回-1
 22 print(str.find("0",0,3))
 23 #format字符串格式化,用法一
 24 s2 = "Hello,My Name {0},I am {1} year old."
 25 print(s2.format("Jack",26))
 26 #format字符串格式化,用法二
 27 s3 = "Hello,My Name is {name},I am {age} year old. "
 28 print(s3.format(name = "jack",age = "26"))
 29 #查找指定字符在字符串中的索引
 30 print(str.index("o"))
 31 #判断这串字符串是否由数字及字母组成,是返回True,如果包含特殊字符,则返回False
 32 s4 = "11aa"
 33 print(s4.isalnum())
 34 #判断字符串是否由单纯的字母组成
 35 s5 = "aab"
 36 print(s5.isalpha())
 37 #判断字符串是否由单纯的数字组成
 38 s6 = "123"
 39 print("isdecimal: ",s6.isdecimal())
 40 print("isdigit: ",s6.isdigit())
 41 #判断字符串是否符合变量的命名规则
 42 s7 = "names"
 43 print("isidentifier: ",s7.isidentifier())
 44 #判断字符串是否由小写字母组成
 45 s8 = "abc"
 46 print("islower: ",s8.islower())
 47 #判断字符串是否可以转换成数字
 48 s9 = "123"
 49 print("isnumeric: ",s9.isnumeric())
 50 #在liunx中一切皆对象
 51 s10 = "hello World"
 52 print("isprinttable: ",s10.isprintable())
 53 #判断字符串是否为空格
 54 s11 = " "
 55 print("isspace: ",s11.isspace())
 56 #判断字符串是否是标题,标题的每个单词的开头字母必须大写
 57 s12 = "Hello World"
 58 print("istitle: ",s12.istitle())
 59 #判断字符串是否大写
 60 s13 = "ABC"
 61 print("isupper: ",s13.isupper())
 62 #把列表转换成字符串,可以指定每个元素之间用什么字符进行分隔
 63 #alibaba---baidu---oldBoy
 64 new_list = ["alibaba","baidu","oldBoy"]
 65 print("join: ","---".join(new_list))
 66 #字符串从左往右数,在没有字符的地方填充指定的字符
 67 #Hello Word!---------------------------------------
 68 print("ljust: ",str.ljust(50,"-"))
 69 #把字符串中所有大写字母都转换成小写
 70 s14 = "NAME"
 71 print("lower: ",s14.lower())
 72 #把字符串中所有小写字母都转换成大写
 73 s15 = "name"
 74 print("upper: ",s15.upper())
 75 #把字符串左边的空格包括回车都脱掉
 76 s16 = " \n    Hello"
 77 print("lstrip: ",s16.lstrip())
 78 #创建密码表,执行密码表对字符串进行加密
 79 s17 = "append"
 80 str_in = "abcdef"
 81 str_out = "!@#$%^"
 82 table = str.maketrans(str_in,str_out)
 83 print(s17.translate(table))
 84 #替换指定字符,可以指定替换多少个
 85 s18 = "2017-12-27"
 86 print("replace: ",s18.replace("-","/"))
 87 #从字符串的右边开始找指定字符,找到返回找到的索引值,没找到返回-1
 88 print("rfind: ",str.rfind("o"))
 89 #从字符串的右边开始找指定字符,找到返回找到的索引值,没找到返回报错信息
 90 print("rindex: ",str.rindex("o"))
 91 #从字符串的右边开始数,把没有字符的地方填充进指定的字符
 92 print("rjust: ",str.rjust(50,"-"))
 93 #从右边按指定字符进行分元素
 94 s19 = "abcdefg"
 95 print("rpartition: ",s19.rpartition("d"))
 96 #把字符串按指定字符进行分割,把分割到的每个元素重新组成一个列表并返回
 97 s20 = "alibaba baidu oldboy"
 98 print("split: ",s20.split(" "))
 99 print("rsplit: ",s20.rsplit(" "))
100 #脱掉右边空格(包括换行符)
101 s21 = "hello world           "
102 print("rstrip: ",s21.rstrip())
103 #按换行符对字符串进行分割,把分割到的每个元素组成一个新的列表并返回
104 s22 = "hello \nworld \n!"
105 print("splitlines: ",s22.splitlines())
106 #判断字符串是否以指定的字符开头
107 print("startswith: ",str.startswith("H"))
108 #把左右两边的空格和回车符都脱掉
109 s23 = "\n            hello world!  \n            "
110 print("strip: ",s23.strip())
111 #把字符串都转换成小写,和lower同
112 s24 = "HELLO"
113 print("swapcase: ",s24.swapcase())
114 #把指定字符转换成标题
115 s25 = "hello world"
116 print("title: ",s25.title())
117 #把字符串变成指定长度的字符串,没有元素的以0进行填充
118 s26 = "hello"
119 print("zfill: ",s26.zfill(40))
string

3.元祖类型(tuple)

1 #元祖和列表类似,但是它的元素是不可变的,但是,如果该元素是可变类型,那是可以改变的
2 #元祖类型一般用来存放那些不可更改的数据,如,数据库连接字符串
3 names = ("Alibaba","Baidu","Oldboy",["Jack","Pende"])
4 #names[0] = "Rupeng" 直接报错,因为元祖的元素不可以修改
5 print("修改之前:",names)
6 names[3][0] = "blibli"
7 print("修改之后:",names)
8 #也可以切片
9 print(names[2:])
tuple

4.哈希类型(hash)

1 #hash只能加密不可变的类型,可变类型不可以用hash进行加密
2 names = ("Jack","Pende","Alibaba")
3 print(hash(names))
hash

 

posted on 2017-12-27 22:11  这个馒头有点小  阅读(576)  评论(0)    收藏  举报