tuples

Internals for python tuples

◇◆丶佛笑我妖孽 提交于 2019-12-04 00:16:32
问题 >>> a=1 >>> b=1 >>> id(a) 140472563599848 >>> id(b) 140472563599848 >>> x=() >>> y=() >>> id(x) 4298207312 >>> id(y) 4298207312 >>> x1=(1) >>> x2=(1) >>> id(x1) 140472563599848 >>> id(x2) 140472563599848 until this point I was thinking there will be only one copy of immutable object and that will be shared(pointed) by all the variables. But when I tried, the below steps I understood that I was wrong. >>> x1=(1,5) >>> y1=(1,5) >>> id(x1) 4299267248 >>> id(y1) 4299267320 can anyone please

Pythonic shortcut for doubly nested for loops?

♀尐吖头ヾ 提交于 2019-12-03 23:31:15
Consider if I had a function that took a tuple argument (x,y), where x was in the range(X), and y in the range(Y), the normal way of doing it would be: for x in range(X): for y in range(Y): function(x,y) is there a way to do for xy in something_like_range(X,Y): function(xy) such that xy was a tuple (x,y)? You can use product from itertools >>> from itertools import product >>> >>> for x,y in product(range(3), range(4)): ... print (x,y) ... (0, 0) (0, 1) (0, 2) (0, 3) (1, 0) (1, 1) (1, 2) (1, 3) ... and so on Your code would look like: for x,y in product(range(X), range(Y)): function(x,y) You

What is the best way to check if a tuple has any empty/None values in Python?

喜你入骨 提交于 2019-12-03 22:27:22
What is the best/most efficient way to check if all tuple values? Do I need to iterate over all tuple items and check or is there some even better way? For example: t1 = (1, 2, 'abc') t2 = ('', 2, 3) t3 = (0.0, 3, 5) t4 = (4, 3, None) Checking these tuples, every tuple except t1 , should return True, meaning there is so called empty value. P.S. there is this question: Test if tuple contains only None values with Python , but is it only about None values It's very easy: not all(t1) returns False only if all values in t1 are non-empty/nonzero and not None . all short-circuits, so it only has to

How to unpack multiple tuples in function call

99封情书 提交于 2019-12-03 22:25:17
If I have a function def f(a, b, c, d) and two tuples, each with two elements, is there any way to unpack these tuples so that I can send their values to the function? f(*tup1, *tup2) As of the release of Python 3.5.0 , PEP 448 "Additional Unpacking Generalizations" makes the natural syntax for this valid Python: >>> f(*tup1, *tup2) 1 2 2 3 In older versions of Python, you can need to concatenate the tuples together to provide a single expanded argument: >>> tup1 = 1, 2 >>> tup2 = 2, 3 >>> def f(a, b, c, d): print(a, b, c, d) >>> f(*tup1+tup2) 1 2 2 3 Another approach using chain >>> from

getting an element from a tuple [duplicate]

北城余情 提交于 2019-12-03 22:16:55
Possible Duplicate: Why doesn't ADL find function templates? Calling get does not seem to invoke argument dependent lookup: auto t = std::make_tuple(false, false, true); bool a = get<0>(t); // error bool b = std::get<0>(t); // okay g++ 4.6.0 says: error: 'get' was not declared in this scope Visual Studio 2010 says: error C2065: 'get': undeclared identifier Why? Nawaz It's because you attempt to explicitly instantiate get function template, by providing 0 as template argument. In case of templates, ADL works if a function template with that name is visible at the point of the call. This visible

Surprising Tuple (in)equality

霸气de小男生 提交于 2019-12-03 22:11:12
Until today, my understanding of .NET Tuple classes had been that they delegate their implementation of Equals() to their contents, allowing me to equate and compare them "by value". Then this test came along and made a fool out of me: [TestMethod] public void EquateTwoTuplesWithSameContent() { var t1 = Tuple.Create("S"); var t2 = Tuple.Create((object)t1.Item1); Assert.IsTrue(t1.Equals(t2)); // Boom! } Reading through MSDN documentation and various blogs has left me with more questions. From what I gather, it would seem that Tuple<object> and Tuple<TWhatever> are always considered not equal,

Pythonic way to split comma separated numbers into pairs

孤街浪徒 提交于 2019-12-03 19:19:32
问题 I'd like to split a comma separated value into pairs: >>> s = '0,1,2,3,4,5,6,7,8,9' >>> pairs = # something pythonic >>> pairs [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)] What would # something pythonic look like? How would you detect and handle a string with an odd set of numbers? 回答1: Something like: zip(t[::2], t[1::2]) Full example: >>> s = ','.join(str(i) for i in range(10)) >>> s '0,1,2,3,4,5,6,7,8,9' >>> t = [int(i) for i in s.split(',')] >>> t [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> p = zip(t

How to convert tuple to a multi nested dictionary in python?

喜你入骨 提交于 2019-12-03 17:45:48
问题 I have a tuple in the following format: (639283, 298290710, 1385) (639283, 298290712, 1389) (639283, 298290715, 1395) (745310, 470212995, 2061) (745310, 470213821, 3713) (745310, 470215360, 6791) (745310, 470215361, 6793) (745310, 470215363, 6797) (911045, 374330803, 4905) (911045, 374330804, 4907) (911045, 374330807, 4913) (911045, 374330808, 4915) (911045, 374330809, 4917) I want to convert into a nested dictionary like this: {639283:{298290710:1385, 298290712:1389, 298290715:1395},745310:

How to create a new tuple type from an old one and a type in boost?

拜拜、爱过 提交于 2019-12-03 17:39:41
问题 I have a tuple type. I want to add a element type in it to get a new tuple type. I can do it like decltype tuple_cat(MyTuple, std::tuple<MyType>()) However, I don't find tuple_cat in boost::tuple , how to do it in boost? 回答1: I assume you want all this in compile time. Here is the general explanation: concatening tuples is similar to concatening lists or arrays, is that the algorithm is the same. Here, given tuples a and b , I choosed to move the last element of a to the beginning of b , and

Elixir: pattern matching works differently for tuples and maps

只愿长相守 提交于 2019-12-03 17:38:50
问题 In Elixir, if I try to pattern match the following two tuples: {a} = {1, 2} I get a match error. But if I do the same for two maps: %{x: a} = %{x: 1, y: 2} It works fine, and a binds to 1. I can see why matching the two tuples gave an error, but why did matching the maps not give an error? 回答1: In the first example you are attempting to match a single element tuple against a two-element tuple. In the second example you are matching on the :x key in both the left and right maps. EDIT: I should