iterable-unpacking

Python-like unpacking of numeric value in R [duplicate]

寵の児 提交于 2019-12-04 17:56:08
问题 This question already has answers here : Assign multiple new variables on LHS in a single line (14 answers) Can I assign variables in batch? [duplicate] (2 answers) Closed 6 years ago . In Python, one can do this: >>> a, b, c = (1, 2, 3) >>> a 1 >>> b 2 >>> c 3 Is there a way to do it in R, as below? > a, b, c = c(1, 2, 3) 回答1: You can do this within a list using [<- e <- list() e[c('a','b','c')] <- list(1,2,3) Or within a data.table using := library(data.table) DT <- data.table() DT[, c('a',

Is it possible to unpack a tuple without using variables?

情到浓时终转凉″ 提交于 2019-12-04 02:56:17
问题 I'm using the os.path.split() function on a path in my program to get the filename and pathname of a file then passing them into another method, but my current solution seems rather ugly: path = os.path.split(somefile) some_class(path[0], path[1]) Is it possible to unpack the path tuple in a cleaner way within the call to some_class? Something like: some_class(os.path.split(somefile).unpack()) Or should I simply be going about this another way? Maybe a more pythonic way? 回答1: Yes, Python has

Pythonic way to unpack an iterator inside of a list

送分小仙女□ 提交于 2019-12-03 20:10:27
I'm trying to figure out what is the pythonic way to unpack an iterator inside of a list. For example: my_iterator = zip([1, 2, 3, 4], [1, 2, 3, 4]) I have come with the following ways to unpack my iterator inside of a list: 1) my_list = [*my_iterator] 2) my_list = [e for e in my_iterator] 3) my_list = list(my_iterator) No 1) is my favorite way to do it since is less code, but I'm wondering if this is also the pythonic way. Or maybe there is another way to achieve this besides those 3 which is the pythonic way? This might be a repeat of Fastest way to convert an iterator to a list , but your

Python-like unpacking of numeric value in R [duplicate]

大憨熊 提交于 2019-12-03 12:26:22
This question already has an answer here: Assign multiple new variables on LHS in a single line 14 answers Can I assign variables in batch? [duplicate] 2 answers In Python, one can do this: >>> a, b, c = (1, 2, 3) >>> a 1 >>> b 2 >>> c 3 Is there a way to do it in R, as below? > a, b, c = c(1, 2, 3) You can do this within a list using [<- e <- list() e[c('a','b','c')] <- list(1,2,3) Or within a data.table using := library(data.table) DT <- data.table() DT[, c('a','b','c') := list(1,2,3)] With both of these (lists), you could then use list2env to copy to the global (or some other) environment

Is it possible to assign a default value when unpacking?

本秂侑毒 提交于 2019-12-03 11:31:02
I have the following: >>> myString = "has spaces" >>> first, second = myString.split() >>> myString = "doesNotHaveSpaces" >>> first, second = myString.split() Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: need more than 1 value to unpack I would like to have second default to None if the string does not have any white space. I currently have the following, but am wondering if it can be done in one line: splitted = myString.split(maxsplit=1) first = splitted[0] second = splitted[1:] or None May I suggest you to consider using a different method, i.e.

What is scheme's equivalent of tuple unpacking?

假装没事ソ 提交于 2019-12-03 11:21:45
问题 In Python, I can do something like this: t = (1, 2) a, b = t ...and a will be 1 and b will be 2. Suppose I have a list '(1 2) in Scheme. Is there any way to do something similar with let ? If it makes a difference, I'm using Racket. 回答1: In racket you can use match, (define t (list 1 2)) (match [(list a b) (+ a b)]) and related things like match-define: (match-define (list a b) (list 1 2)) and match-let (match-let ([(list a b) t]) (+ a b)) That works for lists, vectors, structs, etc etc. For

Is it possible to unpack a tuple in Python without creating unwanted variables?

馋奶兔 提交于 2019-12-03 10:34:20
问题 Is there a way to write the following function so that my IDE doesn't complain that column is an unused variable? def get_selected_index(self): (path, column) = self._tree_view.get_cursor() return path[0] In this case I don't care about the second item in the tuple and just want to discard the reference to it when it is unpacked. 回答1: In Python the _ is often used as an ignored placeholder. (path, _) = self._treeView.get_cursor() You could also avoid unpacking as a tuple is indexable. def get

Unpacking tuples in a python list comprehension (cannot use the *-operator)

时光总嘲笑我的痴心妄想 提交于 2019-12-03 06:39:05
I am trying to create a list based on another list, with the same values repeated 3 times consecutively. At the moment, I am using: >>> my_list = [ 1, 2 ] >>> three_times = [] >>> for i in range( len( my_list ) ): ... for j in range( 3 ): ... three_times.append( my_list[ i ] ) ... >>> print three_times [1, 1, 1, 2, 2, 2] But I would like to do it using a more Pythonic way, such as: >>> my_list = [ 1, 2 ] >>> three_times = [] >>> three_times = [ (value,) * 3 for value in my_list ] >>> print three_times [(1, 1, 1), (2, 2, 2)] However, I cannot find a way to unpack the tuples. Something like

Understanding *x ,= lst

喜夏-厌秋 提交于 2019-12-03 02:31:21
问题 I'm going through some old code trying to understand what it does, and I came across this odd statement: *x ,= p p is a list in this context. I've been trying to figure out what this statement does. As far as I can tell, it just sets x to the value of p . For example: p = [1,2] *x ,= p print(x) Just gives [1, 2] So is this any different than x = p ? Any idea what this syntax is doing? 回答1: *x ,= p is basically an obfuscated version of x = list(p) using extended iterable unpacking. The comma

What is scheme's equivalent of tuple unpacking?

放肆的年华 提交于 2019-12-03 01:45:30
In Python, I can do something like this: t = (1, 2) a, b = t ...and a will be 1 and b will be 2. Suppose I have a list '(1 2) in Scheme. Is there any way to do something similar with let ? If it makes a difference, I'm using Racket. In racket you can use match , (define t (list 1 2)) (match [(list a b) (+ a b)]) and related things like match-define : (match-define (list a b) (list 1 2)) and match-let (match-let ([(list a b) t]) (+ a b)) That works for lists, vectors, structs, etc etc. For multiple values, you'd use define-values : (define (t) (values 1 2)) (define-values (a b) (t)) or let