tuples

Is it OK to exchange tuples between Python modules ?

I have a small Python program consisting of very few modules (about 4 or so). The main module creates a list of tuples, thereby representing a number of records. These tuples are available to the other modules through a simple function that returns them (say, get_records()). I am not sure if this is good design however. The problem be...

Replacing ViewModels with Tuples

Hey everyone, I've just started on a new project which currently contains a fair few ViewModel DTO classes. I'm wondering whether it would be useful and even good practice to replace many of these DTOs with Tuples and to use that as my method of transfering a bunch of objects to my views. What do you think? Is this a correct usage of ...

excess positional arguments, unpacking argument lists or tuples, and extended iterable unpacking

This question is going to be rather long, so I apologize preemptively. In Python we can use * in the following three cases: I. When defining a function that we want to be callable with an arbitrary number of arguments, such as in this example: def write_multiple_items(file, separator, *args): file.write(separator.join(args)) In ...

Python converting the values from dicts into a tuples

I have a list of dictionaries that looks like this: [{'id':1,'name':'Foo'},{'id':2,'name':'Bar'}] I'd like to convert the values from each dict into a list of tuples like this: [(1,'Foo'),(2,'Bar')] How can I do this? ...

How do I convert a tuple of tuples to a one-dimensional list using list comprehension?

I have a tuple of tuples - for example: tupleOfTuples = ((1, 2), (3, 4), (5,)) I want to convert this into a flat, one-dimensional list of all the elements in order: [1, 2, 3, 4, 5] I've been trying to accomplish this with list comprehension. But I can't seem to figure it out. I was able to accomplish it with a for-each loop: myLi...

Unpacking a 1-tuple in a list of length 1

Suppose I have a tuple in a list like this: >>> t = [("asdf", )] I know that the list always contains one 1-tuple. Currently I do this: >>> dummy, = t >>> value, = dummy >>> value 'asdf' Is there a shorter and more elegant way to do this? ...

Creating a namedtuple with a custom hash function

Say I have a namedtuple like this: fooTuple = namedtuple("fooTuple", "item1, item2") And I want the following function to be used for hashing: fooHash(self): return hash(self.item1) * (self.item2) I want this because I want the order of item1 and item2 to be irrelevant (I will do the same for the comparison-operator). I thought...

Using Deque.popleft as args for function

Hi, I am attempting to store a list of commands to send down a serial cable using deque in Python. My function "send_command" accepts 3 values; The command, an int. pause and a boolean wait. its definition is as follows. def send_command(self, command, pause=0, wait=False): What I would like to do is, rather than calling this functi...

sorting a list of tuples

I have a list of tuples of the form (a,b,c,d) and I want to copy only those tuples with unique values of 'a' to a new list. I'm very new to python. Current idea that isn't working: for (x) in list: a,b,c,d=(x) if list.count(a)==1: newlist.append(x) ...

Mapping python tuple and R list with rpy2 ?

Hello guys, I'm having some trouble to understand the mapping with rpy2 object and python object. I have a function(x) which return a tuple object in python, and i want to map this tuple object with R object list or vector. First, i'm trying to do this : # return a python tuple into this r object tlist robjects.r.tlist = get_max_ticks...

How to extract the n-th elements from a list of tuples in python?

Hello, I'm trying to obtain the n-th elements from a list of tuples. I have something like elements = [(1,1,1),(2,3,7),(3,5,10)] and I want to extract only the second elements of each tuple into an array: seconds = [1, 3, 5] I know that it could be done with a for but I wanted to know if there's another way since I have thousands o...

std::tuple get() member function

boost::tuple has a get() member function used like this: tuple<int, string, string> t(5, "foo", "bar"); cout << t.get<1>(); // outputs "foo" It seems the C++0x std::tuple does not have this member function, and you have to instead use the non-member function form: std::get<1>(t); which to me looks uglier. Is there any particular ...

Generating tuples modulo index

I am looking for an algorithm (or a C-like implementation, no itertools available) which generates all tuples [a_0 a_1 ... a_(n-1)] such that 0 <= a_i <= i + 1. Pointers to literature are also welcome. ...

Why tuple is faster than list?

Sorry because this is a noob question. I'm reading Dive into Python, and just read "tuple is faster than list". http://diveintopython3.org/native-datatypes.html#tuples Tuple is immutable, and list is mutable, but I don't quite understand why tuple is faster. Anyone did a performance test on this? Thanks. ...

Simple question about tuple of scala

I'm new to scala, and what I'm learning is tuple. I can define a tuple as following, and get the items: val tuple = ("Mike", 40, "New York") println("Name: " + tuple._1) println("Age: " + tuple._2) println("City: " + tuple._3) My question is: How to get the length of a tuple? Is tuple mutable? Can I modify its items? Is there any o...

What is the difference between lists and tuples in Python?

Which is more efficient? What is the typical use of each? ...

Why can't I do tupleVar.get(3) or .get<3>()?

I was going over C++0x. As i looked at tuple I saw this example. Why do I need to do get<3>(var)? Why can't I do var.get(index) or var.get<index>()? I prefer these to make code look and feel consistant. typedef tuple< int, double, long &, const char * > test_tuple ; long lengthy = 12 ; test_tuple proof( 18, 6.5, lengthy, "Ciao!" ) ; len...

How to initialize with multiple return values in c++(0x)

tuple in boost and TR1/c++0x provides a convenient (for the writer of the function) method to return two values from a function--however it seems to damage one major feature of the language for the caller: the ability to simply use the function to initialize a variable: T happy(); const auto meaningful_name(happy()); // RVO means no exc...

Expanding tuples in python

In the following code: a = 'a' tup = ('tu', 'p') b = 'b' print 'a: %s, t[0]: %s, t[1]: %s, b:%s'%(a, tup[0], tup[1], b) How can I "expand" (can't figure out a better verb) tup so that I don't have to explicitly list all its elements? NOTE That I don't want to print tup per-se, but its individual elements. In other words, the followin...

Enumerating m-tuples of Integers Subject to Implication Constraints

How do I enumerate all m-tuples of nonnegative integers (a[1],...,a[m]) subject to the following constraints? For each i in {1,...,m}, there is a number n[i] >= 0 such that a[i] <= n[i]. For each ordered pair (i,j) with i,j in {1,...,m}, there are numbers c[i][j], d[i][j] >= 0 such that: if a[i] > c[i][j], then a[j] <= d[i][j]. c[i][...