tuples

How to map over a Pair/Tuple?

与世无争的帅哥 提交于 2019-12-11 02:57:20
问题 In Haskell I would do join (***) . In Idris flatten (***) does not work ( (***) is complicated). 回答1: In Idris, there's no Functor / Applicative / Monad instances for r -> _ and no Arrow instance for -> , only via Morphism, so using flatten to do \f x -> f x x leads to horribly verbose code full of going from/to Morphism . You can do it, of course, I'm just not sure it's worth it... compare this: import Control.Arrow import Data.Morphisms both : (a -> b) -> (a, a) -> (b, b) both = applyMor .

finding maximum value in python list of tuples [duplicate]

寵の児 提交于 2019-12-11 02:35:46
问题 This question already has answers here : How does tuple comparison work in Python? (4 answers) Closed 4 years ago . I have a list of tuples (list): ('2015-06-19', 3453455, 5, 'Scheduled') ('2015-05-19', 6786788, 6, 'Overdue') ('2015-04-19', 2342344, 2, 'Not Received') ('2015-03-19', 9438549, 0, 'Not Received') ('2015-02-19', 6348759, 7, 'Not Received') When I run this, I get this: >>> print(max(list)) ('2015-06-19', 3453455, 5, 'Scheduled') Obviously, max(list) determined the max based on the

Computing mean of all tuple values where 1st number is similar

北城以北 提交于 2019-12-11 02:29:08
问题 Consider list of tuples [(7751, 0.9407466053962708), (6631, 0.03942129), (7751, 0.1235432)] how to compute mean of all tuple values in pythonic way where 1st number is similar? for example the answer has to be [(7751, 0.532144902698135), (6631, 0.03942129)] 回答1: One way is using collections.defaultdict from collections import defaultdict lst = [(7751, 0.9407466053962708), (6631, 0.03942129), (7751, 0.1235432)] d_dict = defaultdict(list) for k,v in lst: d_dict[k].append(v) [(k,sum(v)/len(v))

Django tuple checking: TEMPLATE_DIRS should be tuple?

两盒软妹~` 提交于 2019-12-11 02:27:05
问题 I'm trying Django 1.7. This is my TEMPLATE_DIRS setting: TEMPLATE_DIRS = ( os.path.join(os.path.dirname(__file__), 'templates').replace('\\', '/') ) which is fine for Django 1.6, but doesn't work for Django 1.7. Can someone explains this? Thx!! 回答1: You need a trailing , for it to be a tuple, see below, the last , TEMPLATE_DIRS = ( os.path.join(os.path.dirname(__file__), 'templates').replace('\\', '/'), ) When there's a single element in the tuple you need to leave a trailing comma at the end

Split list by tuple separator

可紊 提交于 2019-12-11 02:13:27
问题 I have list: print (L) [('I', 'WW'), ('am', 'XX'), ('newbie', 'YY'), ('.', 'ZZ'), ('You', 'WW'), ('are', 'XX'), ('cool', 'YY'), ('.', 'ZZ')] I want split list to sublists with separator ('.', 'ZZ') : print (new_L) [[('I', 'WW'), ('am', 'XX'), ('newbie', 'YY'), ('.', 'ZZ')], [('You', 'WW'), ('are', 'XX'), ('cool', 'YY'), ('.', 'ZZ')]] I am interested about another possible solutions, performance is important. 回答1: The for-loop approach will be faster, this requires only one-pass: >>> def juan

Filter a tuple of types in c++17

廉价感情. 提交于 2019-12-11 01:48:26
问题 std::tuple a{1,3,4,5} -> make it to numbers greater than 3 std::tuple b{4,5} Or std::tuple a{ std::integral_constant<int,1> {}, std::integral_constant<int,3> {}, std::integral_constant<int,4> {}, std::integral_constant<int,5> {} } to std::tuple a{ std::integral_constant<int,4>{}, std::integral_constant<int,5>{} }; How to convert this at compile time? I can do this using integer_sequence but that is a cumbersome. Is there a simpler way in C++17 using fold expressions or std::apply Also after

convert List[Tuple2[A,B]] to Tuple2[Seq[A],Seq[B]]

时光总嘲笑我的痴心妄想 提交于 2019-12-11 01:40:59
问题 Stuck here, trying to convert a List of case class tuples to a tuple of sequences and multi-assign the result. val items = repo.foo.list // gives me a List[(A,B)] I can pull off multi-assignment like so: val(a,b) = (items.map(_._1).toSeq, items.map(_._2).toSeq) but it would be nicer to do in 1 step, along the lines of: val(a,b) = repo.foo.list.map{case(a,b) => (a,b)} 回答1: I am not sure if I understood the question correctly. Maybe unzip works for what you want? Here is a link with some

Merge Multiple Lists of Lists Based on Template

血红的双手。 提交于 2019-12-11 01:14:21
问题 I have 3 DB calls returning a tuple of tuples with a name, code and count like so: year = (('Fanklin Grand Isle', '5560', 1), ('Windham', '3457', 1)) month = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '3367', 1)) week = (('Fanklin Grand Isle', '5560', 1), ('Chittendon', '3367', 1)) I need to merge these all together so that they will have the Name, code and count for yearly, monthly and weekly. My problem is that if there is no record I need to insert the Name and code in AND a 0

Tuples partial match

∥☆過路亽.° 提交于 2019-12-11 00:38:13
问题 I have a tuple of tuples and a tuple. I'm interested to know which elements of the first tuple match the second tuple (if any), considering partial matches too. This is a filter function to demonstrate what I mean. def f(repo): pattern = (None, None, '1.3') for idx, item in enumerate(pattern): if item != None and item != repo[idx]: return False return True >>> repo = (('framework', 'django', '1.3'), ('cms', 'fein', '1.3'), ('cms', 'django-cms', '2.2')) >>> filter(f, repo) (('framework',

Parse equation to list of tuples in Python

大兔子大兔子 提交于 2019-12-11 00:34:31
问题 I want to parse equations and get a list of tuples. For example, when I enter 2x = 4+3y, I want to get [('', '2', 'x', '='), ('','4','',''), ('+','3','y','')] This is my regex so far: ([+-]*)([0-9]+)([a-z]*)([<=>]*) It works fine for the above query but it does not capture equations like 2 = x +3y , (where x does not have any coefficient) How do I capture that? 回答1: (\d*)(\w*) *(=) *(\d*)(\w*) *[+|\-|*|/] *(\d*)(\w*) How about this regex? It separates all operands and operators. (and inside