tuples

How do I sum the first value in each tuple in a list of tuples in Python?

那年仲夏 提交于 2019-12-17 15:54:19
问题 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] I have a feeling there must be a more Pythonic way. 回答1: A version compatible with Python 2.3 is sum([pair[0] for pair in list_of_pairs]) or in recent versions of Python, see this answer or this one. 回答2: sum(i

Iterate over pairs in a list (circular fashion) in Python

拜拜、爱过 提交于 2019-12-17 15:25:44
问题 The problem is easy, I want to iterate over each element of the list and the next one in pairs (wrapping the last one with the first). I've thought about two unpythonic ways of doing it: def pairs(lst): n = len(lst) for i in range(n): yield lst[i],lst[(i+1)%n] and: def pairs(lst): return zip(lst,lst[1:]+[lst[:1]]) expected output: >>> for i in pairs(range(10)): print i (0, 1) (1, 2) (2, 3) (3, 4) (4, 5) (5, 6) (6, 7) (7, 8) (8, 9) (9, 0) >>> any suggestions about a more pythonic way of doing

C++11 way to index tuple at runtime without using switch

限于喜欢 提交于 2019-12-17 15:25:09
问题 I have a piece of c++11 code similar like below: switch(var) { case 1: dosomething(std::get<1>(tuple)); case 2: dosomething(std::get<2>(tuple)); ... } Is there any way to remove this large switch ? Note that get<var> does not work because var is not constant, but I know var is in small range i.e. (0-20). Note that the point here is to avoid using an array that causes an array lookup... EDIT: well on the issue of performance, there is a discussion Performance of array of functions over if and

How to convert an Array to a Tuple?

落花浮王杯 提交于 2019-12-17 11:27:12
问题 I have an Array[Any] from Java JPA containing (two in this case, but consider any a small number of) differently-typed things. I would like to represent these as tuples instead. I have some quick and dirty conversion code, and wondered how it could be improved and perhaps made more generic. val pair = query.getSingleOrNone // returns Option[Any] (actually a Java array) pair collect { case array: Array[Any] => (array(0).asInstanceOf[MyClass1], array(1).asInstanceOf[MyClass2]) } 回答1: How about

python - get list of tuples first index?

佐手、 提交于 2019-12-17 10:52:48
问题 What's the most compact way to return the following: Given a list of tuples, return a list consisting of the tuples first (or second, doesn't matter) elements. For: [(1,'one'),(2,'two'),(3,'three')] returned list would be [1,2,3] 回答1: use zip if you need both >>> r=(1,'one'),(2,'two'),(3,'three') >>> zip(*r) [(1, 2, 3), ('one', 'two', 'three')] 回答2: >>> tl = [(1,'one'),(2,'two'),(3,'three')] >>> [item[0] for item in tl] [1, 2, 3] 回答3: >>> mylist = [(1,'one'),(2,'two'),(3,'three')] >>> [j for

What does *tuple and **dict means in Python? [duplicate]

匆匆过客 提交于 2019-12-17 10:45:08
问题 This question already has answers here : What do *args and **kwargs mean? [duplicate] (5 answers) Closed 5 years ago . As mentioned in PythonCookbook, * can be added before a tuple, and what does * mean here? Chapter 1.18. Mapping Names to Sequence Elements: from collections import namedtuple Stock = namedtuple('Stock', ['name', 'shares', 'price']) s = Stock(*rec) # here rec is an ordinary tuple, for example: rec = ('ACME', 100, 123.45) In the same section, **dict presents: from collections

Can someone please explain the “indices trick”?

我只是一个虾纸丫 提交于 2019-12-17 09:43:48
问题 I noticed the "indices trick" being mentioned in the context of pretty-printing tuples. It sounded interesting, so I followed the link. Well, that did not go well. I understood the question, but could really not follow what was going on. Why do we even need indices of anything? How do the different functions defined there help us? What is 'Bare'? etc. Can someone give a play-by-play of that thing for the less-than-experts on parameter packs and variadic tuples? 回答1: The problem is: we have a

Tuple or list when using 'in' in an 'if' clause?

纵然是瞬间 提交于 2019-12-17 07:23:57
问题 Which approach is better? Using a tuple, like: if number in (1, 2): or a list, like: if number in [1, 2]: Which one is recommended for such uses and why (both logical and performance wise)? 回答1: The CPython interpreter replaces the second form with the first . That's because loading the tuple from a constant is one operation, but the list would be 3 operations; load the two integer contents and build a new list object. Because you are using a list literal that isn't otherwise reachable, it is

Convert std::tuple to std::array C++11

[亡魂溺海] 提交于 2019-12-17 06:38:31
问题 If I have std::tuple<double, double, double> (where the type is homogeneous), is there a stock function or constructor to convert to std::array<double> ? Edit: : I was able to get it working with recursive template code (my draft answer posted below). Is this the best way to handle this? It seems like there would be a stock function for this... Or if you have improvements to my answer, I'd appreciate it. I'll leave the question unanswered (after all, I want a good way, not just a workable way

Splitting list into a list of possible tuples

元气小坏坏 提交于 2019-12-17 05:45:07
问题 I need to split a list into a list of all possible tuples, but I'm unsure of how to do so. For example: pairs ["cat","dog","mouse"] should result in: [("cat","dog"), ("cat","mouse"), ("dog","cat"), ("dog","mouse"), ("mouse","cat"), ("mouse","dog")] I was able to form the first two, but am unsure of how to get the rest. Here's what I have so far: pairs :: [a] -> [(a,a)] pairs (x:xs) = [(m,n) | m <- [x], n <- xs] 回答1: You can use a list comprehension: allpairs :: Eq a => [a] -> [(a,a)] allpairs