dict方法dict.keys(),dict.items()和dict.values()返回"views"而不是list. http://docs.python.org/dev/3.0/whatsnew//3.0.html
首先,视图与迭代器有何不同?其次,这种变化有什么好处?这只是出于性能原因吗?
这对我来说似乎不太直观,也就是说,我要求列出一些东西(给我所有的钥匙),然后我又得到了别的东西.这会让人迷惑吗?
作为这篇文章的后续内容,我想知道如何make_unique通过分配函数临时缓冲区数组来实现它的播放,如下面的代码所示.
f()
{
auto buf = new int[n]; // temporary buffer
// use buf ...
delete [] buf;
}
Run Code Online (Sandbox Code Playgroud)
可这与一些调用所替换,以make_unique与将[]的删除-version被继续使用?
我们如何在python 3中声明单个字节变量?我想实现C中表示的以下结果:
unsigned char = 0xFF;
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以在python中声明一个8位变量.
谢谢你的回答
我有一个Exception类,我想在它抛出之前设置更多信息.我可以创建Exception对象,调用它的一些函数然后抛出它而不用它的任何副本吗?
我发现的唯一方法是抛出一个指向对象的指针:
class Exception : public std::runtime_error
{
public:
Exception(const std::string& msg) : std::runtime_error(msg) {}
void set_line(int line) {line_ = line;}
int get_line() const {return line_;}
private:
int line_ = 0;
};
std::unique_ptr<Exception> e(new Exception("message"));
e->set_line(__LINE__);
throw e;
...
catch (std::unique_ptr<Exception>& e) {...}
Run Code Online (Sandbox Code Playgroud)
但通常避免通过指针抛出异常,那么还有其他方法吗?
还可以选择通过构造函数设置所有选项,但如果将更多字段添加到类中并且您希望对要设置的字段进行细粒度控制,则可以快速变为不可扩展:
throw Exception("message"); // or:
throw Exception("message", __LINE__); // or:
throw Exception("message", __FILE__); // or:
throw Exception("message", __LINE__, __FILE__); // etc.
Run Code Online (Sandbox Code Playgroud) 我的代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
print (round(1.555, 1)) # It seems normal
print (round(1.555, 2)) # Why it is not output 1.56?
print (round(1.556, 2)) # It seems normal
Run Code Online (Sandbox Code Playgroud)
输出:
sam@sam:~/code/python$ ./t2.py
1.6
1.55
1.56
sam@sam:~/code/python$
Run Code Online (Sandbox Code Playgroud)
round(1.555, 1)输出1.6.
为什么不round(1.555, 2)输出1.56?
在Python 2中,我可以执行以下操作:
>> d = {'a':1}
>> extras = [{'b':2}, {'c':4}]
>> map(d.update, extras)
>> d['c']
>> 4
Run Code Online (Sandbox Code Playgroud)
在Python 3中得到一个KeyError:
>> d = {'a':1}
>> extras = [{'b':2}, {'c':4}]
>> map(d.update, extras)
>> d['c']
>> KeyError: 'c'
Run Code Online (Sandbox Code Playgroud)
我想在Python 3中实现与Python 2中相同的行为.
我知道Python 3中的map将返回一个迭代器(lazy evaluation和whatnot),必须迭代它才能更新字典.
我假设d['c']密钥查找会以某种方式触发映射迭代,但事实并非如此.
有没有pythonic方法来实现这种行为而不编写for循环,我发现与map相比,它是冗长的.
我想过使用列表推导:
>> d = {'a':1}
>> extras = [{'b':2}, {'c':4}]
>> [x for x in map(d.update, extras)]
>> d['c']
>> 4
Run Code Online (Sandbox Code Playgroud)
但它似乎并不像pythonic.
我有这个代码,它显示目录本身的文件夹,而不是其内容.我想显示它的内容.我不想使用boost :: filesystem.
我该如何解决这个问题?
码:
#include <windows.h>
#include <iostream>
int main()
{
WIN32_FIND_DATA data;
HANDLE hFind = FindFirstFile("C:\\semester2", &data); // DIRECTORY
if ( hFind != INVALID_HANDLE_VALUE ) {
do {
std::cout << data.cFileName << std::endl;
} while (FindNextFile(hFind, &data));
FindClose(hFind);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
semester2
Run Code Online (Sandbox Code Playgroud) 从python的运行时效率角度来看,这些效率是否相等?
x = foo()
x = bar(x)
Run Code Online (Sandbox Code Playgroud)
VS
x = bar(foo())
Run Code Online (Sandbox Code Playgroud)
我有一个更复杂的问题,基本上可以归结为这个问题:显然,从代码长度的角度来看,第二个更为有效,但运行时是否也更好?如果不是,为什么不呢?
我现在正在使用Python 3.5解释器,发现了非常有趣的行为:
>>> (1,2,3,"a",*("oi", "oi")*3)
(1, 2, 3, 'a', 'oi', 'oi', 'oi', 'oi', 'oi', 'oi')
>>> [1,2,3,"a",*range(10)]
[1, 2, 3, 'a', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> ('aw','aw',*range(10),*(x**2 for x in range(10)))
('aw', 'aw', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 4, 9, 16, 25, 36, 49, 64, 81)
>>> {"trali":"vali", **dict(q=1,p=2)}
{'q': 1, 'p': 2, 'trali': 'vali'}
>>> {"a",1,11,*range(5)}
{0, 1, 2, 3, 4, 11, 'a'}
Run Code Online (Sandbox Code Playgroud)
尽管我有多年的Python经验,但我从未在文档和示例中以及任何源代码中看到过这种情况.我发现它非常有用. …
我刚刚在Python 3.5中做了一些随机的事情.有15分钟的业余时间,我想出了这个:
a = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
"x", "y", "z"}
len_a = len(a)
list = list(range(0, len_a))
message = ""
wordlist = [ch for ch in message]
len_wl = len(wordlist)
for x in list:
print (a[x])
Run Code Online (Sandbox Code Playgroud)
但令人满意的随机成功的感觉并没有超过我.相反,失败的感觉:
Traceback (most recent call last):
File "/Users/spathen/PycharmProjects/soapy/soup.py", line 9, in <module>
print (a[x])
TypeError: 'set' object does not support indexing
Run Code Online (Sandbox Code Playgroud)
请帮忙
python ×6
python-3.x ×5
c++ ×3
dictionary ×2
arrays ×1
byte ×1
c++11 ×1
dictview ×1
exception ×1
iterator ×1
map-function ×1
optimization ×1
pep448 ×1
performance ×1
python-2.7 ×1
raii ×1
rounding ×1
set ×1
throw ×1
unique-ptr ×1
winapi ×1
windows ×1