tuples

Python tuple vs generator

孤人 提交于 2019-12-09 09:10:39
问题 I am having a problem understanding why one of the following line returns generator and another tuple. How exactly and why a generator is created in the second line, while in the third one a tuple is produced? sample_list = [1, 2, 3, 4] generator = (i for i in sample_list) tuple_ = (1, 2, 3, 4) print type(generator) <type 'generator'> print type(tuple_) <type 'tuple'> Is it because tuple is immutable object and when I try to unpack list inside () , it can't create the tuple as it has to

How to make a tuple of const references?

半城伤御伤魂 提交于 2019-12-09 08:28:37
问题 Say there are two functions: void ff( const std::tuple<const int&> ) { } template < typename TT > void gg( const std::tuple<const TT&> ) { } and calls to these functions: int xx = 0; ff( std::tie( xx ) ); // passes gg( std::tie( xx ) ); // FAILS !! GCC 4.7.2 fails to compile the last line and reports an error note like: note: template argument deduction/substitution failed: note: types ‘const TT’ and ‘int’ have incompatible cv-qualifiers note: ‘std::tuple<int&>’ is not derived from ‘std:

Save a tuple in NSUserDefaults

一曲冷凌霜 提交于 2019-12-09 03:11:49
问题 I'm using a tuple to store something like this. var accessLavels: (hasInventoryAccess: Bool, hasPayrolAccess: Bool) accessLavels = (hasInventoryAccess: true, hasPayrolAccess: false) Now I want to save it in NSUserDefaults . NSUserDefaults.standardUserDefaults().setValue(accessLavels, forKey: "AccessLevelKey") NSUserDefaults.standardUserDefaults().synchronize() But I get the following error. Type '(hasInventoryAccess: Bool, hasPayrolAccess: Bool)' does not conform to protocol 'AnyObject' How

Count frequency of item in a list of tuples

£可爱£侵袭症+ 提交于 2019-12-09 02:39:45
问题 I have a list of tuples as shown below. I have to count how many items have a number greater than 1. The code that I have written so far is very slow. Even if there are around 10K tuples, if you see below example string appears two times, so i have to get such kind of strings. My question is what is the best way to achieve the count of strings here by iterating over the generator List: b_data=[('example',123),('example-one',456),('example',987),.....] My code so far: blockslst=[] for line in

Why do tuples need parantheses in list comprehension

痴心易碎 提交于 2019-12-08 19:51:19
问题 It is well known that tuples are not defined by parentheses, but commas. Quote from documentation: A tuple consists of a number of values separated by commas Therefore: myVar1 = 'a', 'b', 'c' type(myVar1) # Result: <type 'tuple'> Another striking example is this: myVar2 = ('a') type(myVar2) # Result: <type 'str'> myVar3 = ('a',) type(myVar3) # Result: <type 'tuple'> Even the single-element tuple needs a comma, and parentheses are always used just to avoid confusion. My question is: Why can't

Do tuples have an implicit lexicographical comparison?

家住魔仙堡 提交于 2019-12-08 17:37:45
问题 When sorting eg a vector of pairs : vector<pair<int, double>> v; sort(v.begin(), v.end()); You don't need to specify a sorting criterion to have a sorting based on the lexicographical order of the pairs since, when not elseway specified, a lexicographical comparison applies. Is a similar behaviour standard for tuples as well ? In VS2012 this compiles vector<tuple<int, double, char>> tv; sort(tv.begin(), tv.end()); but is it standard mandated to do so ? 回答1: They do, see operator==,!=,<,<=,>,>

Add a tuple to a specific cell of a pandas dataframe

偶尔善良 提交于 2019-12-08 17:32:26
问题 Just when I thought I was getting the hang of Python and Pandas, another seemingly simple issue crops up. I want to add tuples to specific cells of a pandas dataframe. These tuples need to be calculated on-the-fly based on the contents of other cells in the dataframe - in other words, I can't easily calculate all tuples in advance and add them as a single array. As an example, I define a dataframe with some data and add a couple of empty columns: import pandas as pd import bumpy as np tempDF

Using std::get and std::tie on boost tuples, zip_iterator, etc

别等时光非礼了梦想. 提交于 2019-12-08 17:32:14
问题 What are my options for using std::get<>() and std::tie<>() together with boost constructs? Example: I want to use range-based for-loop for iteration over several containers. I can implement zip function, which uses boost::zip_iterator . #include <boost/iterator/zip_iterator.hpp> #include <boost/range.hpp> template <typename... TContainer> auto zip(TContainer&... containers) -> boost::iterator_range<boost::zip_iterator<decltype(boost::make_tuple(std::begin(containers)...))>> { auto zip_begin

Unpacking a 1-tuple in a list of length 1

☆樱花仙子☆ 提交于 2019-12-08 16:27:23
问题 Suppose I have a tuple in a list like this: >>> t = [("asdf", )] I know that the list always contains one 1-tuple. Currently I do this: >>> dummy, = t >>> value, = dummy >>> value 'asdf' Is there a shorter and more elegant way to do this? 回答1: >>> t = [("asdf", )] >>> t[0][0] 'asdf' 回答2: Try (value,), = t It's better than t[0][0] because it also asserts that your list contains exactly 1 tuple with 1 value in it. 回答3: Try [(val, )] = t In [8]: t = [("asdf", )] In [9]: (val, ) = t In [10]: val

cannot create a unary tuple with an empty tuple in it (c++0x)

Deadly 提交于 2019-12-08 15:50:42
问题 I was experimenting with tuples and encountered a problem with creating tuples. The code example is as follows. //a.cpp #include <tuple> using namespace std; int main() { auto te = make_tuple(); //this line is ok auto tte = make_tuple(te); //this line gives an error. return 0; } I compiled it with both g++ 4.5 (g++ -std=c++0x a.cpp) and MS VC++2010. Both compilers are giving me an error on the second line in main(). My question is this: Since 'te' is a well-defined variable, why can't another