tuples

using bisect on list of tuples but compare using first value only

孤人 提交于 2019-12-01 19:04:16
I read that question about how to use bisect on a list of tuples, and I used that information to answer that question . It works, but I'd like a more generic solution. Since bisect doesn't allow to specify a key function, if I have this: import bisect test_array = [(1,2),(3,4),(5,6),(5,7000),(7,8),(9,10)] and I want to find the first item where x > 5 for those (x,y) tuples (not considering y at all, I'm currently doing this: bisect.bisect_left(test_array,(5,10000)) and I get the correct result because I know that no y is greater than 10000, so bisect points me to the index of (7,8) . Had I put

Python: What is the difference between these two import statements?

泄露秘密 提交于 2019-12-01 18:52:50
They both functionally looks same to me. Are there any differences and advantages of using one over another? >>> from datetime import datetime, timedelta >>> from datetime import (datetime, timedelta) Silas Ray If you wrap the imports in parens, you don't have to use a backslash for line continuation if you put a line break in the import statement, which is the preferred style. Functionally, they are identical, and if on one line, leaving out the parens is cleaner. Both of them are same: In [17]: import dis In [18]: def func1(): ....: from datetime import datetime, timedelta ....: In [19]: def

How do I check if an array of tuples contains a particular one in Swift?

徘徊边缘 提交于 2019-12-01 18:22:14
Consider the following Swift code. var a = [(1, 1)] if contains(a, (1, 2)) { println("Yes") } All I need is to check if a contains the tuple but the code leads to error. Cannot find an overload for 'contains' that accepts an argument list of type '([(Int, Int)], (Int, Int))' Why so and how to use contains properly? Add the following to your code: func contains(a:[(Int, Int)], v:(Int,Int)) -> Bool { let (c1, c2) = v for (v1, v2) in a { if v1 == c1 && v2 == c2 { return true } } return false } Swift is not that flexible when it comes to tuples. They do not conform to the Equatable protocol. So

Sort a list of tuples by value and then alphabetically

半腔热情 提交于 2019-12-01 17:48:26
Bit of a python newbie, but I got the following list of tuples. I need to sort it by value and if the value is the same, solve ties alphabetically. Here's a sample: #original list_of_medals = [('Sweden', 24), ('Germany', 16), ('Russia', 10), ('Ireland', 10), ('Spain', 9), ('Albania', 8), ('Lithuania', 7), ('Iceland', 6), ('Malta', 5), ('Italy', 5), ('Serbia', 4), ('Estonia', 4), ('Turkey', 4), ('Moldova', 2), ('Azerbaijan', 2)] # \____/ \_____/ \______/ #after sorting / \ / \ / \ sorted_medals = [('Sweden', 24), ('Germany', 16), ('Ireland', 10), ('Russia', 10), ('Spain', 9), ('Albania', 8), (

F# flatten nested tuples

时光总嘲笑我的痴心妄想 提交于 2019-12-01 17:21:47
is there a way to flatten tuples of arbitrary size in F# without explicitly mapping them? (fun ((((a0,a1),a2),b),c) -> (a0,a1,a2,b,c)) As a note I'm getting these kind of tuples from FParsec but the capability would be convenient if it was available generally. thanks, Sean You can't do it easily, but with a bit of reflection it is possible: let isTuple tuple = Microsoft.FSharp.Reflection.FSharpType.IsTuple(tuple.GetType()) let tupleValues (tuple : obj) = Microsoft.FSharp.Reflection.FSharpValue.GetTupleFields tuple |> Array.toList let rec flatten tupleFields = tupleFields |> List.collect(fun

Is there a way to check if an item exists in a Python tuple?

自作多情 提交于 2019-12-01 17:19:02
I have seen an index function but it says it errors out if it can't find it. Is there a simple way to just check if the item exists? I just want to get a boolean value of the result so like: if tuple.exists("item"): print "it exists" Use in operator: >>> 2 in (2, 3, 4) True in operator can be used to check for the membership of any element in a sequence. And don't name your tuple as tuple . Use a different name. 来源: https://stackoverflow.com/questions/14902997/is-there-a-way-to-check-if-an-item-exists-in-a-python-tuple

Is there a way to check if an item exists in a Python tuple?

孤者浪人 提交于 2019-12-01 17:06:50
问题 I have seen an index function but it says it errors out if it can't find it. Is there a simple way to just check if the item exists? I just want to get a boolean value of the result so like: if tuple.exists("item"): print "it exists" 回答1: Use in operator: >>> 2 in (2, 3, 4) True in operator can be used to check for the membership of any element in a sequence. And don't name your tuple as tuple . Use a different name. 来源: https://stackoverflow.com/questions/14902997/is-there-a-way-to-check-if

F# flatten nested tuples

混江龙づ霸主 提交于 2019-12-01 17:05:25
问题 is there a way to flatten tuples of arbitrary size in F# without explicitly mapping them? (fun ((((a0,a1),a2),b),c) -> (a0,a1,a2,b,c)) As a note I'm getting these kind of tuples from FParsec but the capability would be convenient if it was available generally. thanks, 回答1: You can't do it easily, but with a bit of reflection it is possible: let isTuple tuple = Microsoft.FSharp.Reflection.FSharpType.IsTuple(tuple.GetType()) let tupleValues (tuple : obj) = Microsoft.FSharp.Reflection

What's the difference between (1,) and (1) in python [duplicate]

帅比萌擦擦* 提交于 2019-12-01 16:18:30
This question already has an answer here: How to create a tuple with only one element 4 answers As stated in the title, I found that (1) and (1,) are different. But what's the difference of them? In[39]: (1) == (1,) Out[39]: False The comma makes it a tuple. (1) is just the same as 1 wrapped in delimiters. Try this to convince yourself: >>> type((1)) <type 'int'> >>> type((1,)) <type 'tuple'> The following identity checks may provide you with further insight into the differences: >>> (1) is 1 True >>> (1,) is 1 False 来源: https://stackoverflow.com/questions/37312512/whats-the-difference-between

N-ary tuples vs pairs

萝らか妹 提交于 2019-12-01 16:12:21
In Ocaml, tuples with different arities have different type and value constructors: # let a = (1, 2, 3);; val a : int * int * int = (1, 2, 3) # let b = (1, (2, 3));; val b : int * (int * int) = (1, (2, 3)) Note that second example (b) is more flexible than first (a) because "tail" of b - (2, 3) - itself is valid value: # let (_, c) = b;; val c : int * int = (2, 3) # let d = snd b;; val d : int * int = (2, 3) What is the reason to not parse "(1, 2, 3)" as "(1, (2, 3))" and instead introduce infinite (or, even worse, finite) amount of new type and value constructors for different arities? What