tuples

Is it possible to unpack a tuple in Python without creating unwanted variables?

橙三吉。 提交于 2019-12-03 01:04:32
Is there a way to write the following function so that my IDE doesn't complain that column is an unused variable? def get_selected_index(self): (path, column) = self._tree_view.get_cursor() return path[0] In this case I don't care about the second item in the tuple and just want to discard the reference to it when it is unpacked. In Python the _ is often used as an ignored placeholder. (path, _) = self._treeView.get_cursor() You could also avoid unpacking as a tuple is indexable. def get_selected_index(self): return self._treeView.get_cursor()[0][0] If you don't care about the second item, why

Why are tuples constructed from differently initialized sets equal?

随声附和 提交于 2019-12-03 00:59:06
I expected the following two tuples >>> x = tuple(set([1, "a", "b", "c", "z", "f"])) >>> y = tuple(set(["a", "b", "c", "z", "f", 1])) to compare unequal, but they don't: >>> x == y >>> True Why is that? Zero Piraeus At first glance, it appears that x should always equal y , because two sets constructed from the same elements are always equal: >>> x = set([1, "a", "b", "c", "z", "f"]) >>> y = set(["a", "b", "c", "z", "f", 1]) >>> x {1, 'z', 'a', 'b', 'c', 'f'} >>> y {1, 'z', 'a', 'b', 'c', 'f'} >>> x == y True However , it is not always the case that tuples (or other ordered collections)

What does the term “Tuple” Mean in Relational Databases?

拥有回忆 提交于 2019-12-03 00:20:56
问题 Please explain what is meant by tuples in sql?Thanks.. 回答1: Most of the answers here are on the right track. However, a row is not a tuple . Tuples * are unordered sets of known values with names. Thus, the following tuples are the same thing (I'm using an imaginary tuple syntax since a relational tuple is largely a theoretical construct): (x=1, y=2, z=3) (z=3, y=2, x=1) (y=2, z=3, x=1) ...assuming of course that x, y, and z are all integers. Also note that there is no such thing as a

How to get the count of Custom tuples against two lists

女生的网名这么多〃 提交于 2019-12-03 00:02:24
问题 Please help me to get the counter for the list SS2 in list SS1 in PYTHON using from collections import Counter or any other fastest way 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)] SS2=[(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 4), (1, 3, 5), (1, 3, 6), (1, 4, 5), (1, 4, 6), (1, 5, 6), (2, 3, 4), (2, 3, 5), (2, 3, 6), (2, 4, 5), (2, 4, 6), (2, 5, 6), (3, 4, 5), (3, 4, 6), (3, 5, 6), (4, 5, 6)] Here is what i have tried

Python - print tuple elements with no brackets

丶灬走出姿态 提交于 2019-12-02 22:52:23
I'm looking for a way to print elements from a tuple with no brackets Heres my tuple: mytuple = [(1.0,),(25.34,),(2.4,),(7.4,)] I converted this to a list to make it easier to work with mylist == list(mytuple) then i did the following for item in mylist: print item.strip() but i get the following error 'tuple' object has no attribute 'strip' which is strange because I thought i converted to a list? what I expect to see as the final result is something like 1.0, 25.34, 2.4, 7.4 or 1.0, ,23.43, , 2.4, ,7.4 Thanks mytuple is already a list (a list of tuples), so calling list() on it does nothing.

Get all 1-k tuples in a n-tuple

依然范特西╮ 提交于 2019-12-02 22:39:39
问题 With n=5 and k=3 the following loop will do it List<String> l=new ArrayList<String>(); l.add("A");l.add("B");l.add("C");l.add("D");l.add("E"); int broadcastSize = (int) Math.pow(2, l.size()); for (int i = 1; i < broadcastSize; i++) { StringBuffer buffer = new StringBuffer(50); int mask = i; int j = 0; int size=0; System.out.println(); while (mask > 0) { if ((mask & 1) == 1) { System.out.println(".. "+mask); buffer.append(l.get(j)); if (++size>3){ buffer = new StringBuffer(50); break; } }

How to add values to existing dictionary key Python

谁说我不能喝 提交于 2019-12-02 22:26:47
I am trying to create a function that takes in four parameters: A keyname, start time, end time, and then a dictionary. I have to create a tuple out of the start time and end time and then append that to a list of tuples, as we will be running this function multiple times. Then I am trying to put certain parts of the list of tuples to certain keynames. I think it's better if I would show you would the output looks like: courses = insertIntoDataStruct(“CS 2316”, “1505”, “1555”, courses) courses = insertIntoDataStruct(“CS 2316”, “1405”, “1455”, courses) courses = insertIntoDataStruct(“CS 2316”,

Save list of ordered tuples as CSV [duplicate]

蓝咒 提交于 2019-12-02 22:17:39
This question already has answers here : Python, transposing a list and writing to a CSV file (3 answers) I have a list of tuples ordered by value. They are in the form (name,count) where count is number of occurrences for each unique name. I would like to take this list and transform it into CSV where each name is column header and each value is column value of a single row. Any suggestions how to do it? Thanks. You can do this: import csv data=[('smith, bob',2),('carol',3),('ted',4),('alice',5)] with open('ur file.csv','wb') as out: csv_out=csv.writer(out) csv_out.writerow(['name','num'])

Find and Deletes Duplicates in List of Tuples in C#

与世无争的帅哥 提交于 2019-12-02 22:17:39
I need to find and remove the duplicates from a List of tuples. Basically, my structure is made like that: List<Tuple<string, string>> myList = new List<Tuple<string, string>>(); **** private void FillStructure() { myList.Add(Tuple.Create<string, string>("A", "B")); myList.Add(Tuple.Create<string, string>("A", "C")); myList.Add(Tuple.Create<string, string>("C", "B")); myList.Add(Tuple.Create<string, string>("C", "B")); // Duplicate myList.Add(Tuple.Create<string, string>("A", "D")); FindAndRemoveDuplicates(myList); } private void FindAndRemoveDuplicates(List<Tuple<string, string>> myList) { //

python - can lambda have more than one return

此生再无相见时 提交于 2019-12-02 22:01:37
I know lambda doesn't have a return expression. Normally def one_return(a): #logic is here c = a + 1 return c can be written: lambda a : a + 1 How about write this one in a lambda function: def two_returns(a, b): # logic is here c = a + 1 d = b * 1 return c, d Yes, it's possible. Because an expression such as this at the end of a function: return a, b Is equivalent to this: return (a, b) And there, you're really returning a single value: a tuple which happens to have two elements. So it's ok to have a lambda return a tuple, because it's a single value: lambda a, b: (a, b) # here the return is