随笔分类 -  python

摘要:setp = set(["Red", "Green"]) setq = setp.copy() print(setq) setp.clear() print(setq) 阅读全文
posted @ 2018-11-12 22:37 anobscureretreat 阅读(1355) 评论(0) 推荐(0) 编辑
摘要:#A new empty set color_set = set() color_set.add("Red") print(color_set) #Add multiple items color_set.update(["blue","blue", "Green"]) print(color_set) 阅读全文
posted @ 2018-11-12 22:31 anobscureretreat 阅读(7705) 评论(0) 推荐(0) 编辑
摘要:#create a tuple l = [(1,2), (3,4), (8,9)] print(list(zip(*l))) 阅读全文
posted @ 2018-11-12 22:27 anobscureretreat 阅读(4577) 评论(0) 推荐(0) 编辑
摘要:#create a tuple tuplex = 4, 8, 3 print(tuplex) n1, n2, n3 = tuplex #unpack a tuple in variables print(n1 + n2 + n3) #the number of variables must be equal to the number of items of the tuple n1, n2... 阅读全文
posted @ 2018-11-12 22:25 anobscureretreat 阅读(544) 评论(0) 推荐(0) 编辑
摘要:#create a tuple tuplex = (2, 4, 3, 5, 4, 6, 7, 8, 6, 1) #used tuple[start:stop] the start index is inclusive and the stop index _slice = tuplex[3:5] #is exclusive print(_slice) #if the start index ... 阅读全文
posted @ 2018-11-12 22:22 anobscureretreat 阅读(1895) 评论(0) 推荐(0) 编辑
摘要:#create a tuple x = ("w3resource") # Reversed the tuple y = reversed(x) print(tuple(y)) #create another tuple x = (5, 10, 15, 20) # Reversed the tuple y = reversed(x) print(tuple(y)) 阅读全文
posted @ 2018-11-12 22:20 anobscureretreat 阅读(2211) 评论(0) 推荐(0) 编辑
摘要:#create a tuple tuplex = "w", "j" ,"c", "e" print(tuplex) #tuples are immutable, so you can not remove elements #using merge of tuples with the + operator you can remove an item and it will create ... 阅读全文
posted @ 2018-11-12 22:17 anobscureretreat 阅读(3888) 评论(0) 推荐(0) 编辑
摘要:#create a tuple tuplex = 2, 4, 5, 6, 2, 3, 4, 4, 7 print(tuplex) #return the number of times it appears in the tuple. count = tuplex.count(4) print(count) 阅读全文
posted @ 2018-11-12 10:41 anobscureretreat 阅读(818) 评论(0) 推荐(0) 编辑
摘要:#create a tuple tuplex = tuple("index tuple") print(tuplex) #get index of the first item whose value is passed as parameter index = tuplex.index("p") print(index) #define the index from which you w... 阅读全文
posted @ 2018-11-12 10:37 anobscureretreat 阅读(4194) 评论(0) 推荐(0) 编辑
摘要:from copy import deepcopy #create a tuple tuplex = ("HELLO", 5, [], True) print(tuplex) #make a copy of a tuple using deepcopy() function tuplex_clone = deepcopy(tuplex) tuplex_clone[2].append(50) ... 阅读全文
posted @ 2018-11-12 10:31 anobscureretreat 阅读(155) 评论(0) 推荐(0) 编辑
摘要:tup = ('e', 'x', 'e', 'r', 'c', 'i', 's', 'e', 's') str = ''.join(tup) print(str) 阅读全文
posted @ 2018-11-12 10:25 anobscureretreat 阅读(3946) 评论(0) 推荐(0) 编辑
摘要:#create a tuple tuplex = ((2, "w"),(3, "r")) print(dict((y, x) for x, y in tuplex)) 阅读全文
posted @ 2018-11-12 10:22 anobscureretreat 阅读(4345) 评论(0) 推荐(0) 编辑
摘要:#create a list l = [("x", 1), ("x", 2), ("x", 3), ("y", 1), ("y", 2), ("z", 1)] d = {} for a, b in l: d.setdefault(a, []).append(b) print (d) 阅读全文
posted @ 2018-11-12 10:17 anobscureretreat 阅读(983) 评论(0) 推荐(0) 编辑
摘要:例一 例二 输出: 阅读全文
posted @ 2018-11-11 12:23 anobscureretreat 阅读(157) 评论(0) 推荐(0) 编辑
摘要:输出: 阅读全文
posted @ 2018-11-11 12:22 anobscureretreat 阅读(132) 评论(0) 推荐(0) 编辑
摘要:第一种 第二种 shell下的一句话脚本,在命令行执行以下语句 阅读全文
posted @ 2018-11-10 15:37 anobscureretreat 阅读(146) 评论(0) 推荐(0) 编辑
摘要:这个也是python彪悍的特性. 自省就是面向对象的语言所写的程序在运行时,所能知道对象的类型.简单一句就是运行时能够获得对象的类型.比如type(),dir(),getattr(),hasattr(),isinstance(). a = [1,2,3] b = {'a':1,'b':2,'c':3 阅读全文
posted @ 2018-11-10 00:43 anobscureretreat 阅读(115) 评论(0) 推荐(0) 编辑
摘要:a = 1 def fun(a): a = 2 fun(a) print a # 1 a = [] def fun(a): a.append(1) fun(a) print a # [1] 阅读全文
posted @ 2018-11-10 00:32 anobscureretreat 阅读(123) 评论(0) 推荐(0) 编辑
摘要:⾸先我们来理解下Python中的函数 def hi(name="yasoob"): return "hi " + name print(hi()) # output: 'hi yasoob' # 我们甚⾄可以将⼀个函数赋值给⼀个变量,⽐如 greet = hi # 我们这⾥没有在使⽤⼩括号,因为我们并不是在调⽤hi函数 # ⽽是在将它放在greet变量⾥头。我们尝试运⾏下这个 print(g... 阅读全文
posted @ 2018-11-09 22:47 anobscureretreat 阅读(1283) 评论(0) 推荐(0) 编辑
摘要:def hi(name="yasoob"): def greet(): return "now you are in the greet() function" def welcome(): return "now you are in the welcome() function" if name == "yasoob": ... 阅读全文
posted @ 2018-11-09 22:46 anobscureretreat 阅读(188) 评论(0) 推荐(0) 编辑