tuples

Swift: Multiple intervals in single switch-case using tuple

依然范特西╮ 提交于 2019-12-02 17:24:17
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 intervals inside tuple or it's just not gonna work for tuple switches and I have to use switch inside

Why can't I join this tuple in Python?

霸气de小男生 提交于 2019-12-02 17:23:21
e = ('ham', 5, 1, 'bird') logfile.write(','.join(e)) I have to join it so that I can write it into a text file. 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' djc join() only works with strings, not with integers. Use ','.join(str(i) for i in e) . You might be better off simply converting the tuple to a list first: e = ('ham', 5, 1, 'bird') liste = list(e) ','.join(liste) John Machin Use the csv module. It will save a follow-up question about how

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

百般思念 提交于 2019-12-02 17:22:25
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? 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 collection pressure and are un-self-documenting. "Item1" is not very descriptive. Custom structs can be slow

List and Tuples in Scala

谁都会走 提交于 2019-12-02 16:26:56
问题 From the book 'Programming in Scala' by Martin Odersky: Another useful container object is the tuple. Like lists, tuples are immutable, but unlike lists, tuples can contain different types of elements. But I can have: val oneTwoThreee = List(1, 2, "Third Element") //same as:List.apply(1,2,3) for (i <- 0 to 2) { println(oneTwoThreee.apply((i))) } And its output is: 1 2 Third Element So List in Scala can have different types of elements. And from same book: You may be wondering why you can’t

Differences between vector, set, and tuple

China☆狼群 提交于 2019-12-02 16:14:51
What are the differences between vectors, sets, and tuples in programming? 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. brabster A vector is an ordered sequence of items that does allow duplicates. A set is a collection of items that is unordered and does not allow duplicates. A tuple is an ordered sequence of items of a given length. A tuple is a

How to provide additional initialization for a subclass of namedtuple?

ε祈祈猫儿з 提交于 2019-12-02 16:08:22
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 This appears to be working, but I am really not sure about subclassing and initialization in Python,

C++11 Tagged Tuple

心已入冬 提交于 2019-12-02 15:11:24
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 get my head around how to implement it in a tuple with arbitary number of elements PS Please do not

How to get the key from a given name in dictionary in python

独自空忆成欢 提交于 2019-12-02 14:30:47
问题 I have a variable called anime_dict which contains a dictionary of lists of objects as shown below. {'JI2212': ('Inu Yasha', [('year', 1992), ('rating', 3)]), 'EH389J': (Naruto', [('year', 1994), ('rating', 4), ('readers', 3424322)]), 'PPP67': ('Fruits Basket', [('Year', 1999), ('rating', 5), ('readers', 434232), ('chap', 40)])} so the key of the dict is the 1st part ('JI2212' for Inu Yasha), the 2nd part is the name and the last part contains a list of parts. I want to create 2 functions

How do I add a tuple to a Swift Array?

徘徊边缘 提交于 2019-12-02 14:22:18
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? Mike MacMillan Since this is still the top answer on google for adding tuples to an array, its worth noting that things have

Convert a string with 2-tier delimiters into a dictionary - python

喜你入骨 提交于 2019-12-02 13:31:58
问题 Given a string: s = 'x\t1\ny\t2\nz\t3' I want to convert into a dictionary: sdic = {'x':'1','y':'2','z':'3'} I got it to work by doing this: sdic = dict([tuple(j.split("\t")) for j in [i for i in s.split('\n')]]) First: ['x\t1','y\t2','z\t3'] # str.split('\n') Then: [('x','1'),('y','2'),('z','3')] # tuples([str.split('\t')]) Finally: {'x':'1', 'y':'2', 'z':'3'} # dict([tuples]) But is there a simpler way to convert a string with 2-tier delimiters into a dictionary? 回答1: You're a little