tuples

Sorting a list of tuples with 3 elements in python

大兔子大兔子 提交于 2019-12-02 01:31:53
I have a list of some tuples. Each tuple has three elements. I need to sort the list. To break tie between two tuples, first element of tuple is looked then if still tied then the second element. List is like below. L = [(1, 14, 0), (14, 1, 1), (1, 14, 2), (14, 2, 3), (2, 4, 4), (4, 11, 5), (11, -1000, 6)] In C the sort function takes a compare function and that does everything simply. But I couldn't figure it out after trying for sometimes in python . Can anybody help me? Martijn Pieters Just sort the list; the default sort does just what you want. When comparing two tuples, they are ordered

Why did python choose commas over parenthesis in tuple design?

♀尐吖头ヾ 提交于 2019-12-02 01:16:08
问题 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

How To Extract Tuple Data into Single Element Format

﹥>﹥吖頭↗ 提交于 2019-12-02 01:02:33
I get back good results from the following, but how to I extract that data from the tuple? In other words, how do I clean up the data? Here is the data from the database, I ran out. >>> policy_id = ((2309L,), (118L,), (94L,)) >>> for i in policy_id: print i (2309L,) (118L,) (94L,) But I want the result as: 2309 118 94 policy_id = ((2309L,), (118L,), (94L,)) for i in policy_id: print i[0] >>> from itertools import chain >>> policy_id = ((2309L,), (118L,), (94L,)) >>> for i in chain.from_iterable(policy_id): print i 2309 118 94 print '\n'.join(str(x[0]) for x in policy_id) A notion that I like,

Find index of nested item in python

ε祈祈猫儿з 提交于 2019-12-02 00:36:57
I've been working with some relatively complex arrays such as: array = [ "1", 2, ["4", "5", ("a", "b")], ("c", "d")] and I was looking for a way to find an item and retrieve is "index" (Is it OK to refer to the location of item such as "a" - that is inside a Tuple as index on the same level as array?) Now my first thought was to use something like a simple helper function such as: def myindex(nestedlist, item): for i in nestedlist: if item in i: index = [] index.append(i) index.append(i.index(item)) return index But I'm sure you can guess that such a function won't do much good, especially

Single-element parethesized expressions/tuples vs common use of parentheses

喜欢而已 提交于 2019-12-02 00:06:22
Sorry if this is trivial - I am so new to swift, actually I have only looked at the language guide+reference for a few minutes. As far as I understand a parenthesized expression like (2,3) is used to construct a tuple, and (2) is a single-element tuple of type (Int) . But then what happens with common use of parentheses like (2+4) in expression (2+4)*5 ? Is this still a tuple of type (Int) multiplied by an Int ? From Types in the Swift book: If there is only one element inside the parentheses, the type is simply the type of that element. For example, the type of (Int) is Int , not (Int) . So

To sum up values of same items in a list of tuples while they are string

旧巷老猫 提交于 2019-12-01 23:23:26
If I have list of tuples like this: my_list = [('books', '$5'), ('books', '$10'), ('ink', '$20'), ('paper', '$15'), ('paper', '$20'), ('paper', '$15')] how can I turn the list to this: [('books', '$15'), ('ink', '$20'), ('paper', '$50')] i.e. to add the expense of same item while both the items are string in the tuples. I have problem with the price items being string. Any hint would be greatly appreciated. Thanks a lot! I am getting the first list in this way: my_list=[] for line in data: item, price = line.strip('\n').split(',') cost = ["{:s}".format(item.strip()), "${:.2f}".format(float

python: tuple of dictionary to Dictionary

别来无恙 提交于 2019-12-01 23:18:54
问题 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} 回答1: just iterate on the tuples and rebuild the dictionary "flat" using a dictionary

Feed template function element from tuple at runtime?

别说谁变了你拦得住时间么 提交于 2019-12-01 22:55:50
问题 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

python: group elements of a tuple having the same first element

我的梦境 提交于 2019-12-01 22:27:56
i have a tuple like this [ (379146591, 'it', 55, 1, 1, 'NON ENTRARE', 'NonEntrate', 55, 1), (4746004, 'it', 28, 2, 2, 'NON ENTRARE', 'NonEntrate', 26, 2), (4746004, 'it', 28, 2, 2, 'TheBestTroll Group', 'TheBestTrollGroup', 2, 3) ] i would like to get instead this: [ (379146591, (('it', 55, 1, 1, 'NON ENTRARE', 'NonEntrate', 55, 1)), (4746004, (('it', 28, 2, 2, 'NON ENTRARE', 'NonEntrate', 26, 2), ('it', 28, 2, 2, 'TheBestTroll Group', 'TheBestTrollGroup', 2, 3))) ] so the for any element, anything that is not the first element is inside a sub-tuple of it, and if the following element has the

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

感情迁移 提交于 2019-12-01 21:59:22
问题 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 回答1: 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>