tuples

Is `namedtuple` really as efficient in memory usage as tuples? My test says NO

你说的曾经没有我的故事 提交于 2019-12-06 18:08:03
问题 It is stated in the Python documentation that one of the advantages of namedtuple is that it is as memory-efficient as tuples. To validate this, I used iPython with ipython_memory_usage. The test is shown in the images below: The test shows that: 10000000 instances of namedtuple used about 850 MiB of RAM 10000000 tuple instances used around 73 MiB of RAM 10000000 dict instances used around 570 MiB of RAM So namedtuple used much more memory than tuple ! Even more than dict !! What do you think

Tuple == Confusion

情到浓时终转凉″ 提交于 2019-12-06 17:11:04
问题 Suppose I define two tuples: Tuple<float, float, float, float> tuple1 = new Tuple<float, float, float, float>(1.0f, 2.0f, 3.0f, 4.0f); Tuple<float, float, float, float> tuple2 = new Tuple<float, float, float, float>(1.0f, 2.0f, 3.0f, 4.0f); If I try to compare the tuples, I get different results bool result1 = (tuple1 == tuple2); // FALSE bool result2 = tuple1.Equals(tuple2); // TRUE I would expect for both calls to return true. What exactly is == comparing? 回答1: For Tuple, the == is

Swift: Get an array of element from an array of tuples

為{幸葍}努か 提交于 2019-12-06 17:02:50
问题 I have an array of tuples like this : var answers: [(number: Int, good: Bool)] I want to get from it an array of number member. Like if I did something like : answers["number"] // -> Should give [Int] of all values named "number" I didn't find anything like it, maybe it's not possible, but it would be sad :( 回答1: That's simple: answers.map { $0.number } 回答2: var ints = answers.map { tuple in tuple.0 } 回答3: If your tuple is not named you can do this: let mappedInts = answers.map({$0.0}) let

lookup values from itemgetter in a Dict (python)

让人想犯罪 __ 提交于 2019-12-06 16:45:20
I have a list of tuples that I'm trying to sort. The tuples contain strings: connectionsList = [('C', 'B'), ('A', 'C'), ('D', 'B'), ('C','D')] The strings in the tuples have numeric values stored in a dict: valuesDict = {'A':3, 'B':5, 'C':1, 'D':2} What I'd like to do is sort the list by the sum of the values in the dict for the tuples. Desired output for this toy example would be: [(C,D), (A,C), (C,B), (D,B)] which would have the sums of: [3, 4, 6, 7] Using itemgetter, I can sort alphabetically by position: sortedView = sorted(connectionsList, key=itemgetter(0,1)) However, I'm having trouble

Convert anonymous type to new C# 7 tuple type

我只是一个虾纸丫 提交于 2019-12-06 16:36:36
问题 The new version of C# is there, with the useful new feature Tuple Types: public IQueryable<T> Query<T>(); public (int id, string name) GetSomeInfo() { var obj = Query<SomeType>() .Select(o => new { id = o.Id, name = o.Name, }) .First(); return (id: obj.id, name: obj.name); } Is there a way to convert my anonymous type object obj to the tuple that I want to return without mapping property by property (assuming that the names of the properties match)? The context is in a ORM, my SomeType object

Tuple with missing value

末鹿安然 提交于 2019-12-06 16:31:53
I have a function in python that does error checking on a string. If the function finds an error it will return a tuple with an error code and an identifier for that error. Later in the program I have the function that handles the error. It accepts the tuple into two variables. errCode, i = check_string(s,pathType) check_string is the function that produces the tuple. What I want it to does is return just zero if there is no error. When a zero is returned the above code seems to fail because there is nothing for the variable i. Is there an easy way to get this to work or do I just need to have

Boost Spirit char parser

风格不统一 提交于 2019-12-06 15:40:40
Here is a code sample: // file main.cpp #include <iostream> #include <boost/tuple/tuple.hpp> #include <boost/tuple/tuple_io.hpp> #include <boost/spirit/include/qi.hpp> int main() { std::string s( "1 A" ); boost::tuple<double, char> p; complex_matrix_parser::iterator b = s.begin(); complex_matrix_parser::iterator e = s.end(); qi::phrase_parse( b, e, ( qi::double_ >> qi::char_('A') ), qi::space, qi::skip_flag::postskip, p ); std::cerr << "==== " << p << std::endl; return 0; } This should print ==== (1 A) right? But it prints ==== (1 ) , so it skips the 'A' character. What am I doing wrong here?

Separate tuple from a nested list into a separate list

狂风中的少年 提交于 2019-12-06 15:34:14
I need to separate a tuple based on a value from a nested dictionary as below and put it in another list. I want to separate tuple with values 'bb' original_list= [[('aa','1'),('bb','2')],[('cc','3'),('bb','4')],[('dd','5'),('dd','6')]] I need two lists as below, final_list= [[('aa','1')],[('cc','3')],[('dd','5'),('dd','6')]] deleted_list = [[('bb','2')],[('bb','4')]] I used the following recursive code, def remove_items(lst, item): r = [] for i in lst: if isinstance(i, list): r.append(remove_items(i, item)) elif item not in i: r.append(i) return r It could produce the result list after

how to produce a nested list from two lists in python

泄露秘密 提交于 2019-12-06 15:33:11
I am a newbie in python, I have two lists: l1 = ['a','b','c','d'] l2 = ['new'] i want to get new list like this l3 = [('a','new'),('b','new'),('c','new'),('d','new')] What is the best way to combine the two lists? >>> from itertools import product >>> l1 = ['a','b','c','d'] >>> l2 = ['new'] >>> list(product(l1,l2)) [('a', 'new'), ('b', 'new'), ('c', 'new'), ('d', 'new')] If l2 always just has the one element there is no need to overcomplicate things l3 = [(x, l2[0]) for x in l1] See the itertools docs . In particular, use product for a Cartesian product: from itertools import product: l1 = ['a

Searching through a tuple for arguments of a function

天涯浪子 提交于 2019-12-06 14:56:44
Consider this int foo (int a, char c, bool b) {std::cout << a << ' ' << c << ' ' << b << '\n'; return 8;} double bar (int a, char c, bool b, int d) {std::cout << a << ' ' << c << ' ' << b << ' ' << d << '\n'; return 2.5;} char baz (bool a, bool b) {std::cout << a << ' ' << b << '\n'; return 'a';} int main() { const auto tuple = std::make_tuple(5, true, 'a', 3.5, false, 1000, 't', 2, true, 5.8); const std::tuple<int, double, char> t = searchArguments (tuple, foo, bar, baz); } So the arguments for foo are first searched (from tuple ). Searching from left to right, the first int found is 5 , the