tuples

How do you write the function 'pairs' in Haskell?

独自空忆成欢 提交于 2019-12-20 17:31:36
问题 The pairs function needs to do something like this: pairs [1, 2, 3, 4] -> [(1, 2), (2, 3), (3, 4)] 回答1: pairs [] = [] pairs xs = zip xs (tail xs) 回答2: You could go as far as import Control.Applicative (<*>) pairs = zip <*> tail but pairs xs = zip xs (tail xs) is probably clearer. 回答3: Just for completeness, a more "low-level" version using explicit recursion: pairs (x:xs@(y:_)) = (x, y) : pairs xs pairs _ = [] The construct x:xs@(y:_) means "a list with a head x , and a tail xs that has at

Void type in std::tuple

不羁岁月 提交于 2019-12-20 10:29:04
问题 Obviously, you can't have an instance of type void in a well-formed program, so something like the following declaration won't compile: std::tuple<void, double, int> tup; However, as long as we're dealing strictly with types as opposed to objects, there seems to be no issue. For example, my compiler (GCC) lets me say: typedef std::tuple<void, double, int> tuple_type; This is interesting to me, because it seems that with C++0x we can just use std::tuple to perform a lot of the meta-programming

Why raising a tuple works if first element is an Exception?

杀马特。学长 韩版系。学妹 提交于 2019-12-20 10:08:02
问题 I have a hard time figuring this one out, it's about mistakes that can be done when raising an exception in Python 2.7: try: raise [1, 2, 3, 4] except Exception as ex: print ex the message here is "exceptions must be old-style classes or derived from BaseException, not list" - This part is ok, but when I change it to tuple, I am getting confused: try: raise (1, 2, 3, 4) except Exception as ex: print ex the message here is "exceptions must be old-style classes or derived from BaseException,

Haskell tuple constructor (GHC) and the separation between a language and its implementation

无人久伴 提交于 2019-12-20 09:32:35
问题 Haskell blew my mind yet again when I realised that (x,y) Is just syntactic sugar for (,) x y Naturally I wanted to extend this to larger tuples. But (,) x ((,) y z) Gave me (x,(y,z)) Which was not what I was looking for. On a whim, I tried (,,) x y z And it worked, giving exactly what I wanted: (x,y,z) This raised the question: How far can you take it? Much to my astonishment, there seemed to be no limit. All of the below are valid operators: (,) (,,) (,,,) (,,,,) --etc (,,,,,,,,,,,,,,) (,,,

python sort tuple by different criteria

好久不见. 提交于 2019-12-20 07:47:48
问题 I have a list a = [(1,'a'), (1,'b'), (2,'c')] , and I want to get this list: [(2,'c'), (1,'a'), (1,'b')] If I do this: sorted(a, reverse=True) I can only get: [(2,'c'), (1,'b'), (1,'a')] How can I get the list I want? 回答1: If you want to preserve the sort order in the original list, but sort only by the first element, you can do >>> from operator import itemgetter >>> a = [(1,'a'), (1, 'x'), (1,'b'), (2,'c')] >>> sorted(a, key=itemgetter(0), reverse=True) [(2, 'c'), (1, 'a'), (1, 'x'), (1, 'b

How to fix 'Non-exhaustive patterns in function' error in haskell?

允我心安 提交于 2019-12-20 06:29:19
问题 I am trying to create a function which takes a list of tuples as argument and sort the by the second element. It doesn't print anytning else, just the error '*** Exception: main.hs:20:1-76: Non-exhaustive patterns in function sortWords' Here is the code: sortWords :: [(String, Int)] -> [(String, Int)] sortWords [(str,num)] = sortBy (\x y -> compare (snd x) (snd y)) [(str,num)]` And here is how I call the function main = do putStrLn $ show $ sortWords [("friend",1),("she",2)] I have to say

Converting Tuple to Dictionary

房东的猫 提交于 2019-12-20 06:24:50
问题 i'm parsing an XML file and getting a tuple in return. i converted the tuple to str and then to dictionary. i want to get the key and value for Lanestat. for eg: Lanestat, key 1 and get value 2. but the code is not elegant, appreciate any advice. tq xml: - <Test> - <Default_Config> <LINK>{1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6}</LINK> <Lanestat>{1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12}</Lanestat> </Default_Config> </Test> output: ('LINK', '{1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6}') ('Lanestat', '{1: 2,

Converting a list of ints, tuples into an numpy array

╄→尐↘猪︶ㄣ 提交于 2019-12-20 05:50:54
问题 I have a list of [float, (float,float,float..) ] ... Which is basically an n-dimensional point along with a fitness value for each point. For eg. 4.3, (2,3,4) 3.2, (1,3,5) . . 48.2, (23,1,32) I wish to randomly sample one point based upon the fitness values. I decided the best way to do this would be to use numpy.random.choice(range(n), 1, plist[:,:1,:1]) However, i need to convert this into an numpy array, for which i tried >> pArr = np.array( plist ) ValueError: setting an array element

Remove a tuple containing nan in list of tuples — Python

▼魔方 西西 提交于 2019-12-20 05:13:31
问题 I have a long list of tuples and want to remove any tuple that has a nan in it using Python. What I currently have: x = [('Recording start', 0), (nan, 4), (nan, 7), ..., ('Event marker 1', 150)] Result I'm looking for: x = [('Recording start', 0), ('Event marker 1', 150)] I've tried use np.isnan and variants of that, but have had no success and keep getting an error: ufunc 'isnan' is not supported for the input types, and the inputs could not be safely coerced to any supported types according

Remove a tuple containing nan in list of tuples — Python

心已入冬 提交于 2019-12-20 05:13:04
问题 I have a long list of tuples and want to remove any tuple that has a nan in it using Python. What I currently have: x = [('Recording start', 0), (nan, 4), (nan, 7), ..., ('Event marker 1', 150)] Result I'm looking for: x = [('Recording start', 0), ('Event marker 1', 150)] I've tried use np.isnan and variants of that, but have had no success and keep getting an error: ufunc 'isnan' is not supported for the input types, and the inputs could not be safely coerced to any supported types according