tuples

python: class vs tuple huge memory overhead (?)

三世轮回 提交于 2019-12-04 03:19:07
I'm storing a lot of complex data in tuples/lists, but would prefer to use small wrapper classes to make the data structures easier to understand, e.g. class Person: def __init__(self, first, last): self.first = first self.last = last p = Person('foo', 'bar') print(p.last) ... would be preferable over p = ['foo', 'bar'] print(p[1]) ... however there seems to be a horrible memory overhead: l = [Person('foo', 'bar') for i in range(10000000)] # ipython now taks 1.7 GB RAM and del l l = [('foo', 'bar') for i in range(10000000)] # now just 118 MB RAM Why? is there any obvious alternative solution

Is it possible to unpack a tuple without using variables?

情到浓时终转凉″ 提交于 2019-12-04 02:56:17
问题 I'm using the os.path.split() function on a path in my program to get the filename and pathname of a file then passing them into another method, but my current solution seems rather ugly: path = os.path.split(somefile) some_class(path[0], path[1]) Is it possible to unpack the path tuple in a cleaner way within the call to some_class? Something like: some_class(os.path.split(somefile).unpack()) Or should I simply be going about this another way? Maybe a more pythonic way? 回答1: Yes, Python has

Sorting a dictionary of tuples in Python

烂漫一生 提交于 2019-12-04 02:51:14
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 been trying something along the lines of: b = a.items() b.sort(key = itemgetter(1), reverse=True) I'm

General 'map' function for Scala tuples?

橙三吉。 提交于 2019-12-04 02:44:03
I would like to map the elements of a Scala tuple (or triple, ...) using a single function returning type R. The result should be a tuple (or triple, ...) with elements of type R. OK, if the elements of the tuple are from the same type, the mapping is not a problem: scala> implicit def t2mapper[A](t: (A,A)) = new { def map[R](f: A => R) = (f(t._1),f(t._2)) } t2mapper: [A](t: (A, A))java.lang.Object{def map[R](f: (A) => R): (R, R)} scala> (1,2) map (_ + 1) res0: (Int, Int) = (2,3) But is it also possible to make this solution generic, i.e. to map tuples that contain elements of different types

template function with corresponding parameters to subset of tuple types

余生长醉 提交于 2019-12-04 02:39:52
I would like to write function as this find : multi_set<int, string, double, myType> m; //vector of tuples m.insert(/*some data*/); m.find<1,2>("something",2.123); Or m.find<0,3>(1,instanceOfMyType); m.find<1>("somethingelse"); Where find can be parametrized corresponding to any subset of tuple parameters. My code so far: template <typename ... T> class multi_set{ typedef tuple < T... > Tuple; vector<tuple<T...>> data = vector<tuple<T...>>(); public: void insert(T... t){ data.push_back(tuple<T...>(t...)); } template<size_t ... Pos> void find(???){ // then I would like to use those params to

Applying a function to each element of a tuple

笑着哭i 提交于 2019-12-04 02:38:47
Given an std::tuple -like object (i.e. with defined tuple_size and get semantics) and a unary functor object ftor , I want to be able to call ftor on each element of the tuple -like object. If I disregard the return value, I am aware of the int array trick: namespace details { template <typename Ftor, typename Tuple, size_t... Is> void apply_unary(Ftor&& ftor, Tuple&& tuple, std::index_sequence<Is...>) { using std::get; int arr[] = { (ftor(get<Is>(std::forward<Tuple>(tuple))), void(), 0)... }; } } // namespace details template <typename Ftor, typename Tuple> void apply_unary(Ftor&& ftor, Tuple

What is the difference between value constructors and tuples?

落花浮王杯 提交于 2019-12-04 02:22:25
It's written that Haskell tuples are simply a different syntax for algebraic data types. Similarly, there are examples of how to redefine value constructors with tuples. For example, a Tree data type in Haskell might be written as data Tree a = EmptyTree | Node a (Tree a) (Tree a) which could be converted to "tuple form" like this: data Tree a = EmptyTree | Node (a, Tree a, Tree a) What is the difference between the Node value constructor in the first example, and the actual tuple in the second example? i.e. Node a (Tree a) (Tree a) vs. (a, Tree a, Tree a) (aside from just the syntax)? Under

from list of tuples, get tuple closest to a given value

你离开我真会死。 提交于 2019-12-04 02:03:53
问题 Given a list of tuples containing coordinates, I want to find which coordinate is the closest to a coordinate I give in input: cooList = [(11.6702634, 72.313323), (31.67342698, 78.465323)] coordinate = (11.6702698, 78.113323) takenearest(myList, myNumber) ... (11.6702634, 72.313323) Please let me know... 回答1: For your data cooList = [(11.6702634, 72.313323), (31.67342698, 78.465323)] coordinate = (11.6702698, 78.113323) the shortest Pythonic answer is: nearest = min(cooList, key=lambda x:

Python: Convert tuple to comma separated String

依然范特西╮ 提交于 2019-12-04 01:38:30
问题 import MySQLdb db = MySQLdb.connect("localhost","root","password","database") cursor = db.cursor() cursor.execute("SELECT id FROM some_table") u_data = cursor.fetchall() >>> print u_data ((1320088L,),) What I found on internet got me till here: string = ((1320088L,),) string = ','.join(map(str, string)) >>> print string (1320088L,) what I expect output to look like: #Single element expected result 1320088L #comma separated list if more than 2 elements, below is an example 1320088L,1320089L

Tuple slicing not returning a new object as opposed to list slicing

心已入冬 提交于 2019-12-04 01:17:51
In Python (2 and 3). Whenever we use list slicing it returns a new object, e.g.: l1 = [1,2,3,4] print(id(l1)) l2 = l1[:] print(id(l2)) Output >>> 140344378384464 >>> 140344378387272 If the same thing is repeated with tuple, the same object is returned, e.g.: t1 = (1,2,3,4) t2 = t1[:] print(id(t1)) print(id(t2)) Output >>> 140344379214896 >>> 140344379214896 It would be great if someone can shed some light on why this is happening, throughout my Python experience I was under the impression empty slice returns a new object. My understanding is that it's returning the same object as tuples are