我最近从Python 2.7切换到Python 3.3,似乎在Python 2中,字典键的排序是任意的但是一致的,在Python 3中,用例如获得的字典的键的排序vars()看起来是非确定性的.
如果我跑:
class Test(object): pass
parameters = vars(Test)
print(list(parameters.keys()))
Run Code Online (Sandbox Code Playgroud)
在Python 2.7和Python 3.3中,然后:
Python 2.7一直给我
['__dict__', '__module__', '__weakref__', '__doc__']
Run Code Online (Sandbox Code Playgroud)使用Python 3.3,我可以得到任何随机顺序 - 例如:
['__weakref__', '__module__', '__qualname__', '__doc__', '__dict__']
['__doc__', '__dict__', '__qualname__', '__module__', '__weakref__']
['__dict__', '__module__', '__qualname__', '__weakref__', '__doc__']
['__weakref__', '__doc__', '__qualname__', '__dict__', '__module__']
Run Code Online (Sandbox Code Playgroud)这种非决定论来自哪里?为什么会这样
list({str(i): i for i in range(10)}.keys())
Run Code Online (Sandbox Code Playgroud)
......在跑步之间保持一致,总是给予
['3', '2', '1', '0', '7', '6', '5', '4', '9', '8']
Run Code Online (Sandbox Code Playgroud)
......?
Python 2.x有一个很棒的函数叫做dateutil.parser,它将ISO8601格式的日期转换为python日期时间值.它在Python 3中不存在.有什么替代品?
我熟悉使用enumerate():
>>> seq_flat = ('A', 'B', 'C')
>>> for num, entry in enumerate(seq_flat):
print num, entry
0 A
1 B
2 C
Run Code Online (Sandbox Code Playgroud)
我希望能够为嵌套列表做同样的事情:
>>> seq_nested = (('A', 'Apple'), ('B', 'Boat'), ('C', 'Cat'))
Run Code Online (Sandbox Code Playgroud)
我可以打开包装:
>>> for letter, word in seq_nested:
print letter, word
A Apple
B Boat
C Cat
Run Code Online (Sandbox Code Playgroud)
我应该如何解压缩以获得以下信息?
0 A Apple
1 B Boat
2 C Cat
Run Code Online (Sandbox Code Playgroud)
我知道的唯一方法是使用计数器/增量器,据我所知,这是非Pythonic.有更优雅的方式吗?
Python标准库中是否有任何内容可以正确解析/解析字符串以便在shell命令中使用?我正在寻找perl的python模拟String::ShellQuote::shell_quote:
$ print String::ShellQuote::shell_quote("hello", "stack", "overflow's", "quite", "cool")
hello stack 'overflow'\''s' quite cool
Run Code Online (Sandbox Code Playgroud)
而且,更重要的是,它会在相反的方向上起作用(取一个字符串并将其分解为一个列表).
我正常的搜索foo让我失望.我正在尝试找到一个R函数,它返回整数的所有因子.至少有2个包含factorize()函数的包:gmp和conf.design,但这些函数只返回素因子.我想要一个能够返回所有因子的函数.
显然,搜索这个很困难,因为R有一个叫做因子的结构,它会在搜索中产生很多噪音.
我想用Mayavi.
virtualenv安装了.但我没有用它来安装.我手动安装了它的所有依赖项,包括VTK.
因为VTK,我按照其安装指南并安装了python包装器.
现在,如果我打开一个新的终端窗口并打开python,我可以导入vtk而不会出现任何错误.
我尝试使用以下命令安装Mayavi:
pip install mayavi
Run Code Online (Sandbox Code Playgroud)
但是发生以下错误:
Downloading/unpacking mayavi
Running setup.py egg_info for package mayavi
build_src
building extension "tvtk.array_ext" sources
building data_files sources
build_src: building npy-pkg config files
no previously-included directories found matching 'artwork'
no previously-included directories found matching 'docs/pdf'
Requirement already satisfied (use --upgrade to upgrade): apptools in /Library/Python/2.7/site-packages (from mayavi)
Requirement already satisfied (use --upgrade to upgrade): traits in /Library/Python/2.7/site-packages …Run Code Online (Sandbox Code Playgroud) 我想f(int x) { return x == 0 ? 0 : 1; }用Java 实现.
在C中,我只是" return !!x;",但!在Java中并不像那样.有没有办法在没有条件的情况下做到这一点?没有像展开版本那样俗气的东西
int ret = 0;
for (int i = 0; i < 32; i++) {
ret |= ((x & (1 << i)) >>> i);
}
Run Code Online (Sandbox Code Playgroud)
要么
try {
return x/x;
} catch (ArithmeticException e) {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
)
编辑:
所以,我做了三个不同解决方案的微基准测试:
随机int输入的时间(整个int范围)是:
1. 0.268716
2. 0.324449
3. 0.347852
Run Code Online (Sandbox Code Playgroud)
是的,我的愚蠢的x/x解决方案的速度非常快.当您考虑到其中只有很少的0时,并不是很令人惊讶,并且在绝大多数情况下都会采用快速路径.
50%输入为0的更有趣案例的时间安排: …
在我使用的几个编译器(gcc除了各种版本之外的所有版本)中,我得到一个C99 mode错误,例如int i在for循环表达式中而不是在它之前声明(如果我不使用该std=c99选项).在这里阅读之后,我理解gcc选项-ansi,-std=c89并且-std=iso9899:1990都评估为ANSI C标准,但我不明白为什么/如果我应该选择c89标准而不是更新的标准c99(这是我假设的最新标准).
此外,我看到isoC语言的类型标准的多个版本,其中第一个(根据我的理解)是ANSI标准的直接端口. 是否可以肯定地说iso将更新他们的C标准,但C的原始ANSI标准将始终是相同的?
奖金问题:
我实际上可以自己弄清楚这一点,我还没有花时间去做,所以如果有人知道他们的头顶那么这很好,否则没什么大不了的,我以后会搞清楚的:)
我有一本相当新的书The C Programming Language (ANSI).我的书总是显示像这样的循环:
int i;
for(i = 0; i < foo; i++)
Run Code Online (Sandbox Code Playgroud)
但很多人(他们的小指中有大多数编程才能)都会像这样编写for循环:
(int i = 0; i < foo; i++)
Run Code Online (Sandbox Code Playgroud)
如果我以第一种方式编写循环然后i应该可以访问整个函数是正确的,但是如果我以第二种方式编写它,那么i只能访问for循环REGARDLESS我编译的标准是什么?另一种问同样问题的方法,如果我使用c89标准进行编译,那么ifor循环的两个for循环都可以被整个函数访问,如果我使用c99标准进行编译,i那么整个函数可以访问第一个for循环i.第二个for循环只能通过for循环访问?
对于nodejs有没有像python或其他语言一样的stdout flush?
sys.stdout.write('some data')
sys.stdout.flush()
Run Code Online (Sandbox Code Playgroud)
现在我只看到process.stdout.write()了nodejs.
目的base64.b64encode()是将二进制数据转换为ASCII安全"文本".但是,该方法返回一个bytes类型的对象:
>>> import base64
>>> base64.b64encode(b'abc')
b'YWJj'
Run Code Online (Sandbox Code Playgroud)
简单地接受输出就很容易decode(),但我的问题是:base64.b64encode()返回的意义是什么,bytes而不是str?