tuple

python: list vs tuple, when to use each?

In Python, when should you use lists and when tuples? Sometimes you don't have a choice, for example if you have "hello %s you are %s years old" % x then x must be a tuple. But if I am the one who designs the API and gets to choose the data types, then what are the guidelines? ...

How can I create a typed Tuple2 from Java / Spring?

I want to be able to create a Tuple2 from spring config where I explicitly declare the types of my parameters: <bean class="scala.Tuple2"> <constructor-arg index="0" value="Europe/London" type="java.util.TimeZone" /> <constructor-arg index="1" value="America/New_York" type="java.util.TimeZone" /> </bean> This does not work...

Tuples in Dicts

Is it possible in python to add a tuple as a value in a dictionary? And if it is,how can we add a new value, then? And how can we remove and change it? ...

Why the tuple type can not be inferred in the list recursion?

I want to refine the raw text by using regular expression, given a list of (patten,replacement) tuple. I tried to use the patten matching on the list element but failed, the error showed that "This expression was expected to have type string * string list but here has type 'a list". How can I fix this problem? Thanks a lot. Codes ar...

Why can't I join this tuple in Python?

e = ('ham', 5, 1, 'bird') logfile.write(','.join(e)) I have to join it so that I can write it into a text file. ...

Why can't I sort this list?

statlist = [('abc',5,1), ('bzs',66,1), ... ] sorted(statlist, key=lambda x: int(x[1])) I want to sort it by the integer largest to smallest. In this case, 5 and 66. But it doesn't seem to be working. ...

F# match active pattern as expanded tuple

I get the following error in diff with a red squiggle under Subset. Type mismatch. Expecting a Range -> Choice but given a Range * Range -> Choice Is there some sort of type annotation I can add to the SubSet match so I don't have to use fst and snd? If not is there any intention to support this syntax? type Range = {min : int64; max ...

Translating python dictionary to C++

I have python code that contains the following code. d = {} d[(0,0)] = 0 d[(1,2)] = 1 d[(2,1)] = 2 d[(2,3)] = 3 d[(3,2)] = 4 for (i,j) in d: print d[(i,j)], d[(j,i)] Unfortunately looping over all the keys in python isn't really fast enough for my purpose, and I would like to translate this code to C++. What is the best C++ dat...

In what case would I use a tuple as a dictionary key?

I was studying the difference between lists and tuples (in Python). An obvious one is that tuples are immutable (the values cannot be changed after initial assignment), while lists are mutable. A sentence in the article got me: Only immutable elements can be used as dictionary keys, and hence only tuples and not lists can be us...

how to cache a lambda in c++0x ?

Hello, I'm trying to work with lambda's in C++ after having used them a great deal in C#. I currently have a boost tuple (this is the really simplified version). typedef shared_ptr<Foo> (*StringFooCreator)(std::string, int, bool) typedef tuple<StringFooCreator> FooTuple I then load a function in the global namespace into my FooTuple...

Pattern matching against a tuple in the IO Monad in Haskell

I've been studying Haskell in my spare time and have recently crossed into the area of monadic functions. I've distilled the code from an excercise I've been working on into this very contrived example to isolate the exact problem I'm having: import System.Random rndPermu :: [a] -> IO (a, [a]) rndPermu xs = (front, back) where (fro...

Getting the first elements per row in an array in Python?

Let's say i have an array of Tuples, s, in the form of: s = ((1, 23, 34),(2, 34, 44), (3, 444, 234)) and i want to return another Tuple, t, consisting of the first element per row: t = (1, 2, 3) Which would be the most efficient method to do this? I could of course just iterate through s, but is there any slicker way of doing it? ...

What's the best practice for handling single-value tuples in Python?

I am using a 3rd party library function which reads a set of keywords from a file, and is supposed to return a tuple of values. It does this correctly as long as there are at least two keywords. However, in the case where there is only one keyword, it returns a raw string, not a tuple of size one. This is particularly pernicious because ...

how to declare a byte[] in C# so that ironpython interprets it as byte[] and not as a tuple

In a C++/CLI we have a function that returns this: array^ OutBuffer = gcnew array(BufferSize); IronPython treats it as a byte[]. In C#, we have a funtion that returns this: OutBuffer = new Byte[InBuffer.Length]; While a C# client treats Outbuffer as a byte[], IronPython treats it as a tuple containing multiple arrays. How do we mak...

In Scala, is there a way to take convert two lists into a Map?

I have a two lists, a List[A] and a List[B]. What I want is a Map[A,B] but I want the semantics of zip. So started out like so: var tuplesOfAB = listOfA zip listOfB Now I'm not sure how to construct a Map from my tuplesOfAB. As a follow-up question, I also want to invert my map so that from a Map[A,B] I can create a Map[B,A]. Can an...

Adding values in a tuple that is in a list in python

I retrieve some data from a database which returns it in a list of tuple values such as this: [(1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,)] Is there a function that can sum up the values in the list of tuples? For example, the above sample should return 18. ...

Giving 'hints' problem

I have a simple word jumble game. I made the jumble already, but now I want to add a 'hint' system. I don't know how to have 1 item from tuples show up. I have 2 tuples, and I want to pull from the 2nd tuple based on the what the first tuple is. I have a WORD=("x", "y", "z") and HINT=("x", "y", "z"). When the user enters "hint", I want t...

A list vs. tuple situation in Python

Is there a situation where the use of a list leads to an error, and you must use a tuple instead? I know something about the properties of both tuples and lists, but not enough to find out the answer to this question. If the question would be the other way around, it would be that lists can be adjusted but tuples don't. ...

Are there more ways to define a tuple with only one item?

I know this is one way, by placing a comma: >>> empty = () >>> singleton = 'hello', # <-- note trailing comma >>> len(empty) 0 >>> len(singleton) 1 >>> singleton ('hello',) Source: http://docs.python.org/tutorial/datastructures.html Are there more ways to define a tuple with only 1 item? ...

Export list as .txt (Python)

My Python module has a list that contains all the data I want to save as a .txt file somewhere. The list contains several tuples like so: list = [ ('one', 'two', 'three'), ('four', 'five', 'six')] How do I print the list so each tuple item is separated by a tab and each tuple is separated by a newline? Thanks ...