I have a method in my Python code that returns a tuple - a row from a SQL query. Let's say it has three fields: (jobId, label, username)
For ease of passing it around between functions, I've been passing the entire tuple as a variable called 'job'. Eventually, however, I want to get at the bits, so I've been using code like this:
(jobId...
I am learning Python for a class now, and we just covered tuples as one of the data types. I read the Wikipedia page on it, but, I could not figure out where such a data type would be useful in practice. Can I have some examples, perhaps in Python, where an immutable set of numbers would be needed? How is this different from a list?
...
For example, if passed the following:
a = []
How do I check to see if a is empty?
...
That is, I'd like to have a tuple of values.
The use case on my mind:
Dictionary<Pair<string, int>, object>
or
Dictionary<Triple<string, int, int>, object>
Are there built-in types like Pair or Triple? Or what's the best way of implementing it?
Update There are some general-purpose tuples implementations described in the answers,...
I have a list of tuples eg. [{1,40},{2,45},{3,54}....{7,23}] where 1...7 are days of the week (calculated by finding calendar:day_of_the_week()). So now I want to change the list to [{Mon,40},{Tue,45},{Wed,54}...{Sun,23}]. Is there an easier way to do it than lists:keyreplace?
...
I have the following tuple, which contains tuples:
MY_TUPLE = (
('A','Apple'),
('C','Carrot'),
('B','Banana'),
)
I'd like to sort this tuple based upon the second value contained in inner-tuples (i.e., sort Apple, Carrot, Banana rather than A, B, C).
Any thoughts?
...
In my views.py, I'm building a list of two-tuples, where the second item in the tuple is another list, like this:
[ Product_Type_1, [ product_1, product_2 ],
Product_Type_2, [ product_3, product_4 ]]
In plain old Python, I could iteration the list like this:
for product_type, products in list:
print product_type
for product...
I am new to Python, and I'm working on writing some database code using the cx_Oracle module. In the cx_Oracle documentation they have a code example like this:
import sys
import cx_Oracle
connection = cx_Oracle.Connection("user/pw@tns")
cursor = connection.cursor()
try:
cursor.execute("select 1 / 0 from dual")
except cx_Oracle.D...
I am trying to talk to a device using python. I have been handed a tuple of bytes which contains the storage information. How can I convert the data into the correct values:
response = (0, 0, 117, 143, 6)
The first 4 values are a 32-bit int telling me how many bytes have been used and the last value is the percentage used.
I can acc...
I'm trying to get my head around tuples (thanks @litb), and the common suggestion for their use is for functions returning > 1 value.
This is something that I'd normally use a struct for , and I can't understand the advantages to tuples in this case - it seems an error-prone approach for the terminally lazy.
Borrowing an example, I'd ...
Observation: This is a repost of a question I asked at the Hibernate forum but got no response.
I have a simple graph structure like this (in MySQL):
create table Node(
id int not null auto_increment,
name varchar(255),
primary key(id)
) engine=InnoDB;
create table Edge(
source int not null,
target int not null,
cost...
Is there anyway to get tuples operation in python to work like this:
>>>a = (1,2,3)
>>>b = (3,2,1)
>>>a + b
(4,4,4)
instead of:
>>>a = (1,2,3)
>>>b = (3,2,1)
>>>a + b
(1,2,3,3,2,1)
I know it works like that because the __add__ and __mul__ methods are defined to work like that.
So the only way would be to redefine them?
...
Does anyone use tuples in Ruby? If so, how may one implement a tuple? Ruby hashes are nice and work almost as well, but I'd really like to see something like the Tuple class in Python, where you can use . notation to find the value for which you are looking. I'm wanting this so that I can create an implementation of D, similar to Dee for...
Right now I have vector3 values represented as lists. is there a way to subtract 2 of these like vector3 values, like
[2,2,2] - [1,1,1] = [1,1,1]
Should I use tuples?
If none of them defines these operands on these types, can I define it instead?
If not, should I create a new vector3 class?
...
Currently, I'm trying to get a method in Python to return a list of zero, one, or two strings to plug into a string formatter and then pass them to the string method. My code looks something like this:
class PairEvaluator(HandEvaluator):
def returnArbitrary(self):
return ('ace', 'king')
pe = PairEvaluator()
cards = pe.returnArbit...
In F# code I have a tuple:
let myWife=("Tijana",32)
I want to access each member of the touple separately. For instance this what I want to achieve by I can't
Console.WriteLine("My wife is {0} and her age is {1}",myWife[0],myWife[1])
This code doesn't obviously work, by I think you can gather what I want to achieve.
...
I have a dictionary of values read from 2 fields in a database: a string field and a numeric field. The string field is unique so that is the key of the dictionary.
I can sort on the keys, but how can I sort based on the values?
Note: I have read this post 72899 and probably could change my code to have a list of dictionaries but since...
What's the difference?
What are the advantages / disadvantages of tuples / lists?
...
Hi,
I have a list of tuples (always pairs) like this:
[(0, 1), (2, 3), (5, 7), (2, 1)]
I'd like to find the sum of the first items in each pair, i.e.:
0 + 2 + 5 + 2
How can I do this in python? At the moment I'm iterating through the list:
sum = 0
for pair in list_of_pairs:
sum += pair[0]
but I have a feeling there must be ...
The codebase where I work has an object called Pair where A and B are the types of the first and second values in the Pair. I find this object to be offensive, because it gets used instead of an object with clearly named members. So I find this:
List<Pair<Integer, Integer>> productIds = blah();
// snip many lines and method calls
voi...