是否有一种"好的"方法来消除列表元素的连续重复?
例:
["red"; "red"; "blue"; "green"; "green"; "red"; "red"; "yellow"; "white"; "white"; "red"; "white"; "white"]
Run Code Online (Sandbox Code Playgroud)
应该成为
["red"; "blue"; "green"; "red"; "yellow"; "white"; "red"; "white"]
Run Code Online (Sandbox Code Playgroud)
- "好"我的意思是对新用户和快速执行最具可读性和可理解性:)
"goatse运算符"或=()=
Perl中的习语导致表达式在列表上下文中进行评估.
一个例子是:
my $str = "5 and 4 and a 3 and 2 1 BLAST OFF!!!";
my $count =()= $str =~ /\d/g; # 5 matches...
print "There are $count numbers in your countdown...\n\n";
Run Code Online (Sandbox Code Playgroud)
当我解释使用时,会发生以下情况:
$str =~ /\d/g
匹配所有数字.所述g
开关和列表环境产生的那些匹配的列表.让它成为"List Producer"的例子,在Perl中这可能是很多东西.=()=
原因,分配到一个空列表,所以所有的实际匹配被复制到一个空列表.=()=
标量赋值后,空列表的引用计数变为零.然后Perl删除列表元素的副本.关于效率的问题是:
这个琐碎的列表很有用,但是如果列表是成千上万的匹配怎么办?使用此方法,您将生成每个匹配的完整副本,然后将其删除以计算它们.
与Windows,GNOME和大多数其他GUI不同,如果关闭该应用程序的主窗口(或所有窗口),则OS X应用程序不会全部终止.
例如,启动Firefox,Safari,Word或大多数基于文档的应用程序.单击角落中的红点或键入cmdW以关闭窗口.您可以看到该程序的菜单仍处于活动状态,程序仍在运行.对于OS X新手,有时候你会看到几十个无窗口的僵尸在运行,他们想知道为什么他们的计算机变慢了.
对于一些基于文档的程序,如果没有窗口,有一些意义就是不终止应用程序.例如,使用Safari或Word,您仍然可以CmdN为应用程序的任何设计键入并获取新的文档窗口:浏览Web(Safari)或键入新文档(Word).
Apple在这方面融入了他们的设计理念.一些关闭在最后一个窗口关闭,一些不关闭.第三方应用程序更加复杂.
单击其红色关闭按钮时,还有其他应用程序关闭.系统首选项,词典,Mac App Store,iPhoto和Calculator会在单一或最后一个窗口关闭时终止.iCal,地址簿,iTunes,DVD播放器不会终止.
我觉得特别恼人的是没有逻辑"新文档"或"打开"功能的应用程序,但是当文档窗口关闭时它们不会终止.示例:启动iTunes或通讯簿并终止主窗口.除了手动选择"退出"之外,还有一个没有窗口且没有功能的僵尸.
最后一个窗口关闭后很容易关闭应用程序.Cocoa甚至会告诉你该事件的通知.只需将此添加到您的应用程序委托:
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
return YES;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:在最后一个窗口关闭后,我有什么理由不应该终止我的申请吗?为什么在OS X软件上如此变化?除非应用程序有"新"或"开放"或其他一些明确理解的理由不终止没有窗口打开,否则终止失败对我来说似乎是个错误.
在Python中,对于二进制文件,我可以这样写:
buf_size=1024*64 # this is an important size...
with open(file, "rb") as f:
while True:
data=f.read(buf_size)
if not data: break
# deal with the data....
Run Code Online (Sandbox Code Playgroud)
有了我想逐行阅读的文本文件,我可以这样写:
with open(file, "r") as file:
for line in file:
# deal with each line....
Run Code Online (Sandbox Code Playgroud)
这是简写:
with open(file, "r") as file:
for line in iter(file.readline, ""):
# deal with each line....
Run Code Online (Sandbox Code Playgroud)
这个成语记录在PEP 234中,但我找不到二进制文件的类似习惯用法.
我试过这个:
>>> with open('dups.txt','rb') as f:
... for chunk in iter(f.read,''):
... i+=1
>>> i
1 # 30 MB file, …
Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的例子:
#!/usr/bin/env python
#a() # 1: NameError: name 'a' is not defined
#b() # 1: NameError: name 'b' is not defined
#c() # 1: NameError: name 'c' is not defined
def a():
c() # note the forward use here...
#a() #2: NameError: global name 'c' is not defined
#b() #2: NameError: name 'b' is not defined
#c() #2: NameError: name 'c' is not defined
def b():
a()
#a() #3: NameError: global name 'c' is not defined
#b() #3: NameError: global …
Run Code Online (Sandbox Code Playgroud) 我对Python 中这个用于循环遍历大词列表的算法的小例子感兴趣.我正在写一些"工具",这将允许我以与Python类似的方式切片Objective-C字符串或数组.
具体来说,这个优雅的解决方案引起了我的注意,非常快速地执行,它使用字符串切片作为算法的关键元素.尝试解决这个没有切片!
我使用下面的Moby单词列表复制了我的本地版本./usr/share/dict/words
如果您不想下载Moby,可以使用.源只是一个类似于字典的大型独特单词列表.
#!/usr/bin/env python
count=0
words = set(line.strip() for line in
open("/Users/andrew/Downloads/Moby/mwords/354984si.ngl"))
for w in words:
even, odd = w[::2], w[1::2]
if even in words and odd in words:
count+=1
print count
Run Code Online (Sandbox Code Playgroud)
这个脚本将a)由Python解释; b)读取4.1 MB,354,983字的Moby字典文件; c)剥去线条; d)将线条放入一组,并且; e)并找到所有组合,其中平均值和给定单词的几率也是单词.这在MacBook Pro上执行约0.73秒.
我试图在Objective-C中重写相同的程序.我是这种语言的初学者,所以请放轻松,但请指出错误.
#import <Foundation/Foundation.h>
NSString *sliceString(NSString *inString, NSUInteger start, NSUInteger stop,
NSUInteger step){
NSUInteger strLength = [inString length];
if(stop > strLength) {
stop = strLength;
}
if(start > strLength) { …
Run Code Online (Sandbox Code Playgroud) 我用谷歌搜索过,我已经测试了,这让我在我的智慧结束时.我有一个我需要按相似性分组的数字列表.例如,在[1,6,9,10,110,105,109,134,139]的列表中,1 6 9将被放入列表中,100,102,105和109将被放入列表,134和139.我在数学方面很糟糕,我已经尝试过并试过这个,但我无法让它发挥作用.为了尽可能明确,我希望将10个值之间的数字组合在一起.有人可以帮忙吗?谢谢.
要刷新,C和Perl中的条件运算符是:
(test) ? (if test was true) : (if test was false)
Run Code Online (Sandbox Code Playgroud)
如果与左值一起使用,您可以使用一个动作进行分配和测试:
my $x= $n==0 ? "n is 0" : "n is not 0";
Run Code Online (Sandbox Code Playgroud)
我正在阅读伊戈尔·奥斯特罗夫斯基(Igor Ostrovsky)的博客,以一种简洁的方式来表达基于C语言的多语句if语句,并且意识到这在Perl中确实是一种"巧妙的方式".
例如:(编辑:使用Jonathan Leffler更易读的形式......)
# ternary conditional form of if / elsif construct:
my $s=
$n == 0 ? "$n ain't squawt"
: $n == 1 ? "$n is not a lot"
: $n < 100 ? "$n is more than 1..."
: $n < 1000 ? "$n …
Run Code Online (Sandbox Code Playgroud) 我正在学习python,我只是想知道我是否有办法编写一个类似的代码:
def f(x):
if x>1:
return(x)
else:
# don't return anything
Run Code Online (Sandbox Code Playgroud)
我问的是代码的其他部分.如果x<=1
返回None
是不可接受的,我不需要返回任何东西.
python ×5
perl ×2
c# ×1
cocoa ×1
conditional ×1
file ×1
function ×1
goatse ×1
idioms ×1
iteration ×1
iterator ×1
list ×1
macos ×1
namespaces ×1
nsstring ×1
numbers ×1
objective-c ×1
operators ×1
performance ×1
syntax ×1