tuples

How to convert an custom class object to a tuple in Python? [closed]

天涯浪子 提交于 2019-11-30 21:57:06
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 years ago . If we define __str__ method in a class: class Point(): def __init__(self, x, y): self.x = x self.y = y def __str__(self, key): return '{},{}'.format(self.x, self.y) So we can convert its object to str immediately: a = Point(1, 1) b = str(a) print(b) But as far as I know, there is not such __tuple__

Spark Scala 2.10 tuple limit

无人久伴 提交于 2019-11-30 21:52:11
I have DataFrame with 66 columns to process (almost each column value needs to be changed someway) so I'm running following statement val result = data.map(row=> ( modify(row.getString(row.fieldIndex("XX"))), (...) ) ) till 66th column. Since scala in this version has limit to max tuple of 22 pairs I cannot perform this like that. Question is, is there any workaround for it? After all line operations I'm converting it to df with specific column names result.toDf("c1",...,"c66") result.storeAsTempTable("someFancyResult") "modify" function is just an example to show my point If all you do is

Obtain a pointer to a C char array in Swift

只谈情不闲聊 提交于 2019-11-30 21:37:32
A have a structure like this (defined in bson.h of mongodb c driver): typedef struct { uint32_t domain; uint32_t code; char message[504]; } bson_error_t; In Swift I have a pointer to this structure like this: err: UnsafePointer<bson_error_t> = ... Now whatever I do I cannot convert message[504] (which Swift sees as a tuple of (Int8, Int8, Int8, ...504 times)) to char* to use it in String.fromCString(). Is it even possible to do that in Swift? As a temporary solution I created a helper C function in a separate .c file which takes err *bson_error_t and returns char* , but this is weird if Swift

tuple vector and initializer_list

萝らか妹 提交于 2019-11-30 21:31:06
问题 I tried to compile the following snippets with gcc4.7 vector<pair<int,char> > vp = {{1,'a'},{2,'b'}}; //For pair vector, it works like a charm. vector<tuple<int,double,char> > vt = {{1,0.1,'a'},{2,4.2,'b'}}; However, for the vector of tuples, the compiler complains: error: converting to ‘std::tuple’ from initializer list would use explicit constructor ‘constexpr std::tuple< >::tuple(_UElements&& ...) [with _UElements = {int, double, char}; = void; _Elements = {int, double, char}]’ The error

Writing a list of tuples to a text file in Python

允我心安 提交于 2019-11-30 21:23:32
I have a list of tuples in the format: ("some string", "string symbol", some number) For example, ("Apples", "=", 10) . I need to write them into the output file, like this: Apples = 10 I'm having trouble with the write method. How can it be done? You can use: for t in some_list: f.write(' '.join(str(s) for s in t) + '\n') where f is your file . U-DON list_of_tuples = [('Apples', '=', 10), ('Oranges', '<', 20)] f = open('file.txt', 'w') for t in list_of_tuples: line = ' '.join(str(x) for x in t) f.write(line + '\n') f.close() Given a list of tuples, you open a file in write mode. For each

Unable to pass pig tuple to python UDF

懵懂的女人 提交于 2019-11-30 21:04:10
问题 I have master.txt which has 10K records, so each line of it will be a tuple & whole of the same needs to be passed to python UDF. Since it has multiple records, so on storing p2preportmap getting following error. Please help Error is as follows: Unable to open iterator for alias p2preportmap. Backend error : org.apache.pig.backend.executionengine.ExecException: ERROR 0: Scalar has more than one row in the output. 1st : (010301,MTS,MM), 2nd :(010B06,MTS,TN) (common cause: "JOIN" then "FOREACH

python tuple is immutable - so why can I add elements to it

做~自己de王妃 提交于 2019-11-30 20:28:50
I've been using Python for some time already and today while reading the following code snippet: >>> a = (1,2) >>> a += (3,4) >>> a (1, 2, 3, 4) I asked myself a question: how come python tuples are immutable and I can use an += operator on them (or, more generally, why can I modify a tuple)? And I couldn't answer myself. I get the idea of immutability, and, although they're not as popular as lists, tuples are useful in python. But being immutable and being able to modify length seems contradictory to me... 5 is immutable, too. When you have an immutable data structure, a += b is equivalent to

Converting Swift Array to NSData for NSUserDefaults.StandardUserDefaults persistent storage

北城以北 提交于 2019-11-30 19:52:40
问题 I'm trying to get my head around Swift (after being relatively competent with Obj-C) by making a small app. I would like to use NSUserDefaults to persistently save a small amount of data but I am having problems. I initialise an empty array of tuples like this: var costCategoryArray: [(name:String, defaultValue:Int, thisMonthsEstimate:Int, sumOfThisMonthsActuals:Int, riskFactor:Float, monthlyAverage:Float)]=[] When the array has an entry, I want to save the array to NSUserDefaults with

Query Python dictionary to get value from tuple

两盒软妹~` 提交于 2019-11-30 19:43:42
Let's say that I have a Python dictionary, but the values are a tuple: E.g. dict = {"Key1": (ValX1, ValY1, ValZ1), "Key2": (ValX2, ValY2, ValZ2),...,"Key99": (ValX99, ValY99, ValY99)} and I want to retrieve only the third value from the tuple, eg. ValZ1, ValZ2, or ValZ99 from the example above. I could do so using .iteritems() , for instance as: for key, val in dict.iteritems(): ValZ = val[2] however, is there a more direct approach? Ideally, I'd like to query the dictionary by key and return only the third value in the tuple... e.g. dict[Key1] = ValZ1 instead of what I currently get, which is

Equivalent of std::transform for tuples

六眼飞鱼酱① 提交于 2019-11-30 18:54:16
I want a function that will behave like std::transform for tuples. Basically the functionality to be implemented is template<size_t From, size_t To, class Tuple, class Func> void tuple_transform(Tuple&& source, Tuple&& target, Func f) { // elements <From, To> of `target` ti become `f(si)`, where // si is the corresponding element of `source` }; I believe that to implement this I'll need a compile time integer range struct, a generalization of std::index_sequence and I've implemented it here with cti::range . I also believe that this type of compile time traversal is ideal here : template<class