tuples

Find the maximum value in a list of tuples in Python [duplicate]

百般思念 提交于 2019-12-18 10:05:59
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Sorting or Finding Max Value by the second element in a nested list. Python I have a list with ~10^6 tuples in it like this: [(101, 153), (255, 827), (361, 961), ...] ^ ^ X Y I want to find the maximum value of the Ys in this list, but also want to know the X that it is bound to. How do I do this? 回答1: Use max() : Using itemgetter() : In [53]: lis=[(101, 153), (255, 827), (361, 961)] In [81]: from operator

get the count of elements of tuples of your own…not just the range or sequence

时间秒杀一切 提交于 2019-12-18 09:55:11
问题 The below code is running for first three elements of the tuple of this list SS1=[(1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 5, 6), (1, 2, 4, 5, 6), (1, 3, 4, 5, 6), (2, 3, 4, 5, 6)] from collections import Counter c = Counter(elem[0:3] for elem in SS1) for k, v in c.items(): if (v > 0): print(k,v) and the output is: (1, 2, 3) 3 (1, 2, 4) 1 (1, 3, 4) 1 (2, 3, 4) 1 But my expectation is not just for first three tuple...i want the counter for tuple (0,2,3) or tuple (1,2,4) likewise i can pass

Tuple and function composition

天涯浪子 提交于 2019-12-18 06:09:18
问题 Is there a better way to express (\(a, b) -> a < b) with function composition? I feel like I'm missing something and experimenting with curry only confused me more. 回答1: curry is the wrong thing to use here; it turns a function operating on tuples into a curried function. You want the opposite, which is uncurry: uncurry :: (a -> b -> c) -> (a, b) -> c In this case, it's uncurry (<) . (Another useful source for combinators useful in writing functions on tuples is Control.Arrow; since (->) is

Haskell- Two lists into a list of tuples

跟風遠走 提交于 2019-12-18 04:52:49
问题 I am trying to implement a function (described below) that takes two lists (each or both may be infinite) and return a list of tuples of all the possible pairs of elements between the lists zipInf :: [a] -> [b] -> [(a,b)] (e.g the output should be like this, but doesn't have to be exactly like this) zipInf [0 .. 2] ['A' .. 'C'] ~> [(0,'A'),(1,'A'),(0,'B'),(1,'B'),(0,'C'),(2,'A'),(2,'B'),(1,'C'),(2,'C')] zipInf [] [0 ..] ~> [] zipInf [0 ..] [] ~> [] take 9 (zipInf ['A'] [0 .. ]) ~> [('A',0),(

Passing lists or tuples as arguments in django raw sql

旧巷老猫 提交于 2019-12-18 04:33:09
问题 I have a list and want to pass thru django raw sql. Here is my list region = ['US','CA','UK'] I am pasting a part of raw sql here. results = MMCode.objects.raw('select assigner, assignee from mm_code where date between %s and %s and country_code in %s',[fromdate,todate,region]) Now it gives the below error, when i execute it in django python shell Traceback (most recent call last): File "<console>", line 1, in <module> File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py",

Iterative find/replace from a list of tuples in Python

你说的曾经没有我的故事 提交于 2019-12-18 04:24:06
问题 I have a list of tuples, each containing a find/replace value that I would like to apply to a string. What would be the most efficient way to do so? I will be applying this iteratively, so performance is my biggest concern. More concretely, what would the innards of processThis() look like? x = 'find1, find2, find3' y = [('find1', 'replace1'), ('find2', 'replace2'), ('find3', 'replace3')] def processThis(str,lst): # Do something here return something >>> processThis(x,y) 'replace1, replace2,

Iterative find/replace from a list of tuples in Python

随声附和 提交于 2019-12-18 04:24:01
问题 I have a list of tuples, each containing a find/replace value that I would like to apply to a string. What would be the most efficient way to do so? I will be applying this iteratively, so performance is my biggest concern. More concretely, what would the innards of processThis() look like? x = 'find1, find2, find3' y = [('find1', 'replace1'), ('find2', 'replace2'), ('find3', 'replace3')] def processThis(str,lst): # Do something here return something >>> processThis(x,y) 'replace1, replace2,

How to write a tuple of tuples to a CSV file using Python

瘦欲@ 提交于 2019-12-18 04:21:09
问题 I have a tuple of tuples import csv A = (('Max', 3 ,' M'),('bob',5,'M'),('jane',6,'F')) result = open("newfile.csv",'wb') writer = csv.writer(result, dialect = 'excel') writer.writerow(A) result.close This writes a CSV file with rows with A[0], A[1] and A[2] . What i want is a row with name, age and gender , which has the corresponding values . 回答1: Write all rows at once: writer.writerows(A) instead of writer.writerow(A) File newfile.csv looks now like this: Max,3, M bob,5,M jane,6,F Also,

How to write a tuple of tuples to a CSV file using Python

我只是一个虾纸丫 提交于 2019-12-18 04:21:02
问题 I have a tuple of tuples import csv A = (('Max', 3 ,' M'),('bob',5,'M'),('jane',6,'F')) result = open("newfile.csv",'wb') writer = csv.writer(result, dialect = 'excel') writer.writerow(A) result.close This writes a CSV file with rows with A[0], A[1] and A[2] . What i want is a row with name, age and gender , which has the corresponding values . 回答1: Write all rows at once: writer.writerows(A) instead of writer.writerow(A) File newfile.csv looks now like this: Max,3, M bob,5,M jane,6,F Also,

How to reverse tuples in Python? [duplicate]

我只是一个虾纸丫 提交于 2019-12-18 03:00:44
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Traverse a list in reverse order in Python Is this possible? Doesn't have to be in place, just looking for a way to reverse a tuple so I can iterate on it backwards. 回答1: There are two idiomatic ways to do this: reversed(x) # returns an iterator or x[::-1] # returns a new tuple Since tuples are immutable, there is no way to reverse a tuple in-place. Edit: Building on @lvc's comment, the iterator returned by