tuples

Writing a list of tuples to a text file in Python

Deadly 提交于 2019-11-30 05:21:39
问题 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? 回答1: You can use: for t in some_list: f.write(' '.join(str(s) for s in t) + '\n') where f is your file . 回答2: list_of_tuples = [('Apples', '=', 10), ('Oranges', '<', 20)] f = open('file.txt', 'w') for t in list_of_tuples: line = ' '.join(str(x) for

How can I use shared_ptr using PostThreadMessage?

佐手、 提交于 2019-11-30 05:13:24
问题 I would like to upgrade my MFC production code to use the std::shared_ptr smart pointer when calling other windows or threads. Such calls are SendMessage , PostMessage and PostThreadMessage which pass wparam and lparam and which respectively are an unsigned int and long . Currently, I create a class object, new an object, make the call passing a pointer to the object, use the object on the receiving end and then delete it. Since shared_ptr works so well in the rest of my code I wanted to at

How to query a constexpr std::tuple at compile time?

只愿长相守 提交于 2019-11-30 04:55:16
In C++0x, one can create a constexpr std::tuple, e.g. like #include <tuple> constexpr int i = 10; constexpr float f = 2.4f; constexpr double d = -10.4; constexpr std::tuple<int, float, double> tup(i, f, d); One also can query a std::tuple at runtime, e.g. via int i2 = std::get<0>(tup); But it is not possible to query it at compile time, e.g., constexpr int i2 = std::get<0>(tup); will throw a compilation error (at least with the latest g++ snapshot 2011-02-19). Is there any other way to query a constexpr std::tuple at compile time? And if not, is there a conceptual reason why one is not

How to flatten a nested tuple?

浪子不回头ぞ 提交于 2019-11-30 04:46:07
I have a nested tuple structure like (String,(String,Double)) and I want to transform it to (String,String,Double) . I have various kinds of nested tuple, and I don't want to transform each manually. Is there any convenient way to do that? If you use shapeless , this is exactly what you need, I think. There is no flatten on a Tupple. But if you know the structure, you can do something like this: implicit def flatten1[A, B, C](t: ((A, B), C)): (A, B, C) = (t._1._1, t._1._2, t._2) implicit def flatten2[A, B, C](t: (A, (B, C))): (A, B, C) = (t._1, t._2._1, t._2._2) This will flatten Tupple with

overloading operator << for std::tuple - possible simplications?

假如想象 提交于 2019-11-30 04:41:19
问题 I used an answer to the SO question "iterate over tuple" to write a method to overload << . This method was tested and appears to work correctly with g++ 4.7 on Debian squeeze. However this method is kind of roundabout, since it seems << cannot be explicitly instantiated (I found a post about it here). So, one is forced to define a string method and then call that. I have a similar method for vector, which is more direct. Does anyone have suggestions about how to eliminate the extra step of

Sort a list of tuples depending on two elements [duplicate]

[亡魂溺海] 提交于 2019-11-30 04:31:50
Possible Duplicate: python: how to sort a complex list on two different keys I've got a list of tuples. I want to sort them depending two elements. Here is following example unsorted = [('a', 4, 2), ('a', 4, 3), ('a', 7, 2), ('a', 7, 3), ('b', 4, 2), ('b', 4, 3), ('b', 7, 2), ('b', 7, 3)] sorted = [('a', 4, 2), ('b', 4, 2), ('a', 4, 3), ('b', 4, 3), ('a', 7, 2), ('b', 7, 2), ('a', 7, 3), ('b', 7, 3)] I know how to sort them on the second element: sorted(unsorted, key = lambda element : element[1]) But how to do that with two keys? sorted(unsorted, key=lambda element: (element[1], element[2]))

Query Python dictionary to get value from tuple

余生颓废 提交于 2019-11-30 04:19:49
问题 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

Strongly typed access to csv in scala?

柔情痞子 提交于 2019-11-30 04:15:24
I would like to access csv files in scala in a strongly typed manner. For example, as I read each line of the csv, it is automatically parsed and represented as a tuple with the appropriate types. I could specify the types beforehand in some sort of schema that is passed to the parser. Are there any libraries that exist for doing this? If not, how could I go about implementing this functionality on my own? product-collections appears to be a good fit for your requirements: scala> val data = CsvParser[String,Int,Double].parseFile("sample.csv") data: com.github.marklister.collections.immutable

Python: what is the difference between (1,2,3) and [1,2,3], and when should I use each?

99封情书 提交于 2019-11-30 04:11:27
In many places, (1,2,3) (a tuple) and [1,2,3] (a list) can be used interchangeably. When should I use one or the other, and why? From the Python FAQ : Lists and tuples, while similar in many respects, are generally used in fundamentally different ways. Tuples can be thought of as being similar to Pascal records or C structs; they're small collections of related data which may be of different types which are operated on as a group. For example, a Cartesian coordinate is appropriately represented as a tuple of two or three numbers. Lists, on the other hand, are more like arrays in other

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

左心房为你撑大大i 提交于 2019-11-30 03:59:15
问题 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