在不使用Boost的情况下,在以下代码中编写readvals函数的最佳方法是什么?基本上,它应该得到一个元组,调用它的elemets的特定函数,并将生成的结果再次作为元组返回.
是否有基于C++ 0X的元组Functor定义库?
template <class T>
struct A
{
    A(T _val):val(_val){}
    T ret() {return val;}
    T val;
};
template <typename... ARGS>
std::tuple<ARGS...> readvals(std::tuple<A<ARGS>...> targ)
{
    //???
}
int main(int argc, char **argv)
{
    A<int> ai = A<int>(5);
    A<char> ac = A<char>('c');
    A<double> ad = A<double>(0.5);
    std::tuple<A<int>,A<char>,A<double>> at = std::make_tuple(ai,ac,ad);
    // assuming proper overloading of "<<" for tuples exists
    std::cout << readvals<int,char,double>(at) << std::endl;
    // I expect something like (5, c, 0.5) in the output
    return 0;
}
我在SO上发现了一些问题,这些问题部分地解决了这个问题(元组解包,迭代元组元素等),但在我看来,与将所有这些解决方案放在一起相比,应该有一个更简单的解决方案.
我一直在得到奇怪的结果,我终于注意到我在一个元组中放置空格的习惯导致了这个问题.如果你可以重现这个问题并告诉我它为什么会这样运作,那么你就可以保存我的头发.谢谢!
jcomeau@intrepid:/tmp$ cat haversine.py
#!/usr/bin/python
def dms_to_float(degrees):
 d, m, s, compass = degrees
 d, m, s = int(d), float(m), float(s)
 float_degrees = d + (m / 60) + (s / 3600)
 float_degrees *= [1, -1][compass in ['S', 'W', 'Sw']]
 return float_degrees
jcomeau@intrepid:/tmp$ python
Python 2.6.7 (r267:88850, Jun 13 2011, 22:03:32) 
[GCC 4.6.1 20110608 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from haversine import *
>>> dms_to_float((111, 41, 0, 'SW'))
111.68333333333334
>>> dms_to_float((111,41,0,'Sw'))
-111.68333333333334
在元组中有空格,答案是错误的.没有,答案是正确的.
Python 3学习者:
这个问题有以下公认的答案:
rr,tt = zip(*[(i*10, i*12) for i in xrange(4)])
返回两个元组.如果有人可以打破答案并解释它在Python 3中做了什么(我知道range()在Python 3中返回迭代器),我将不胜感激.我理解列表理解但是我对解压缩感到困惑(我认为你只能使用星号表达式作为赋值目标的一部分).
我同样对以下代码感到困惑.我理解结果和拉链(或者我认为),但是星号表达再次击败了我.
x2, y2 = zip(*zip(x, y))
从这个:
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]
>>> x2, y2 = zip(*zip(x, y))
>>> x == list(x2) and y == list(y2)
True
实际上这是三个例子.
>>> result = []
>>> for k in range(10):
>>>    result += k*k
>>> result = []
>>> for k in range(10):
>>>    result.append(k*k)
>>> result = [k*k for k in range(10)]
第一个出错.错误打印如下
TypeError: 'int' object is not iterable
但是,第二个和第三个效果很好.
我无法理解这三个陈述之间的区别.
我试图理解Python,但我仍然没有得到它.我是这门语言的新手,想要正确理解它.这是使用循环的Fibonacci序列的一条线.请解释此代码的含义.我试图手工获得模式.我得到的模式最多为3,但在3之后我得不到答案.
a, b = 0, 1
while b < 50:
    print(b)
    a, b = b, a + b
为什么以下代码会在 Python 2.7.3 中抛出SyntaxErrorfor *phones?
contact = ('name', 'email', 'phone1', 'phone2')
name, email, *phones = contact
这是在 Python 3 中引入的而不是向后移植的吗?我怎样才能让它在 Python 2 中工作?也就是说,如果这里没有一些简单的方法可以解决问题。
[foo(item['fieldA'], item['fieldC']) for item in barlist]
有没有相同的地图?
我的意思是,像这样:
map(foo, [(item['fieldA'], item['fieldB']) for item in barlist])
但它不起作用.只是好奇.
根据此,我可以调用一个函数,N个参数与含有这些参数的元组,具有f(*my_tuple)。
有没有办法结合解包和解包变量?
就像是:
def f(x,y,z):...
a = (1,2)
f(*a, 3)
我有这本字典,
states = {
    'CT': 'Connecticut',
    'CA': 'California',
    'NY': 'New York',
    'NJ': 'New Jersey'
    }
和代码在这里..
state2 = {state: abbrev for abbrev, state in states.items()}
我试图了解这是什么以及如何abbrev for abbrev工作。我也不清楚究竟state:是什么。我得到了第二部分(states.items() 中的状态)。这个的输出给出
{'Connecticut': 'CT', 'California': 'CA', 'New York': 'NY', 'New Jersey': 'NJ'}
但我不确定这是如何工作的.. 在此先感谢您。
python dictionary dictionary-comprehension iterable-unpacking
为什么这个解包不适用于我的字典?
字典构建如下:
cutDict = {'scene1': [('scene3', 1001, 1125)]} 
我想解压缩这个:
for key, (cut, fIn, fOut) in cutDict.iteritems():   
    print key 
    print cut
    print fIn
    print fOut
但结果是:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack
现在我正在通过以下方式构建字典:
cutDict.setdefault(itm, [])
for cutItm in itm.cut_items_ordered:
    cutInOut = (mostcutItm, recentcutItm.cut_item_in, callcutItm.cut_item_out)
    cutDict[itm].append(cutInOut): 
python ×8
python-2.7 ×3
dictionary ×2
python-3.x ×2
c++ ×1
c++11 ×1
functor ×1
int ×1
iterator ×1
loops ×1
map-function ×1
tuples ×1