tuples

How to add with tuples

痞子三分冷 提交于 2019-12-04 04:39:40
I have pseudo-code like this: if( b < a) return (1,0)+foo(a-b,b) I want to write it in python. But can python add tuples? What is the best way to code something like that? Do you want to do element-wise addition, or to append the tuples? By default python does (1,2)+(3,4) = (1,2,3,4) You could define your own as: def myadd(x,y): z = [] for i in range(len(x)): z.append(x[i]+y[i]) return tuple(z) Also, as @delnan's comment makes it clear, this is better written as def myadd(xs,ys): return tuple(x + y for x, y in izip(xs, ys)) or even more functionally: myadd = lambda xs,ys: tuple(x + y for x, y

Haskell List of Tuple Search

给你一囗甜甜゛ 提交于 2019-12-04 04:36:23
I have a list of tuples like this: [("username", 123), ("df", 54), ("as",2 34)] I need to search the value based on username. I have using lookup but i need to change the value of the integer and write back to file. My logic of this is to delete the tuple and insert another new tuple value rather than change it. Any idea how to do this ? Use Data.Map like this : import qualified Data.Map as Map m1 :: Map String Int m1 = Map.fromList [("username", 123), ("df", 54), ("as",234)] Let's replace 54 by 78 (on "df"): m2 = Map.insert "df" 78 m1 You can use insertWith' to combine the old and new values

Are Python3.5 tuple comprehension really this limited?

这一生的挚爱 提交于 2019-12-04 04:34:03
问题 I've been loving the tuple comprehensions added to Python3.5: In [128]: *(x for x in range(5)), Out[128]: (0, 1, 2, 3, 4) However, when I try to return a tuple comprehension directly I get an error: In [133]: def testFunc(): ...: return *(x for x in range(5)), ...: File "<ipython-input-133-e6dd0ba638b7>", line 2 return *(x for x in range(5)), ^ SyntaxError: invalid syntax This is just a slight inconvenience since I can simply assign the tuple comprehension to a variable and return the

Predicate to unzip a list

痞子三分冷 提交于 2019-12-04 04:22:28
问题 List1=[(x,1),(y,1),(z,1)] I'm attempting to split this list: into two lists: List3=[x,y,z] List4=[1,1,1] So I have written this predicate to try to do it: splt([], [], []). splt([X|Xs], [Y|Ys], [X,Y|Zs]) :- splt(Xs,Ys,Zs). However instead of the desired result, the predicate returns: 1 ?- splt([(x,1),(y,2),(z,3)],L3,L4). L3 = [_G1760, _G1769, _G1778], L4 = [ (z, 1), _G1760, (y, 2), _G1769, (z, 3), _G1778]. 回答1: First, the term you have chosen. This: (a, b) , is most definitely not how you

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

纵饮孤独 提交于 2019-12-04 04:19:21
问题 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,

Python: What is the difference between these two import statements?

好久不见. 提交于 2019-12-04 03:43:34
问题 They both functionally looks same to me. Are there any differences and advantages of using one over another? >>> from datetime import datetime, timedelta >>> from datetime import (datetime, timedelta) 回答1: If you wrap the imports in parens, you don't have to use a backslash for line continuation if you put a line break in the import statement, which is the preferred style. Functionally, they are identical, and if on one line, leaving out the parens is cleaner. 回答2: Both of them are same: In

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

陌路散爱 提交于 2019-12-04 03:41:06
This question already has answers here : Closed 5 years ago . What is the pythonic way to unpack tuples? [duplicate] (2 answers) 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? or any other workaround ideas? Martijn Pieters Pass in the tuple using *arg variable arguments call

Saving dictionary whose keys are tuples with json, python

别来无恙 提交于 2019-12-04 03:24:09
I am writing a little program in python and I am using a dictionary whose (like the title says) keys and values are tuples. I am trying to use json as follows import json data = {(1,2,3):(a,b,c),(2,6,3):(6,3,2)} print json.dumps(data) Problem is I keep getting TypeError: keys must be a string . How can I go about doing it? I tried looking at the python documentation but didn't see any clear solution. Thanks! You'll need to convert your tuples to strings first: json.dumps({str(k): v for k, v in data.iteritems()}) Of course, you'll end up with strings instead of tuples for keys: '{"(1, 2, 3)": [

What is the Triplet class used for? Is it related to Tuples?

℡╲_俬逩灬. 提交于 2019-12-04 03:22:54
问题 So I just learnt about the Triplet class. I have no experience with ASP.NET, only the core .NET Framework. Can someone explain to me where/why the Triplet class exists? Is it like a Tuple? 回答1: Yes, it's pretty much like Tuple from .NET 4.0, but dates back to .NET 1.0 and ASP.NET 1.0 in particular. It's primarily used in ViewState serialization: The Page class contains a SavePageViewState() , which is invoked during the page life cycle's save view state stage. The SavePageViewState() method

Return tuple with smallest y value from list of tuples

本秂侑毒 提交于 2019-12-04 03:20:29
I am trying to return a tuple the smallest second index value (y value) from a list of tuples. If there are two tuples with the lowest y value, then select the tuple with the largest x value (i.e first index). For example, suppose I have the tuple: x = [(2, 3), (4, 3), (6, 9)] The the value returned should be (4, 3) . (2, 3) is a candidate, as x[0][1] is 3 (same as x[1][1] ), however, x[0][0] is smaller than x[1][0] . So far I have tried: start_point = min(x, key = lambda t: t[1]) However, this only checks the second index, and does not compare two tuples first index if their second index's