tuples

Subtracting 2 lists in Python

扶醉桌前 提交于 2019-12-17 04:47:00
问题 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? 回答1: If this is something you end up doing frequently, and with different operations, you should probably create a class to handle cases like this, or better use some library like Numpy. Otherwise, look

Unpacking a list / tuple of pairs into two lists / tuples [duplicate]

依然范特西╮ 提交于 2019-12-17 03:46:52
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: A Transpose/Unzip Function in Python I have a list that looks like this: list = (('1','a'),('2','b'),('3','c'),('4','d')) I want to separate the list in 2 lists. list1 = ('1','2','3','4') list2 = ('a','b','c','d') I can do it for example with: list1 = [] list2 = [] for i in list: list1.append(i[0]) list2.append(i[1]) But I want to know if there is a more elegant solution. 回答1: >>> source_list = ('1','a'),('2','b

When are parentheses required around a tuple?

谁说胖子不能爱 提交于 2019-12-17 02:29:47
问题 Is there a reference somewhere defining precisely when enclosing tuples with parentheses is or is not required? Here is an example that surprised me recently: >>> d = {} >>> d[0,] = 'potato' >>> if 0, in d: File "<stdin>", line 1 if 0, in d: ^ SyntaxError: invalid syntax 回答1: The combining of expressions to create a tuple using the comma token is termed an expression_list. The rules of operator precedence do not cover expression lists; this is because expression lists are not themselves

How to extract the n-th elements from a list of tuples?

独自空忆成欢 提交于 2019-12-17 02:12:09
问题 I'm trying to obtain the n-th elements from a list of tuples. I have something like: elements = [(1,1,1),(2,3,7),(3,5,10)] I wish to extract only the second elements of each tuple into a list: seconds = [1, 3, 5] I know that it could be done with a for loop but I wanted to know if there's another way since I have thousands of tuples. 回答1: n = 1 # N. . . [x[n] for x in elements] 回答2: I know that it could be done with a FOR but I wanted to know if there's another way There is another way. You

How do I expand a tuple into variadic template function's arguments?

狂风中的少年 提交于 2019-12-16 19:52:43
问题 Consider the case of a templated function with variadic template arguments: template<typename Tret, typename... T> Tret func(const T&... t); Now, I have a tuple t of values. How do I call func() using the tuple values as arguments? I've read about the bind() function object, with call() function, and also the apply() function in different some now-obsolete documents. The GNU GCC 4.4 implementation seems to have a call() function in the bind() class, but there is very little documentation on

How do I get data in tuples and tuples in lists?

。_饼干妹妹 提交于 2019-12-16 18:05:12
问题 I am trying to figure out the route that a car takes in a fictional manhattan. I have defined the starting position, being (1,2) (in a 2 dimensional grid). manhattan=[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]] car_in_manhattan=8 car_unmerged = ([(index, row.index(car_in_manhattan)) for index, row in enumerate(manhattan) if car_in_manhattan in row]) car=list(itertools.chain(*car_unmerged)) i am doing this because I do not want a list in a list route1=car The

scala zip list to tuple

孤人 提交于 2019-12-14 04:17:13
问题 Working with JodaTime, trying to convert a List[LocalDate] to Tuple2[JodaTime, JodaTime] so I can do multi-assigment like so: val(expire, now) = List(row.expireDate, new JodaDate) zip (_.toDateTimeAtStartOfDay.getMillis) which of course does not compile. Is there a similarly concise way to do the above? I know I can just do it manually: val(expire, now) = (row.expireDate.toDateTimeAtStartOfDay.getMillis, new JodaDate().toDateTimeAtStartOfDay.getMillis) but that's a bit ugly 回答1: val Seq

How to modify just one field of a record without rewriting it completely? [duplicate]

两盒软妹~` 提交于 2019-12-14 03:56:08
问题 This question already has answers here : Shorthand way for assigning a single field in a record, while copying the rest of the fields? (3 answers) Is there a Haskell idiom for updating a nested data structure? (3 answers) Closed 8 months ago . It's the second time I'm tackling this problem... And for the second time this is while working with the State monad, apparently a state likes to consist of many fields, not just one I have a tuple: type CurrentState = (Int, Int, String, [Int],

How to remove duplicate from list of tuple when order is important

主宰稳场 提交于 2019-12-14 03:53:15
问题 I have seen some similar answers, but I can't find something specific for this case. I have a list of tuples: [(5, 0), (3, 1), (3, 2), (5, 3), (6, 4)] What I want is to remove tuples from this list only when first element of tuple has occurred previously in the list and the tuple which remains should have the smallest second element. So the output should look like this: [(5, 0), (3, 1), (6, 4)] 回答1: Here's a linear time approach that requires two iterations over your original list. t = [(5, 0

Count occurences of all items of a list in a tuple

杀马特。学长 韩版系。学妹 提交于 2019-12-14 03:29:46
问题 I have a tuple (1,5,2,3,4,5,6,7,3,2,2,4,3) and a list [1,2,3] and now want to count how often all items of the list occur in the tuple (so it should return 7 ). I could loop the list, count each item in the tuple and then sum up the results, but I bet there is a better possibility in python. Thats not a duplicate of How to count the occurrences of a list item? because I explicitly said that I am not just asking for list.count(item_of_list) (that would need to be done in a loop) but for a