tuples

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

馋奶兔 提交于 2019-12-03 10:34:20
问题 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. 回答1: 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

Why are tuples constructed from differently initialized sets equal?

拥有回忆 提交于 2019-12-03 10:29:52
问题 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? 回答1: 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

Why does splatting create a tuple on the rhs but a list on the lhs?

流过昼夜 提交于 2019-12-03 10:22:27
问题 Consider, for example, squares = *map((2).__rpow__, range(5)), squares # (0, 1, 4, 9, 16) *squares, = map((2).__rpow__, range(5)) squares # [0, 1, 4, 9, 16] So, all else being equal we get a list when splatting on the lhs and a tuple when splatting on the rhs. Why? Is this by design, and if yes, what's the rationale? Or, if not, are there any technical reasons? Or is this just how it is, no particular reason? 回答1: The fact that you get a tuple on the RHS has nothing to do with the splat. The

python all possible pairs of 2 list elements, and getting the index of that pair

橙三吉。 提交于 2019-12-03 09:44:09
let's say I have two lists: a = list(1,2,3) b = list(4,5,6) So I can have 9 pairs of these list members: (1,4) (1,5) (1,6) (2,4) (2,5) (2,6) (3,4) (3,5) (3,6) Now, given two list members like above, can I find out the pair's index? Like (1,4) from above would be the 1st pair. And to complete the answer and stay in the example: import itertools a = [1, 2, 3] b = [4, 5, 6] c = list(itertools.product(a, b)) idx = c.index((1,4)) But this will be the zero-based list index, so 0 instead of 1. One way to do this: Find the first element of the pair your are looking for in the first list: p = (1, 4) i

GCC/Clang x86_64 C++ ABI mismatch when returning a tuple?

时光毁灭记忆、已成空白 提交于 2019-12-03 09:22:34
问题 When trying to optimize return values on x86_64, I noticed a strange thing. Namely, given the code: #include <cstdint> #include <tuple> #include <utility> using namespace std; constexpr uint64_t a = 1u; constexpr uint64_t b = 2u; pair<uint64_t, uint64_t> f() { return {a, b}; } tuple<uint64_t, uint64_t> g() { return tuple<uint64_t, uint64_t>{a, b}; } Clang 3.8 outputs this assembly code for f : movl $1, %eax movl $2, %edx retq and this for g : movl $2, %eax movl $1, %edx retq which look

Why tuple is not mutable in Python? [duplicate]

安稳与你 提交于 2019-12-03 08:30:40
This question already has answers here : Why are python strings and tuples are made immutable? (6 answers) Possible Duplicate: Why are python strings and tuples are made immutable? What lower-level design makes tuple not mutable in Python? Why this feature is useful? A few reasons: Mutable objects like lists cannot be used as dictionary keys or set members in Python, since they are not hashable . If lists were given __hash__ methods based on their contents, the values returned could change as the contents change, which violates the contract for hash values. If Python only had mutable sequences

Find and Deletes Duplicates in List of Tuples in C#

女生的网名这么多〃 提交于 2019-12-03 08:30:02
问题 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"));

How to add values to existing dictionary key Python

不羁岁月 提交于 2019-12-03 08:27:29
问题 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)

python - can lambda have more than one return

丶灬走出姿态 提交于 2019-12-03 08:19:56
问题 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 回答1: 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

Scala's tuple unwrapping nuance

雨燕双飞 提交于 2019-12-03 08:10:55
问题 I've noticed the following behavior in scala when trying to unwrap tuples into vals: scala> val (A, B, C) = (1, 2, 3) <console>:5: error: not found: value A val (A, B, C) = (1, 2, 3) ^ <console>:5: error: not found: value B val (A, B, C) = (1, 2, 3) ^ <console>:5: error: not found: value C val (A, B, C) = (1, 2, 3) ^ scala> val (u, v, w) = (1, 2, 3) u: Int = 1 v: Int = 2 w: Int = 3 Is that because scala's pattern matching mechanism automatically presumes that all identifiers starting with