tuples

Pythonistic way to intersect and add elements of lists at the same time

你离开我真会死。 提交于 2019-12-05 22:42:31
I have 3 lists, a , b and c Each of this lists contains tuples with 3 numbers. Here is an example input: a = [(1,2,4),(1,7,8),(1,5,4),(3,6,7)] b = [(1,2,5),(1,9,3),(1,0,3),(3,6,8)] c = [(2,6,3),(2,4,9),(2,8,5),(1,2,7)] I'm looking for a way to generate a list that takes elements of those 3 lists if the two firsts items of eachs tuple are equals, and addind the third element. In the data I gave, there is only 1 set of tuple with the 2 first values equals : (1,2,4) , (1,2,5) and (1,2,7) . If I add their third value I have 4+5+7 = 16 , so with those data, I should have [(1,2,16)] in the end. The

Turning a generator of pairs into a pair of generators

自古美人都是妖i 提交于 2019-12-05 22:22:37
问题 How would I turn a generator of pairs (tuples): tuple_gen = (i for i in [(1, "a"), (2, "b"), (3, "c")]) Into two generators which would yield [1, 2, 3] and ["a", "b", "c"] ? I need to process separately the first and second elements of the tuples and the processing functions expect an iterable. The generator is very large (millions of items) so I'd like to avoid having all items in memory at the same time unless there is no other solution. 回答1: You can create n distinct iterators using the

Python 3 - using tuples in str.format() [duplicate]

≯℡__Kan透↙ 提交于 2019-12-05 22:07:16
问题 This question already has answers here : What is the pythonic way to unpack tuples? [duplicate] (2 answers) Closed 5 years ago . I'm trying to use the str.format() method, and having some difficulties when my values are stored within a tuple. For example, if I do: s = "x{}y{}z{}" s.format(1,2,3) Then I get 'x1y2z3' - no problem. However, when I try: s = "x{}y{}z{}" tup = (1,2,3) s.format(tup) I get IndexError: tuple index out of range. So how can I 'convert' the tuple into separate variables?

Adding an entry to a python tuple

拜拜、爱过 提交于 2019-12-05 21:43:15
I have a list of tuples representing x,y points. I also have a list of values for each of these points. How do I combine them into a list of lists (i.e one entry for each point [x,y,val]) or a list of tuples? Thanks You can't add entries to tuples, since tuples are immutable. But you can create a new list of lists: new = [[x, y, val] for (x, y), val in zip(points, vals)] Tuples are immutable, they can't be modified. Convert it to a list, then convert it back if you want to (list((a, b))). 来源: https://stackoverflow.com/questions/2282300/adding-an-entry-to-a-python-tuple

C++0x: iterating through a tuple with a function

喜你入骨 提交于 2019-12-05 21:42:47
I have a function named _push which can handle different parameters, including tuples, and is supposed to return the number of pushed elements. For example, _push(5) should push '5' on the stack (the stack of lua ) and return 1 (because one value was pushed), while _push(std::make_tuple(5, "hello")) should push '5' and 'hello' and return 2. I can't simply replace it by _push(5, "hello") because I sometimes use _push(foo()) and I want to allow foo() to return a tuple. Anyway I can't manage to make it work with tuples: template<typename... Args, int N = sizeof...(Args)> int _push(const std:

How to access specific element of dictionary of tuples

陌路散爱 提交于 2019-12-05 21:35:46
I want to access a specific element of a tuple in a dictionary of tuples. Let's say that I have a dictionary with a unique key, and a tuple with three values, for each key. I want to write a iterator the prints every third item in a tuple for every element in the dictionary. For example dict = {"abc":(1,2,3), "bcd":(2,3,4), "cde", (3,4,5)} for item in dict: print item[2] But this returns c d e Where am I going wrong? for item in dict: print dict[item][2] Also, you should not name anything after a built-in, so name your dictionary 'd' or something other than 'dict' for item in dict: does the

Python tuples as keys slow?

不打扰是莪最后的温柔 提交于 2019-12-05 21:27:51
问题 I'm trying to implement an fast lookup for sorted tuples in a dictionary; something that answers the question "Does the tuple (3,8) have an associated value, and if yes, what is it?". Let the integers in the tuples be bound from below by 0 and from above by max_int. I went ahead and used Python's dict but found that to be pretty slow. Another approach to this problem would be to create a list T with max_int (mostly empty) dicts, and for each tuple (3,8) put T[3][8] = value. I though this is

Asp.net mvc 2 .net 4.0 error when View model type is Tuple with more than 4 items

馋奶兔 提交于 2019-12-05 21:15:27
When I create strongly typed View in Asp.net mvc 2, .net 4.0 with model type Tuple I get error when Tuple have more than 4 items example 1: type of view is Tuple<string, string, string, string> (4-tuple) and everything works fine view: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/WebUI.Master" Inherits="System.Web.Mvc.ViewPage<Tuple<string, string, string, string>>" %> controller: var tuple = Tuple.Create("a", "b", "c", "d"); return View(tuple); example 2: type of view is Tuple<string, string, string, string, string> (5-tuple) and I have this error: Compiler Error Message:

Readable convention for unpacking single value tuple

余生长醉 提交于 2019-12-05 21:13:30
问题 There are some related questions about unpacking single-value tuples, but I'd like to know if there is a preferred method in terms of readability for sharing and maintaining code. I'm finding these to be a source of confusion or misreading among colleagues when they involve a long function chain such as an ORM query. Is there some convention for this similar to the pep8 guidelines? If not, which is the clearest, most readable way to do it? Below are the ways I've tried, and my thoughts on

Finding groups of increasing numbers in a list

僤鯓⒐⒋嵵緔 提交于 2019-12-05 20:32:16
问题 The aim is to find groups of increasing/monotonic numbers given a list of integers. Each item in the resulting group must be of a +1 increment from the previous item Given an input: x = [7, 8, 9, 10, 6, 0, 1, 2, 3, 4, 5] I need to find groups of increasing numbers and achieve: increasing_numbers = [(7,8,9,10), (0,1,2,3,4,5)] And eventually also the number of increasing numbers: len(list(chain(*increasing_numbers))) And also the len of the groups: increasing_num_groups_length = [len(i) for i