try:
data=open('info.txt')
for each_line in data:
try:
(role,line_spoken)=each_line.split(':',1)
print(role,end='')
print(' said: ',end='')
print(line_spoken,end='')
except ValueError:
print(each_line)
data.close()
except IOError:
print("File is missing")
Run Code Online (Sandbox Code Playgroud)
当逐行打印文件时,代码往往会在前面添加三个不必要的字符,即"".
实际产量:
Man said: Is this the right room for an argument?
Other Man said: I've told you once.
Man said: No you haven't!
Other Man said: Yes I have.
Run Code Online (Sandbox Code Playgroud)
预期产量:
Man said: Is this the right room for an argument?
Other Man said: I've told you once.
Man said: No you haven't!
Other Man said: Yes I …Run Code Online (Sandbox Code Playgroud) 所以我用三种不同的方法写下了用于评估多项式的代码.霍纳的方法应该是最快的,而天真的方法应该是最慢的,对吗?但是,为什么计算它的时间不是我所期望的呢?对于itera和naive方法,计算时间有时会变得完全相同.它出什么问题了?
import numpy.random as npr
import time
def Horner(c,x):
p=0
for i in c[-1::-1]:
p = p*x+i
return p
def naive(c,x):
n = len(c)
p = 0
for i in range(len(c)):
p += c[i]*x**i
return p
def itera(c,x):
p = 0
xi = 1
for i in range(len(c)):
p += c[i]*xi
xi *= x
return p
c=npr.uniform(size=(500,1))
x=-1.34
start_time=time.time()
print Horner(c,x)
print time.time()-start_time
start_time=time.time()
print itera(c,x)
print time.time()-start_time
start_time=time.time()
print naive(c,x)
print time.time()-start_time
Run Code Online (Sandbox Code Playgroud)
以下是一些结果:
[ 2.58646959e+69]
0.00699996948242
[ 2.58646959e+69]
0.00600004196167 …Run Code Online (Sandbox Code Playgroud) <T extends Drawable & DrawerToggle> ActionBarDrawerToggle(
Activity activity,
Toolbar toolbar,
DrawerLayout drawerLayout,
T slider,
@StringRes int openDrawerContentDescRes,
@StringRes int closeDrawerContentDescRes) {
Run Code Online (Sandbox Code Playgroud)
在浏览ActionBarDrawerToggle.java类的源代码期间,我发现这个构造函数是在没有访问说明符的情况下声明的.相反,它的声明始于
<T extends Drawable & DrawerToggle>
Run Code Online (Sandbox Code Playgroud)
请解释一下,它究竟意味着什么?
用过的:
typings install jquery --global
typings ERR! message Unable to find "jquery" ("npm") in the registry. Did you want to try searching another source? Also, if you want contribute these typings, please help us: https://github.com/typings/registry
typings ERR! caused by https://api.typings.org/entries/npm/jquery/versions/latest responded with 404, expected it to equal 200
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我.
有人请告诉我以下使用sealed不编译的原因吗?然而,如果我更换sealed与final和编译如Java,它的工作原理.
private sealed int compInt = 100;
public bool check(int someInt)
{
if (someInt > compInt)
{
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud) 我到了ImportError : no module named 'requests'.
但是我已经requests使用该命令安装了软件包pip install requests.
在pip freeze命令提示符下运行命令时,结果为
requests==2.7.0
Run Code Online (Sandbox Code Playgroud)
那么为什么在运行python文件时会发生这种错误呢?
如果我有一个静态局部变量或thread_local局部变量,它在一个内联函数中,在不同的翻译单元中定义,在最终的程序中它们是否由标准保证具有相同的地址?
// TU1:
inline int* f() { static int x; return &x; }
extern int* a;
void sa() { a = f(); }
// TU2:
inline int* f() { static int x; return &x; }
extern int* b;
void sb() { b = f(); }
// TU3:
int *a, *b;
void sa();
void sb();
int main() { sa(); sb(); return a == b; }
Run Code Online (Sandbox Code Playgroud)
以上总是会返回1吗?
我有一个ASP MVC 3网站,我们正试图在动作链接中加入一些样式.
我希望html是类似的<a href="/somepath/someaction"><span class="someclass">some text</span> some more text</a>但我无法弄清楚如何告诉Razor <span>正确渲染它.
到目前为止我尝试过的:
@Html.ActionLink("<span class="someclass">some text</span> some more text", SomeAction, SomeController);
Run Code Online (Sandbox Code Playgroud)
导致链接看起来像: <span class="someclass">some text</span> some more text
@Html.ActionLink("<text><span class="someclass">some text</span></text> some more text", SomeAction, SomeController);
Run Code Online (Sandbox Code Playgroud)
导致链接看起来像: <text><span class="someclass">some text</span></text> some more text
@Html.ActionLink(<text>"<span class="someclass">some text</span> some more text"</text>, SomeAction, SomeController);
Run Code Online (Sandbox Code Playgroud)
导致编译错误.
思考?
我是python的新手,在地图上工作减少了百果馅的问题.运行mincemeat脚本时出现以下错误.
$python mincemeat.py -p changeme localhost
error: uncaptured python exception, closing channel <__main__.Client connected at 0x923fdcc>
(<type 'exceptions.NameError'>:global name 're' is not defined
[/usr/lib/python2.7/asyncore.py|read|79]
[/usr/lib/python2.7/asyncore.py|handle_read_event|438]
[/usr/lib/python2.7/asynchat.py|handle_read|140]
[mincemeat.py|found_terminator|96]
[mincemeat.py|process_command|194]
[mincemeat.py|call_mapfn|170]
[raw1.py|mapfn|43])
Run Code Online (Sandbox Code Playgroud)
我的代码位于raw1.py脚本中,该脚本在上面的stacktrace中给出[raw1.py|mapfn|43].
import re
import mincemeat
# ...
allStopWords = {'about':1, 'above':1, 'after':1, 'again':1}
def mapfn(fname, fcont):
# ...
for item in tList[1].split():
word = re.sub(r'[^\w]', ' ', item).lower().strip() # ERROR
if (word not in allStopWords) and (len(word) > 1):
# ....
Run Code Online (Sandbox Code Playgroud)
我已经re在raw1.py中导入了.如果我re在mincemeat.py中导入,则不会出现该错误.
我有一个后台应用程序,需要每1.5秒向另一个进程发送一个keep-alive.在OSX 10.7和10.8中一切都运行顺畅,但在OSX 10.9下,许多保持活动通知都会丢失,有时会达到3次.通常情况下,前3或4分钟一切正常,然后问题就开始发生了.
经过进一步检查,似乎OSX Mavericks"定时器合并"功能将负责决定将请求的1.5秒延长到4.0秒.
有没有办法在NSThread中表明不合并?或者至少表明允许的最大合并变化?
请参阅以下代码以供参考:
+(void)keepAliveThread
{
@autoreleasepool {
void (^keepAlive)() = ^ (){
// (snipped!) do something...
};
dispatch_queue_t mainQueue = dispatch_get_main_queue();
while( [NSThread currentThread].isCancelled == NO )
{
@autoreleasepool {
dispatch_async(mainQueue, keepAlive);
[NSThread sleepForTimeInterval:1.5];
}
}
}
}
Run Code Online (Sandbox Code Playgroud)