tuples

Haskell - apply tuple of functions to tuple of values?

房东的猫 提交于 2020-01-02 05:19:08
问题 I have a tuple of values representing some state, and want to translate it by an addition (shift). My values are a longer version of ( Int, [Int], Int), and I want something conceptually (but not literally) like this: shift n = ??? (+n) (id, map, id) -- simple(?) which would be equivalent to: shift n (a, b, c) = (a+n, map (+n) b, c+n) I am happy to just go with this explicit function usage, but wondered it there was a more idiomatic point-free version using Applicative or Arrows or ..., or if

Extracting Information from a Tuple (Python)

萝らか妹 提交于 2020-01-02 04:49:05
问题 I'm currently using the httplib library in Python 2.7 to obtain some headers from a website to establish a) the filesize of a download and b) the last modified date of the file. I've used some online tools and these details do exist. I'm currently scripting my Python code and it appears to work correctly bringing back the required information. Nonetheless, the response containing the header information is a list containing a number of tuples. A sample of the response is below:- [('content

Weird behavior accessing tuple from Java

狂风中的少年 提交于 2020-01-02 03:30:11
问题 I am looking for explanation and/or versioning details (if possible) about a very strange behavior I found in Java accessing tuples created in Scala. I will show the weird behavior with an easy test I did. I created this Scala class: class Foo { def intsNullTuple = (null.asInstanceOf[Int], 2) def intAndStringNullTuple = (null.asInstanceOf[Int], "2") } and then I run this Java program: Tuple2<Object, Object> t = (new Foo()).intsNullTuple(); t._1(); // returns 0 ! t._1; // return null Tuple2

How to create a non-TH package from code generated using Template Haskell?

一笑奈何 提交于 2020-01-02 02:04:08
问题 I'm making a small package that defines wrappers for tuples and adds instances form them, like newtype Tuple2 a = Tuple2 { untuple2 :: (a, a) } deriving (...) tuple2 :: a -> a -> Tuple2 a tuple2 = ... instance Traversable Tuple2 where ... instance Foldable Tuple2 where ... instance Functor Tuple2 where ... instance Applicative Tuple2 where ... This repeats from 2 to 15, so it looks like a job for Template Haskell. The generated code is always Haskell 98 compatible, so I'd like the final

Limitations of PyTuple_SetItem

蹲街弑〆低调 提交于 2020-01-02 01:20:50
问题 I have a Python extension module which creates a tuple as an attribute of another object, and sets items in the tuple. Whenever I execute this module in Python, I keep getting the error SystemError: bad argument to internal function After reading over the docs for PyTuple , and debugging my program for a few hours, I still couldn't figure out what the hell was going on. Running my program through a debugger indicated the problem was occurring within a library call inside the Python

List lookup faster than tuple?

可紊 提交于 2020-01-01 23:53:11
问题 In the past, when I've needed array-like indexical lookups in a tight loop, I usually use tuples, since they seem to be generally extremely performant (close to using just n-number of variables). However, I decided to question that assumption today and came up with some surprising results: In [102]: l = range(1000) In [103]: t = tuple(range(1000)) In [107]: timeit(lambda : l[500], number = 10000000) Out[107]: 2.465047836303711 In [108]: timeit(lambda : t[500], number = 10000000) Out[108]: 2

how to convert numpy array into tuple

▼魔方 西西 提交于 2020-01-01 19:15:35
问题 I need to convert array like this: [[1527 1369 86 86] [ 573 590 709 709] [1417 1000 68 68] [1361 1194 86 86]] to like this: [(726, 1219, 1281, 664), (1208, 1440, 1283, 1365), (1006, 1483, 1069, 1421), (999, 1414, 1062, 1351),] I tried using convert diretly to tuple but got this: ( array([1527, 1369, 86, 86], dtype=int32), array([573, 590, 709, 709], dtype=int32), array([1417, 1000, 68, 68], dtype=int32), array([1361, 1194, 86, 86], dtype=int32)) (array([701, 899, 671, 671], dtype=int32),) 回答1

Sorting a dictionary of tuples in Python

耗尽温柔 提交于 2020-01-01 08:57:51
问题 I know there's tonnes of questions on python sorting lists/dictionaries already, but I can't seem to find one which helps in my case, and i'm looking for the most efficient solution as I'm going to be sorting a rather large dataset. My data basically looks like this at the moment: a = {'a': (1, 2, 3), 'b': (3, 2, 1)} I'm basically creating a word list in which I store each word along with some stats about it (n, Sigma(x), Sigma(x^2) ) I want to sort it based on a particular stat. So far I've

Convert dict of nested lists to list of tuples

 ̄綄美尐妖づ 提交于 2020-01-01 08:34:46
问题 I have dict of nested list s: d = {'a': [[('a1', 1, 1), ('a2', 1, 2)]], 'b': [[('b1', 2, 1), ('b2', 2, 2)]]} print (d) {'b': [[('b1', 2, 1), ('b2', 2, 2)]], 'a': [[('a1', 1, 1), ('a2', 1, 2)]]} I need create list of tuple s like: [('b', 'b1', 2, 1), ('b', 'b2', 2, 2), ('a', 'a1', 1, 1), ('a', 'a2', 1, 2)] I tried: a = [[(k, *y) for y in v[0]] for k,v in d.items()] a = [item for sublist in a for item in sublist] I think my solution is a bit over-complicated. Is there some better, more pythonic

Ordered subsets test

a 夏天 提交于 2020-01-01 05:42:06
问题 I want to test if an ordered set is a subset of a bigger ordered set. I used tuples and itertools.combinations : def subset_test(a, b): return a in itertools.combinations(b, len(a)) For instance, >>> subset_test((0, 1, 2), (0, 3, 1, 4, 2)) True >>> subset_test((0, 1, 2), (0, 3, 2, 4, 1)) False It works, but is slow when I test big tuples. 回答1: You can simply use an iterator to keep track of the position in B >>> A = (0, 1, 2) >>> B = (0, 3, 1, 4, 2) >>> b_iter = iter(B) >>> all(a in b_iter