tuples

Element-wise tuple addition

非 Y 不嫁゛ 提交于 2019-12-03 14:35:41
问题 I have some values held in a tuple, and I am looking to add another tuple to it element-wise. So I would like functionality like this: std::tuple<int,int> a = {1,2}; std::tuple<int,int> b = {2,4}; std::tuple<int,int> c = a + b; // possible syntax 1 a += b; // possible syntax 2 a += {2,4}; // possible syntax 3 Where the output tuple would have the value {3,6} Was looking at CPP reference, but I couldn't find this functionality. It's possible that this and this question are relevant, however

Finding index of pairwise elements

孤人 提交于 2019-12-03 14:25:21
Given the target ('b', 'a') and the inputs: x0 = ('b', 'a', 'z', 'z') x1 = ('b', 'a', 'z', 'z') x2 = ('z', 'z', 'a', 'a') x3 = ('z', 'b', 'a', 'a') The aim is to find the location of the continuous ('b', 'a') element and get the output: >>> find_ba(x0) 0 >>> find_ba(x1) 0 >>> find_ba(x2) None >>> find_ba(x3) 1 Using the pairwise recipe: from itertools import tee def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = tee(iterable) next(b, None) return zip(a, b) I could do this to get the desired output: def find_ba(x, target=('b', 'a')): try: return next(i for i, pair in

Erlang : Tuple List into JSON

孤人 提交于 2019-12-03 12:51:23
I have a list of tuples which are http headers. I want to convert the list to a JSON object. I try mochijson2 but to no avail. So I have the following : [{'Accept',"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}, {'Accept-Charset',"ISO-8859-1,utf-8;q=0.7,*;q=0.7"}, {'Accept-Encoding',"gzip,deflate"}, {'Accept-Language',"en-us,en;q=0.5"}, {'Cache-Control',"max-age=0"}, {'Connection',"close"}, {'Cookie',"uid=CsDbk0y1bKEzLAOzAwZUAg=="}, {'User-Agent',"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10"}] And would like this ( a

Python tuple vs generator

ぐ巨炮叔叔 提交于 2019-12-03 12:03:24
I am having a problem understanding why one of the following line returns generator and another tuple. How exactly and why a generator is created in the second line, while in the third one a tuple is produced? sample_list = [1, 2, 3, 4] generator = (i for i in sample_list) tuple_ = (1, 2, 3, 4) print type(generator) <type 'generator'> print type(tuple_) <type 'tuple'> Is it because tuple is immutable object and when I try to unpack list inside () , it can't create the tuple as it has to change the tuple tuple. You can imagine tuples as being created when you hardcode the values, while

How can I use the new Slick 2.0 HList to overcome 22 column limit?

谁都会走 提交于 2019-12-03 11:38:16
问题 I'm currently writing Slick code to target an old schema with two tables > 22 columns. How do I use the new HList code? I've got 2.0-M3 working fine in other respects under Scala 2.10.3. Here's the syntax I'm currently using with case classes / tuples. What would I do to use the new HLists mentioned in the docs? case class Joiner( id: Int, name: Option[String], contact: Option[String] ) class Joiners(tag: Tag) extends Table[Joiner](tag, "joiner") { def id = column[Int]("id", O.PrimaryKey, O

Can I use an object (an instance of a class) as a dictionary key in Python?

荒凉一梦 提交于 2019-12-03 11:30:47
I want to use a class instance as a dictionary key, like: classinstance = class() dictionary[classinstance] = 'hello world' Python seems to be not able to handle classes as dictionary key, or am I wrong? In addition, I could use a Tuple-list like [(classinstance, helloworld),...] instead of a dictionary, but that looks very unprofessional. Do you have any clue for fixing that issue? Buttons840 Your instances need to be hashable. The python glossary tells us: An object is hashable if it has a hash value which never changes during its lifetime (it needs a __hash__() method), and can be compared

From tuples to multiple columns in pandas

守給你的承諾、 提交于 2019-12-03 11:25:11
How do I convert this dataframe location value 0 (Richmond, Virginia, nan, USA) 100 1 (New York City, New York, nan, USA) 200 to this: city state region country value 0 Richmond Virginia nan USA 100 1 New York City New York nan USA 200 Note that the location column in the first dataframe contains tuples. I want to create four columns out of the location column. new_col_list = ['city','state','regions','country'] for n,col in enumerate(new_col_list): df[col] = df['location'].apply(lambda location: location[n]) df = df.drop('location',axis=1) If you return a Series of the (split) location, you

Feeding tuple into function such as printfn

房东的猫 提交于 2019-12-03 11:21:10
I want to give a tuple to a printf function: let tuple = ("Hello", "world") do printfn "%s %s" tuple This, of course, does not work, compiler first says, that it needs string instead of string*string . I write it as follows: let tuple = ("Hello", "world") do printfn "%s %s" <| fst tuple Then compiler reasonably notes that now I have function value of type string -> unit . Makes sense. I can write let tuple = ("Hello", "world") do printfn "%s %s" <| fst tuple <| snd tuple And it works for me. But I'm wondering, if there might be any way to do it nicer, like let tuple = ("Hello", "world") do

How to use priority queues in Scala?

梦想与她 提交于 2019-12-03 11:02:22
I am trying to implement A* search in Scala (version 2.10), but I've ran into a brick wall - I can't figure out how to use Scala's Priority Queue. It seems like a simple task, but searching on Google didn't turn up anything (except for a single code sample that stopped working back in version 2.8) I have a set of squares, represented by (Int, Int) s, and I need to insert them with priorities represented by Int s. In Python it's pretty simple, since you just have a list of key, value pairs and use the heapq functions to sort it. But it appears that Scala's tuples aren't even comparable. So how

Accessing a specific member in a F# tuple

你。 提交于 2019-12-03 10:34:32
问题 In F# code I have a tuple: let myWife=("Tijana",32) I want to access each member of the tuple separately. For instance this what I want to achieve by I can't Console.WriteLine("My wife is {0} and her age is {1}",myWife[0],myWife[1]) This code doesn't obviously work, by I think you can gather what I want to achieve. 回答1: You want to prevent your wife from aging by making her age immutable? :) For a tuple that contains only two members, you can fst and snd to extract the members of the pair.