tuples

How to convert an custom class object to a tuple in Python? [closed]

。_饼干妹妹 提交于 2019-12-01 02:21:29
If we define __str__ method in a class: class Point(): def __init__(self, x, y): self.x = x self.y = y def __str__(self, key): return '{},{}'.format(self.x, self.y) So we can convert its object to str immediately: a = Point(1, 1) b = str(a) print(b) But as far as I know, there is not such __tuple__ magic method, so I do not know how to define a class which can pass to tuple() so that we can convert its object to tuple immediately. The tuple "function" (it's really a type, but that means you can call it like a function) will take any iterable, including an iterator, as its argument. So if you

c# sorting a List<> using Tuple?

冷暖自知 提交于 2019-12-01 01:56:06
问题 I need to sort a List<> of MediaItem objects by publish date...the publish date is not a property of the item. So my initial intention was to temporarily tack on a publish date property, load em up, sort, and ditch the property. Someone at my work suggested I use Tuple and sort with LINQ...I've got about this far in my snippet test, filling up the List<>...but I'm not sure how to sort by that date property using LINQ. I'll keep looking on my end but if anyone's got any help I'd appreciate it.

Advanced sorting criteria for a list of nested tuples

拥有回忆 提交于 2019-12-01 01:39:32
I have a list of nested tuples of the form: [(a, (b, c)), ...] Now I would like to pick the element which maximizes a while minimizing b and c at the same time. For example in [(7, (5, 1)), (7, (4, 1)), (6, (3, 1))] the winner should be (7, (4, 1)) Any help is appreciated. In my understanding, you want to sort decreasingly by a, and ascendingly by b, then by c. If that's right, you can do it like so: >>> l=[(7, (5, 1)), (7, (4, 1)), (6, (3, 2)), (6, (3, 1))] >>> sorted(l, key = lambda x: (-x[0], x[1])) [(7, (4, 1)), (7, (5, 1)), (6, (3, 1)), (6, (3, 2))] Picking the "winner" would be as simple

C# 7:How can I deconstruct an object into a single value using a tuple?

不羁的心 提交于 2019-12-01 01:15:30
问题 One of the nice new features of C# 7 is the possibility to define deconstructors for classes and assign the deconstructed values directly to a value tuple. However, in the case that the object is deconstructed into a single value, I can't find a way to assign it to a tuple. Although there is a type for tuples with a single element ( ValueTuple<T> ), the shorthand syntax using parentheses doesn't work here. The only way I found to access the deconstructor was to call the Deconstruct method

Converting Swift Array to NSData for NSUserDefaults.StandardUserDefaults persistent storage

你。 提交于 2019-12-01 00:23:44
I'm trying to get my head around Swift (after being relatively competent with Obj-C) by making a small app. I would like to use NSUserDefaults to persistently save a small amount of data but I am having problems. I initialise an empty array of tuples like this: var costCategoryArray: [(name:String, defaultValue:Int, thisMonthsEstimate:Int, sumOfThisMonthsActuals:Int, riskFactor:Float, monthlyAverage:Float)]=[] When the array has an entry, I want to save the array to NSUserDefaults with standard Swift code such as this: NSUserDefaults.standardUserDefaults().setObject(costCategoryArray, forKey:

Using tuple comparison in mysql is it efficient?

情到浓时终转凉″ 提交于 2019-12-01 00:20:54
问题 I have a table of books : CREATE TABLE `books` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `nameOfBook` VARCHAR(32), `releaseDate` DATETIME NULL DEFAULT NULL, PRIMARY KEY (`id`), INDEX `Index 2` (`releaseDate`, `id`) ) COLLATE='latin1_swedish_ci' ENGINE=InnoDB AUTO_INCREMENT=33029692; I compared two SQL requests to do a pagiation with sort on releaseDate. Both of theses request return the same result. (simple one) select SQL_NO_CACHE id,name, releaseDate from books where releaseDate <= '2016-11

Compare two lists of tuples

假如想象 提交于 2019-12-01 00:07:06
old = [('ver','1121'),('sign','89'),('address','A45'),('type','00')] new = [('ver','1121'),('sign','89'),('type','01')] I need to compare the new list against old one based on first element of the tuples, and show the difference between whatever elements new list has, so that the output should look like: Match : ver = 1121 Match : sign = 89 Mismatch : type = 01 (old : 00) I could get all the matching tuples with below list comprehension but could not think beyond it. my_list = [(a,b) for (a,b) in new for (c,d) in old if ((a==c) and (b==d))] print( my_list) Please suggest me a way to do it.

Python tuple assignment and checking in conditional statements [duplicate]

戏子无情 提交于 2019-11-30 23:28:27
This question already has an answer here: When are parentheses required around a tuple? 3 answers So I stumbled into a particular behaviour of tuples in python that I was wondering if there is a particular reason for it happening. While we are perfectly capable of assigning a tuple to a variable without explicitely enclosing it in parentheses: >>> foo_bar_tuple = "foo","bar" >>> we are not able to print or check in a conditional if statement the variable containing the tuple in the previous fashion (without explicitely typing the parentheses): >>> print foo_bar_tuple == "foo","bar" False bar >

Sort a list of tuples by their second elements

耗尽温柔 提交于 2019-11-30 23:16:26
问题 I want to sort a list of tuples by their second elements. Example input: [("Bob",3),("Terry",1)] Example output: [("Terry",1)("Bob",3)] 回答1: Another cool trick is to use on from Data.Function: import Data.Function (on) import Data.List (sortBy) sortBy (compare `on` snd) [...] Not much different than comparing but a nice trick from time to time. 回答2: You can use sortBy and comparing: sortBy :: (a -> a -> Ordering) -> [a] -> [a] comparing :: (Ord b) => (a -> b) -> a -> a -> Ordering In this

Convert list in tuple to numpy array?

ぃ、小莉子 提交于 2019-11-30 22:36:08
问题 I have tuple of lists. One of these lists is a list of scores. I want to convert the list of scores to a numpy array to take advantage of the pre-built stats that scipy provides. In this case the tuple is called 'data' In [12]: type data[2] -------> type(data[2]) Out[12]: <type 'list'> In [13]: type data[2][1] -------> type(data[2][1]) Out[13]: <type 'list'> In [14]: type data[2][1][1] -------> type(data[2][1][1]) Out[14]: <type 'float'> In [15]: print data[2][1] -------> print(data[2][1])