tuples

“Tuple comprehensions” and the star splat/unpack operator *

五迷三道 提交于 2019-12-03 17:31:32
I just read the question Why is there no tuple comprehension in Python? In the comments of the accepted answer , it is stated that there are no true "tuple comprehensions". Instead, our current option is to use a generator expression and pass the resulting generator object to the tuple constructor: tuple(thing for thing in things) Alternatively, we can create a list using a list comprehension and then pass the list to the tuple constructor: tuple([thing for thing in things]) Lastly and to the contrary of the accepted answer, a more recent answer stated that tuple comprehensions are indeed a

C++ - Iterating over a tuple & resolution of type vs constant parameters

时光毁灭记忆、已成空白 提交于 2019-12-03 17:09:40
问题 I'm currently in the process of writing arithmetic operator overloads for tuples. The operator iterates over the tuple to perform the operation on each of its individual element. Here is the definition for operator +=: template< typename... Ts, std::size_t I = 0 > inline typename std::enable_if< I == sizeof... (Ts), std::tuple< Ts... >& >::type operator +=(std::tuple< Ts... >& lhs, const std::tuple< Ts... >& rhs) { return lhs; } template< typename... Ts, std::size_t I = 0 > inline typename

Python 3: Removing an empty tuple from a list of tuples

戏子无情 提交于 2019-12-03 17:05:38
I have a list of tuples that reads as such: >>>myList [(), (), ('',), ('c', 'e'), ('ca', 'ea'), ('d',), ('do',), ('dog', 'ear', 'eat', 'cat', 'car'), ('dogs', 'cars', 'done', 'eats', 'cats', 'ears'), ('don',)] And I would like it to read as such: >>>myList [('',), ('c', 'e'), ('ca', 'ea'), ('d',), ('do',), ('dog', 'ear', 'eat', 'cat', 'car'), ('dogs', 'cars', 'done', 'eats', 'cats', 'ears'), ('don',)] i.e. I would like to remove the empty tuples () from the list. While doing this I want to preserve the tuple ('',) . I cannot seem to find a way to remove these empty tuples from the list. I have

Scala underscore explanation

本小妞迷上赌 提交于 2019-12-03 16:55:01
Have a look at these scala snippets: if we have something like this: List(List(1, 2), List(3, 4), List(5)) map (x => (x.size)) we can shorten it to: List(List(1, 2), List(3, 4), List(5)) map ((_.size)) but, if we have something like this: List(List(1, 2), List(3, 4), List(5)) map (x => (x.size, x.size)) why can't we shorten it to: List(List(1, 2), List(3, 4), List(5)) map ((_.size, _.size)) ? An amount of placeholders should be equals amount of function parameters. In your case map has 1 parameter that's why you can't use two placeholders. Because List(List(1, 2), List(3, 4), List(5)) map ((_

Scala: List[Tuple3] to Map[String,String]

非 Y 不嫁゛ 提交于 2019-12-03 16:38:21
I've got a query result of List[(Int,String,Double)] that I need to convert to a Map[String,String] (for display in an html select list) My hacked solution is: val prices = (dao.getPricing flatMap { case(id, label, fee) => Map(id.toString -> (label+" $"+fee)) }).toMap there must be a better way to achieve the same... A little more concise: val prices = dao.getPricing.map { case (id, label, fee) => ( id.toString, label+" $"+fee)} toMap shorter alternative: val prices = dao.getPricing.map { p => ( p._1.toString, p._2+" $"+p._3)} toMap How about this? val prices: Map[String, String] = dao

List Tuple more than 8 items

谁都会走 提交于 2019-12-03 16:02:49
Can anyone help the following List Tuple more than 8 elements is not working: List<Tuple<int, string, double, string, int, string, double, Tuple<int, string>>> tpl = new List<Tuple<int, string, double, string, int, string, double, Tuple<int, string>>>(); tpl.Add(Tuple.Create(1, "ABC", 100.123, "XYZ", 1, "ABC", 100.123, new Tuple<int, string>(100, "My Rest Item"))); foreach(var k in tpl) listBox1.Items.Add(k.Item1.ToString() + " ---> " + k.Item2.ToString() + " ---> " + k.Item3.ToString() + " ---> " + k.Item4.ToString() + " ---> " + k.Item5.ToString() + " ---> " + k.Item6.ToString() + " ---> " +

What's the best way of using a pair (triple, etc) of values as one value in C#?

社会主义新天地 提交于 2019-12-03 15:08:22
问题 That is, I'd like to have a tuple of values. The use case on my mind: Dictionary<Pair<string, int>, object> or Dictionary<Triple<string, int, int>, object> Are there built-in types like Pair or Triple? Or what's the best way of implementing it? Update There are some general-purpose tuples implementations described in the answers, but for tuples used as keys in dictionaries you should additionaly verify correct calculation of the hash code. Some more info on that in another question. Update 2

Create a slice using a tuple

梦想的初衷 提交于 2019-12-03 15:04:19
问题 Is there any way in python to use a tuple as the indices for a slice? The following is not valid: >>> a = range(20) >>> b = (5, 12) # my slice indices >>> a[b] # not valid >>> a[slice(b)] # not valid >>> a[b[0]:b[1]] # is an awkward syntax [5, 6, 7, 8, 9, 10, 11] >>> b1, b2 = b >>> a[b1:b2] # looks a bit cleaner [5, 6, 7, 8, 9, 10, 11] It seems like a reasonably pythonic syntax so I am surprised that I can't do it. (update) And the solution turns out to be: >>> a[slice(*b)] [5, 6, 7, 8, 9, 10

pop/remove items out of a python tuple

人走茶凉 提交于 2019-12-03 14:53:36
问题 I am not sure if I can make myself clear but will try. I have a tuple in python which I go through as follows (see code below). While going through it, I maintain a counter (let's call it 'n') and 'pop' items that meet a certain condition. Now of course once I pop the first item, the numbering all goes wrong, how can I do what I want to do more elegantly while removing only certain entries of a tuple on the fly? for x in tupleX: n=0 if (condition): tupleX.pop(n) n=n+1 回答1: As DSM mentions,

Sorting a list of tuples with multiple conditions

邮差的信 提交于 2019-12-03 14:44:23
I am currently trying to sort the following list: list_ = [(1, '0101'), (1, '1010'), (1, '101'), (2, '01'), (2, '010'), (2, '10')] These are the steps I want to take in order to sort it: Sort the list by the value of the first element of the tuples Next, sort the list by the length of the second element of the tuples (not the value, the length!) AFTER step 1 finishes. Next, sort the list by the value of the second element of the tuples AFTER step 1 and step 2 finishes. My attempt: sorted_by_length = sorted(list_, key=len x:x[1]) However, I received a syntax error concerning the x after key=