tuples

Why can't I join this tuple in Python?

喜欢而已 提交于 2019-12-03 04:04:53
问题 e = ('ham', 5, 1, 'bird') logfile.write(','.join(e)) I have to join it so that I can write it into a text file. 回答1: join only takes lists of strings, so convert them first >>> e = ('ham', 5, 1, 'bird') >>> ','.join(map(str,e)) 'ham,5,1,bird' Or maybe more pythonic >>> ','.join(str(i) for i in e) 'ham,5,1,bird' 回答2: join() only works with strings, not with integers. Use ','.join(str(i) for i in e) . 回答3: You might be better off simply converting the tuple to a list first: e = ('ham', 5, 1,

Returning two values, Tuple vs 'out' vs 'struct'

北城余情 提交于 2019-12-03 04:00:45
问题 Consider a function which returns two values. We can write: // Using out: string MyFunction(string input, out int count) // Using Tuple class: Tuple<string, int> MyFunction(string input) // Using struct: MyStruct MyFunction(string input) Which one is best practice and why? 回答1: They each have their pros and cons. Out parameters are fast and cheap but require that you pass in a variable, and rely upon mutation. It is almost impossible to correctly use an out parameter with LINQ. Tuples make

Differences between vector, set, and tuple

◇◆丶佛笑我妖孽 提交于 2019-12-03 03:55:09
问题 What are the differences between vectors, sets, and tuples in programming? 回答1: Vector: Ordered collection of objects of the same type. Set: Unordered collection of objects, possibly of the same type or possibly different depending on the collection type and language. Any given object can only appear once. Tuple: Ordered collection of objects of different types. 回答2: A vector is an ordered sequence of items that does allow duplicates. A set is a collection of items that is unordered and does

Swift: Multiple intervals in single switch-case using tuple

牧云@^-^@ 提交于 2019-12-03 03:51:23
问题 Have a code like: switch (indexPath.section, indexPath.row) { case (0, 1...5): println("in range") default: println("not at all") } The question is can I use multiple intervals in second tuple value? for non-tuple switch it can be done pretty easily like switch indexPath.section { case 0: switch indexPath.row { case 1...5, 8...10, 30...33: println("in range") default: println("not at all") } default: println("wrong section \(indexPath.section)") } Which separator should I use to separate my

Matlab Table / Dataset type optimization

别等时光非礼了梦想. 提交于 2019-12-03 02:41:32
I am searching some optimized datatypes for "observations-variables" table in Matlab, that can be fast and easily accessed by columns (through variables) and by rows (through observations). Here is сomparison of existing Matlab datatypes: Matrix is very fast, hovewer, it has no built-in indexing labels/enumerations for its dimensions, and you can't always remember variable name by column index. Table has very bad performance, especially when reading individual rows/columns in a for loop (I suppose it runs some slow convertion methods, and is designed to be more Excel-like). Scalar structure

How to provide additional initialization for a subclass of namedtuple?

我是研究僧i 提交于 2019-12-03 02:34:46
问题 Suppose I have a namedtuple like this: EdgeBase = namedtuple("EdgeBase", "left, right") I want to implement a custom hash-function for this, so I create the following subclass: class Edge(EdgeBase): def __hash__(self): return hash(self.left) * hash(self.right) Since the object is immutable, I want the hash-value to be calculated only once, so I do this: class Edge(EdgeBase): def __init__(self, left, right): self._hash = hash(self.left) * hash(self.right) def __hash__(self): return self._hash

How do I add a tuple to a Swift Array?

和自甴很熟 提交于 2019-12-03 02:29:02
问题 I'm trying to add a tuple (e.g., 2-item tuple) to an array. var myStringArray: (String,Int)[]? = nil myStringArray += ("One", 1) What I'm getting is: Could not find an overload for '+=' that accepts the supplied arguments Hint: I tried to do an overload of the '+=' per reference book: @assignment func += (inout left: (String,Int)[], right: (String,Int)[]) { left = (left:String+right:String, left:Int+right+Int) } ...but haven't got it right. Any ideas? ...solution? 回答1: Since this is still the

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

谁说我不能喝 提交于 2019-12-03 01:58:24
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? The fact that you get a tuple on the RHS has nothing to do with the splat. The splat just unpacks your map iterator. What you unpack it into is decided by the fact that you've used tuple

All combinations of elements of two lists in Haskell

左心房为你撑大大i 提交于 2019-12-03 01:45:45
Given two lists, [a, b] and [c, d] , I'd like to get the following result: [(a,c), (a,d), (b,c), (b,d)] How can I do this in Haskell? Is there a built-in function for this, or should I implement one myself? [ (x,y) | x<-[a,b], y<-[c,d] ] This doesn't really require any further explanation, does it? jub0bs Applicative style all the way! λ> :m + Control.Applicative λ> (,) <$> ['a','b'] <*> ['c','d'] [('a','c'),('a','d'),('b','c'),('b','d')] (I've eschewed any String syntactic sugar above, in order to stay close to your example.) For information, (,) is special syntax for a function that takes

C++11 Tagged Tuple

吃可爱长大的小学妹 提交于 2019-12-03 01:41:25
问题 C++11 tuples are nice, but they have two hude disadvantages to me, accessing members by index is unreadable difficilt to maintain (if I add an element in the middle of the tuple, I'm screwed) In essence what I want to achieve is this tagged_tuple <name, std::string, age, int, email, std::string> get_record (); {/*...*/} // And then soomewhere else std::cout << "Age: " << get_record().get <age> () << std::endl; Something similar (type tagging) is implemented in boost::property_map, but I ca'nt