tuples

Python `dict` indexed by tuple: Getting a slice of the pie

让人想犯罪 __ 提交于 2019-12-01 21:23:52
Let's say I have my_dict = { ("airport", "London"): "Heathrow", ("airport", "Tokyo"): "Narita", ("hipsters", "London"): "Soho" } What is an efficient (no scanning of all keys), yet elegant way to get all airports out of this dictionary, i.e. expected output ["Heathrow", "Narita"] . In databases that can index by tuples, it's usually possible to do something like airports = my_dict.get(("airport",*)) (but usually only with the 'stars' sitting at the rightmost places in the tuple since the index usually is only stored in one order). Since I imagine Python to index dictionary with tuple keys in a

Feed template function element from tuple at runtime?

旧时模样 提交于 2019-12-01 21:04:52
In C++ I have a tuple with some elements in it: std::tuple <int, char> my_tuple(3, 'q'); And some template function that perfectly works both on integers and chars: template <class T> void my_function(T); Now, say that at runtime I want to run my_function on one of the elements of my tuple (but I don't know which). I noticed that it is not possible to do something like: unsigned int n; // Give a value to n my_function(std::get <n> (my_tuple)); However, in principle what I need should be identical to something like: unsigned int n; // Give a value to n switch(n) { case 0: my_function(std::get

Sorting a list of based on the 2nd element of a tuple

安稳与你 提交于 2019-12-01 20:51:06
I have a dictionary and want to convert it to a list. Then I would like to sort the resulting list consisting of {Key, Value} pairs from min to max depending on the 2nd element(Value). Is there a built in sort method for Lists to handle this or how does one do this? Thanks The easiest way to sort by the second element would be to define your own sorting function that could work as follows: fun({KeyA,ValA}, {KeyB,ValB}) -> {ValA,KeyA} =< {ValB,KeyB} end. And call it in lists:sort/2 : 1> lists:sort(fun({KeyA,ValA}, {KeyB,ValB}) -> {ValA,KeyA} =< {ValB,KeyB} end., [{a,b},{b,a},{b,b}]). [{b,a},{a

Processing tuples in java

落花浮王杯 提交于 2019-12-01 20:50:35
I am processing some data with the following format: String s = "{(30,2884090,1410450570357,235),(30,2863348,1410451100148,285)}" Some doubts beset me: Are there two entries (tuples) in this String? Is there any off-the-shelf data structure I can use to parse this? Is there any way to figure out a pattern matching which can return a list of two Strings for the given String ? As far as I know, Java API does not have something that can be used out-of-box. You need to write a small parser for that. Writing a parser for something like this is trivial. Here is a good start: public class TupleParser

Why did python choose commas over parenthesis in tuple design?

烂漫一生 提交于 2019-12-01 20:50:17
From python wiki Multiple Element Tuples In Python, multiple-element tuples look like: 1,2,3 ... but again, it is the commas, not the parentheses, that define the tuple. Oh, really?! Then why: >>> tuple((((((1, 2, 3)))))) # creates a valid tuple # (1, 2, 3) >>> tuple(1, 2, 3, ) # But not here # TypeError: tuple() takes at most 1 argument (3 given) More seriously, I don't get why the parenthesis was not chosen over the commas? Because I think it would create a paradox when: >>> 1, # is a valid tuple # (1,) >>> tuple([1]) # Or this # (1,) >>> tuple(1) # But not this one # TypeError: 'int' object

python: tuple of dictionary to Dictionary

隐身守侯 提交于 2019-12-01 20:18:07
How can I convert tuple of dictionaries like example present below: ({(1, 2): 3}, {(1, 3): 5}, {(1, 4): 5}, {(2, 4): 5}, {(1, 5): 10}, {(2, 6): 9}, {(1, 6): 9}, {(2, 1): 2}, {(2, 2): 3}, {(2, 3): 5}, {(2, 5): 10}, {(1, 1): 2}) to a rather simpler form like dictionary: {(1, 1): 2, (1, 2): 3, (1, 3): 5, (1, 4): 5, (1, 5): 10, (1, 6): 9, (2, 1): 12, (2, 2): 7, (2, 3): 7, (2, 4): 3, (2, 5): 4, (2, 6): 2} just iterate on the tuples and rebuild the dictionary "flat" using a dictionary comprehension: a = ({(1, 2): 3}, {(1, 3): 5}, {(1, 4): 5}, {(2, 4): 5}, {(1, 5): 10}, {(2, 6): 9}, {(1, 6): 9}, {(2,

Convert a columns of string to list in pandas

喜欢而已 提交于 2019-12-01 20:10:58
I have a problem with the type of one of my column in a pandas dataframe. Basically the column is saved in a csv file as a string, and I wanna use it as a tuple to be able to convert it in a list of numbers. Following there is a very simple csv: ID,LABELS 1,"(1.0,2.0,2.0,3.0,3.0,1.0,4.0)" 2,"(1.0,2.0,2.0,3.0,3.0,1.0,4.0)" If a load it with the function "read_csv" I get a list of strings. I have tried to convert to a list, but I get the list version of a string: df.LABELS.apply(lambda x: list(x)) returns: ['(','1','.','0',.,.,.,.,.,'4','.','0',')'] Any idea on how to be able to do it? Thank you

Using array as tuple member: Valid C++11 tuple declaration?

∥☆過路亽.° 提交于 2019-12-01 19:33:47
The code below compiles fine with G++ 4.7.2: #include <tuple> std::tuple<float,int[2]> x; With clang++ 3.2, however, the following error is produced: error: array initializer must be an initializer list. If I remove the float type from the tuple declaration, the error disappears. Is the above tuple declaration valid? ( $CXX -std=c++11 -c file.cpp ) I don't think there is anything in the Standard that forbids your declaration. However, you will run into problems as soon as you try to initialise, copy, move or assign your tuples, because for these operations, all member types of the tuple must

using bisect on list of tuples but compare using first value only

烈酒焚心 提交于 2019-12-01 19:31:04
问题 I read that question about how to use bisect on a list of tuples, and I used that information to answer that question. It works, but I'd like a more generic solution. Since bisect doesn't allow to specify a key function, if I have this: import bisect test_array = [(1,2),(3,4),(5,6),(5,7000),(7,8),(9,10)] and I want to find the first item where x > 5 for those (x,y) tuples (not considering y at all, I'm currently doing this: bisect.bisect_left(test_array,(5,10000)) and I get the correct result

sorting tuples in python with a custom key

隐身守侯 提交于 2019-12-01 19:18:02
问题 Hi: I'm trying to sort a list of tuples in a custom way: For example: lt = [(2,4), (4,5), (5,2)] must be sorted: lt = [(5,2), (2,4), (4,5)] Rules: * b tuple is greater than a tuple if a[1] == b[0] * a tuple is greater than b tuple if a[0] == b[1] I've implemented a cmp function like this: def tcmp(a, b): if a[1] == b[0]: return -1 elif a[0] == b[1]: return 1 else: return 0 but sorting the list: lt.sort(tcmp) lt show me: lt = [(2, 4), (4, 5), (5, 2)] What am I doing wrong? 回答1: I'm not sure