dict.items()
和之间是否有任何适用的差异dict.iteritems()
?
从Python文档:
dict.items()
:返回字典的(键,值)对列表的副本.
dict.iteritems()
:在字典(键,值)对上返回一个迭代器.
如果我运行下面的代码,每个似乎都返回对同一对象的引用.我缺少哪些微妙的差异?
#!/usr/bin/python
d={1:'one',2:'two',3:'three'}
print 'd.items():'
for k,v in d.items():
if d[k] is v: print '\tthey are the same object'
else: print '\tthey are different'
print 'd.iteritems():'
for k,v in d.iteritems():
if d[k] is v: print '\tthey are the same object'
else: print '\tthey are different'
Run Code Online (Sandbox Code Playgroud)
输出:
d.items():
they are the same object
they are the same object
they are the same object
d.iteritems():
they are the same …
Run Code Online (Sandbox Code Playgroud) 在Python shell中,如果我输入列表解析,例如:
>>> [x for x in string.letters if x in [y for y in "BigMan on campus"]]
Run Code Online (Sandbox Code Playgroud)
我得到了一个很好的印刷结果:
['a', 'c', 'g', 'i', 'm', 'n', 'o', 'p', 's', 'u', 'B', 'M']
Run Code Online (Sandbox Code Playgroud)
字典理解相同:
>>> {x:x*2 for x in range(1,10)}
{1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}
Run Code Online (Sandbox Code Playgroud)
如果我输入一个生成器表达式,我得不到这么友好的响应:
>>> (x for x in string.letters if x in (y for y in "BigMan on campus"))
<generator object <genexpr> at 0x1004a0be0>
Run Code Online (Sandbox Code Playgroud)
我知道我可以这样做:
>>> …
Run Code Online (Sandbox Code Playgroud) 我在工作中学习Perl并享受它.我通常用Python做我的工作,但老板想要Perl.
Python和Perl中的大多数概念都很匹配:Python dictionary = Perl hash; Python元组= Perl列表; Python list = Perl数组; 等等
问题:是否有迭代器 /生成器的Python形式的Perl版本?
示例:生成Fibonacci数字的经典Python方法是:
#!/usr/bin/python
def fibonacci(mag):
a, b = 0, 1
while a<=10**mag:
yield a
a, b = b, a+b
for number in fibonacci(15):
print "%17d" % number
Run Code Online (Sandbox Code Playgroud)
如果要根据需要生成更大列表的子部分,迭代器也很有用.Perl'列表'似乎更加静态 - 更像是Python元组.在Perl中,可以foreach
是动态的还是仅基于静态列表?
Iterator的Python形式是我已经习惯的形式,我没有在Perl中找到它...除了在循环或递归中编写它或生成一个巨大的静态列表,我如何(为ex)写它在Perl中的Fibonacci子程序?是否有yield
我错过的Perl ?
具体来说 - 我该怎么写:
#!/usr/bin/perl
use warnings; use strict; # yes -- i use those!
sub fibonacci {
# What goes here other than returning an array or …
Run Code Online (Sandbox Code Playgroud) 我有这个非常大的CSV文件(15 Gb),我需要读取大约100万条随机行.据我所知 - 并实现 - Python中的CSV实用程序只允许在文件中按顺序迭代.
将所有文件读入内存以使用一些随机选择是非常耗费内存的,并且通过所有文件并丢弃一些值并选择其他文件非常耗时,因此,无论如何都要从CSV文件中选择一些随机行并且只读那行?
我尝试没有成功:
import csv
with open('linear_e_LAN2A_F_0_435keV.csv') as file:
reader = csv.reader(file)
print reader[someRandomInteger]
Run Code Online (Sandbox Code Playgroud)
CSV文件的示例:
331.093,329.735
251.188,249.994
374.468,373.782
295.643,295.159
83.9058,0
380.709,116.221
352.238,351.891
183.809,182.615
257.277,201.302
61.4598,40.7106
Run Code Online (Sandbox Code Playgroud) Python 2.x允许比较异构类型.
一个有用的快捷方式(在Python 2.7中)None
比较小于任何整数或浮点值:
>>> None < float('-inf') < -sys.maxint * 2l < -sys.maxint
True
Run Code Online (Sandbox Code Playgroud)
在Python 2.7中,空元组()
是一个无限值:
>>> () > float('inf') > sys.maxint
True
Run Code Online (Sandbox Code Playgroud)
当人们可能对int和float的混合列表进行排序并希望具有绝对最小值和最大值时,此快捷方式非常有用.
这个快捷方式已经在Python 3000中删除了(这是Python 3.2):
>>> None < 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: NoneType() < int()
Run Code Online (Sandbox Code Playgroud)
此外,Python3000 已经删除 sys.maxint
了理论上所有的内容促进了多头并且限制不再适用.
PEP 326,一个顶部和底部值的案例,在Python中提高了参考最小值和最大值.新的排序行为记录在案.
由于PEP 326被拒绝了,在Python 2X和Python 3000上使用整数和浮点数和长整数的最小值和最大值有用,可用的定义是什么?
编辑
有几个答案是"只使用maxv = float('inf')"......我想的原因,无论多么遥远的可能性,是这样的:
>>> float(2**5000)
Traceback (most recent call last):
File "<stdin>", line …
Run Code Online (Sandbox Code Playgroud) 假设我MUSICAL SYMBOL G CLEF
在Objective-C源文件中的字符串文字中有符号:****.
OS X字符查看器表示CLEF 在其术语中是UTF8 F0 9D 84 9E
和Unicode 1D11E(D834+DD1E)
.
经过一些困难,并使用ICU UNICODE演示页面,我确实得到以下代码:
NSString *uni=@"\U0001d11e";
NSString *uni2=[[NSString alloc] initWithUTF8String:"\xF0\x9D\x84\x9E"];
NSString *uni3=@"";
NSLog(@"unicode: %@ and %@ and %@",uni, uni2, uni3);
Run Code Online (Sandbox Code Playgroud)
我的问题:
@"\U0001d11e
UTF-32吗?在Python 2中,一个常见的(旧的,遗留的)习惯用于使用map
如下形式连接不均匀长度的迭代器map(None,iter,iter,...)
:
>>> map(None,xrange(5),xrange(10,12))
[(0, 10), (1, 11), (2, None), (3, None), (4, None)]
Run Code Online (Sandbox Code Playgroud)
在Python 2中,它被扩展,以便最长的迭代器是返回列表的长度,如果一个比另一个短,则用它填充None
.
在Python 3中,这是不同的.首先,你不能None
用作位置1中可调用的参数:
>>> list(map(None, range(5),range(10,12)))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
Run Code Online (Sandbox Code Playgroud)
好的 - 我可以解决这个问题:
>>> def f(*x): return x
...
>>> list(map(f, *(range(5),range(10,12))))
[(0, 10), (1, 11)]
Run Code Online (Sandbox Code Playgroud)
但现在,我有一个不同的问题:map
返回最短迭代器的长度 - 不再填充None
.
当我将Python 2代码移植到Python 3时,这不是一个非常罕见的习惯用法,我还没有找到一个简单易用的解决方案.
不幸的是,2to3的工具,不挑这件事-帮倒忙提示:
-map(None,xrange(5),xrange(10,18)) …
Run Code Online (Sandbox Code Playgroud) 可以使用以下类别在Objective C中扩展类:
@interface NSString (CategoryName)
-(NSString *)myFabulousAddition; // a fabulous additional method
@end
/////////////////////////////
@implementation NSString (CategoryName)
-(NSString *)myFabulousAddition {
// do something fabulous...
}
@end
Run Code Online (Sandbox Code Playgroud)
在这个小例子中,我将把方法添加myFabulousAddition
到NSString中.然后我就可以调用它,[anNSString myFabulousAddition]
就像它是NSString方法集的一部分一样.很棒,很有用.
您可以添加到类中的类别数量没有限制,但每个类别名称必须不同,并且每个类别都应声明并定义一组不同的方法.
如果你有这样的事情怎么办:
@interface NSString (CategoryName)
-(NSString *)myFabulousAddition; // a fabulous additional method
@end
@interface NSString (ANOTHERCategoryName)
-(NSString *)myFabulousAddition; // a DIFFERENT fabulous additional method
// BUT with same name as the other category
@end
/////////////////////////////
@implementation NSString (CategoryName)
-(NSString *)myFabulousAddition {
// do …
Run Code Online (Sandbox Code Playgroud) 在目录中运行所有Python文件的最佳方法是什么?
python *.py
Run Code Online (Sandbox Code Playgroud)
只执行一个文件.在shell脚本(或make文件)中为每个文件写一行似乎很麻烦.我需要这个b/c我有一系列小的matplotlib脚本,每个脚本创建一个png文件,并希望一次创建所有图像.
PS:我正在使用bash shell.
假设我有一个由列表列表组成的矩阵,如下所示:
>>> LoL=[list(range(10)) for i in range(10)]
>>> LoL
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, …
Run Code Online (Sandbox Code Playgroud) python ×8
python-3.x ×3
objective-c ×2
bash ×1
csv ×1
dictionary ×1
file ×1
iterator ×1
map-function ×1
matplotlib ×1
matrix ×1
perl ×1
python-2.x ×1
random ×1
unicode ×1
xcode ×1