iterable-unpacking

How to unpack optional items from a tuple? [duplicate]

只谈情不闲聊 提交于 2021-02-04 17:57:05
问题 This question already has answers here : Idiomatic way to unpack variable length list of maximum size n (5 answers) How to unpack tuple of length n to m<n variables [duplicate] (5 answers) Closed 6 years ago . I have a list of some input values, of which the first couple of mandatory and the last couple optional. Is there any easy way to use tuple unpacking to assign these to variables, getting None if the optional parameters are missing. eg. a = [1,2] foo, bar, baz = a # baz == None ideally

Python: Formatting a string using variable names placeholders

我怕爱的太早我们不能终老 提交于 2021-02-04 14:53:25
问题 Consider the following string building statement: s="svn cp %s/%s/ %s/%s/" % (root_dir, trunk, root_dir, tag) Using four %s can be confusing, so I prefer using variable names: s="svn cp {root_dir}/{trunk}/ {root_dir}/{tag}/".format(**SOME_DICTIONARY) When root_dir , tag and trunk are defined within the scope of a class, using self.__dict__ works well: s="svn cp {root_dir}/{trunk}/ {root_dir}/{tag}/".format(**self.__dict__) But when the variables are local, they are not defined in a dictionary

python futures and tuple unpacking

故事扮演 提交于 2021-01-27 18:45:50
问题 What is an elagant/idiomatic way to achieve something like tuple unpacking with futures? I have code like a, b, c = f(x) y = g(a, b) z = h(y, c) and I would like to convert it to use futures. Ideally I would like to write something like a, b, c = ex.submit(f, x) y = ex.submit(g, a, b) z = ex.submit(h, y, c) The first line of that throws TypeError: 'Future' object is not iterable though. How can I get a,b,c without having to make 3 additional ex.submit calls? ie. I would like to avoid having

How to define a tuple of randint without repeating code?

梦想与她 提交于 2021-01-27 12:08:37
问题 I often get to use tuples of randint for color-values and such like (a, b, c) = randint(0, 255), randint(0, 255), randint(0, 255) when I thought there has to be a better way - is there? 回答1: Using numpy? 1 import numpy as np tuple(np.random.randint(256, size=3)) # (222, 49, 14) Multiple import numpy as np n=3 [tuple(i) for i in np.random.randint(256, size=(n,3))] # list # (tuple(i) for i in np.random.randint(256, size=(n,3))) # generator # [(4, 70, 3), (10, 231, 41), (141, 198, 105)] Speed

How to define a tuple of randint without repeating code?

佐手、 提交于 2021-01-27 11:59:56
问题 I often get to use tuples of randint for color-values and such like (a, b, c) = randint(0, 255), randint(0, 255), randint(0, 255) when I thought there has to be a better way - is there? 回答1: Using numpy? 1 import numpy as np tuple(np.random.randint(256, size=3)) # (222, 49, 14) Multiple import numpy as np n=3 [tuple(i) for i in np.random.randint(256, size=(n,3))] # list # (tuple(i) for i in np.random.randint(256, size=(n,3))) # generator # [(4, 70, 3), (10, 231, 41), (141, 198, 105)] Speed

How to define a tuple of randint without repeating code?

好久不见. 提交于 2021-01-27 11:59:15
问题 I often get to use tuples of randint for color-values and such like (a, b, c) = randint(0, 255), randint(0, 255), randint(0, 255) when I thought there has to be a better way - is there? 回答1: Using numpy? 1 import numpy as np tuple(np.random.randint(256, size=3)) # (222, 49, 14) Multiple import numpy as np n=3 [tuple(i) for i in np.random.randint(256, size=(n,3))] # list # (tuple(i) for i in np.random.randint(256, size=(n,3))) # generator # [(4, 70, 3), (10, 231, 41), (141, 198, 105)] Speed

Unpacking: [x,y], (x,y), x,y - what is the difference?

时间秒杀一切 提交于 2020-12-25 01:41:56
问题 What is the difference in Python between unpacking a function call with [] , with () or with nothing? def f(): return 0, 1 a, b = f() # 1 [a, b] = f() # 2 (a, b) = f() # 3 回答1: There is no difference. Regardless of what kind of syntactic sequence you use, the same byte code is generated. >>> def f(): ... return 0, 1 ... >>> import dis >>> dis.dis('[a,b] = f()') 1 0 LOAD_NAME 0 (f) 2 CALL_FUNCTION 0 4 UNPACK_SEQUENCE 2 6 STORE_NAME 1 (a) 8 STORE_NAME 2 (b) 10 LOAD_CONST 0 (None) 12 RETURN

Unpacking: [x,y], (x,y), x,y - what is the difference?

…衆ロ難τιáo~ 提交于 2020-12-25 01:41:33
问题 What is the difference in Python between unpacking a function call with [] , with () or with nothing? def f(): return 0, 1 a, b = f() # 1 [a, b] = f() # 2 (a, b) = f() # 3 回答1: There is no difference. Regardless of what kind of syntactic sequence you use, the same byte code is generated. >>> def f(): ... return 0, 1 ... >>> import dis >>> dis.dis('[a,b] = f()') 1 0 LOAD_NAME 0 (f) 2 CALL_FUNCTION 0 4 UNPACK_SEQUENCE 2 6 STORE_NAME 1 (a) 8 STORE_NAME 2 (b) 10 LOAD_CONST 0 (None) 12 RETURN