摘要: s = str.split("Hello World") # ['Hello', 'World'] s = "Hello World".split() # ['Hello', 'World'] s = str.upper("Hello World") # HELLO WORLD s = str.is 阅读全文
posted @ 2023-05-18 23:01 sangern 阅读(33) 评论(0) 推荐(0)
摘要: names = ["Tom Cat", "Jerry Mouse", "Thomas Basper", "Gerald Din"] res = sorted(names, key=len) # 按照名字长度排序 ['Tom Cat', 'Gerald Jin', 'Jerry Mouse', 'Th 阅读全文
posted @ 2023-05-18 22:37 sangern 阅读(86) 评论(0) 推荐(0)
摘要: *a, = 'abc' # 如果不加 *a 后面的 逗号,报错 带星号的赋值目标必须位于列表或元组中 print(a) # ['a', 'b', 'c'] *a, b = 'ABC' print(a) # ['A', 'B'] print(b) # C *a, b = 'ABCD' print(a) 阅读全文
posted @ 2023-05-18 21:10 sangern 阅读(56) 评论(0) 推荐(0)
摘要: # 电脑销售量字典 counts = {'MBP': 268, 'HP': 125, 'DELL': 201, 'Small_mi': 200, 'Lenovo': 199, ' acer': 99} # 需求:提取上述电脑数量大于等于200的字典数据 count1 = {key: value fo 阅读全文
posted @ 2023-05-18 16:58 sangern 阅读(13) 评论(0) 推荐(0)
摘要: d = {i: i ** 2 for i in range(1, 8)} print(d) lst3 = ['name', 'age', 'gender'] lst4 = ['Tom', 20, 'Male', 9] d = {lst3[i]: lst4[i] for i in range(min( 阅读全文
posted @ 2023-05-18 16:41 sangern 阅读(17) 评论(0) 推荐(0)
摘要: lst2 = [(i, j) for i in range(1, 3) for j in list("ABC")] print(lst2) lst = {x ** 2 for x in range(1, 11)} print(lst) 阅读全文
posted @ 2023-05-18 16:38 sangern 阅读(48) 评论(0) 推荐(0)
摘要: lst = list("ABC") for i in enumerate(lst): print(i) # 返回结果为元组 (下标,数据) ''' (0, 'A') (1, 'B') (2, 'C') ''' for i in enumerate(lst, start=30): print(i) ' 阅读全文
posted @ 2023-05-18 15:41 sangern 阅读(40) 评论(0) 推荐(0)
摘要: s0 = set() # <class 'set'> s1 = {1, 2, 3, 4, 5, 6, 12, 10, 11, 5, 6, 10} # 去重,无序 print(s1) s2 = set("AaBDG9dtyOp6M") print(s2) # {'9', 'd', 'a', 'G', 阅读全文
posted @ 2023-05-18 14:45 sangern 阅读(14) 评论(0) 推荐(0)
摘要: ''' ''' dict1 = {'K1': 'Aa', 'K2': 'Bb', 'K3': 'Cc'} for k in dict1.keys(): print(k, end=' \t') print() for v in dict1.values(): print(v, end='\t') pr 阅读全文
posted @ 2023-05-18 14:13 sangern 阅读(46) 评论(0) 推荐(0)
摘要: def func(*args): print(args, type(args)) func(3, 4, 5,67,8,'io') 阅读全文
posted @ 2023-05-18 12:35 sangern 阅读(20) 评论(0) 推荐(0)
摘要: >>> t = 2, >>> t (2,) >>> type(t) <class 'tuple'> >>> t = 1,2,3 >>> t (1, 2, 3) >>> type(t) <class 'tuple'> 阅读全文
posted @ 2023-05-18 12:25 sangern 阅读(26) 评论(0) 推荐(0)
摘要: '''把8位老师随机分配到3个办公室''' import random teach = list("ABCDEFGH") off = [[], [], []] for i in teach: j = random.randint(0, 2) # 随机三个数 0 1 2 off[j].append(i 阅读全文
posted @ 2023-05-18 11:42 sangern 阅读(99) 评论(0) 推荐(0)