小编Non*_*one的帖子

测试列表是否包含另一个Python列表

如何测试列表是否包含另一个列表(即,它是一个连续的子序列).假设有一个名为contains的函数:

contains([1,2], [-1, 0, 1, 2]) # Returns [2, 3] (contains returns [start, end])
contains([1,3], [-1, 0, 1, 2]) # Returns False
contains([1, 2], [[1, 2], 3]) # Returns False
contains([[1, 2]], [[1, 2], 3]) # Returns [0, 0]
Run Code Online (Sandbox Code Playgroud)

编辑:

contains([2, 1], [-1, 0, 1, 2]) # Returns False
contains([-1, 1, 2], [-1, 0, 1, 2]) # Returns False
contains([0, 1, 2], [-1, 0, 1, 2]) # Returns [1, 3]
Run Code Online (Sandbox Code Playgroud)

python contains list list-comparison

43
推荐指数
6
解决办法
7万
查看次数

如果try语句在python中成功运行代码

我想知道在python中是否有一种简单的方法来运行代码,如果try语句是成功的,而不是在try语句本身.这是其他或最终的命令(我不明白他们的文档)?我知道我可以使用这样的代码:

successful = False
try:
    #code that might fail
    successful = True
except:
    #error handling if code failed
if successful:
    #code to run if try was successful that isn't part of try
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有更短的路.

python error-handling try-catch

28
推荐指数
2
解决办法
2万
查看次数

自动调整大小和滚动的Java JTextArea

我在JPanel中有一个JTextArea.我怎样才能让JTextArea填充整个JPanel并在JPanel调整大小时调整大小并在输入太多文本时滚动?

java swing jpanel jtextarea autoresize

13
推荐指数
2
解决办法
3万
查看次数

分配给命名组的Python正则表达式

当你在python正则表达式中使用变量(是正确的单词吗?)时:"blah(?P\w +)"("value"将是变量),你怎么能让变量的值成为"之后的文本" blah"到行尾或某个字符没有注意变量的实际内容.例如,这是我想要的伪代码:

>>> import re
>>> p = re.compile("say (?P<value>continue_until_text_after_assignment_is_recognized) endsay")
>>> m = p.match("say Hello hi yo endsay")
>>> m.group('value')
'Hello hi yo'
Run Code Online (Sandbox Code Playgroud)

注意:标题可能不易理解.那是因为我不知道怎么说.对不起,如果我引起任何混淆.

python regex variables variable-assignment

11
推荐指数
2
解决办法
1万
查看次数

Quartz Composer条码扫描器

是否有可以在Leopard(10.5)上运行的Quartz Composer条形码扫描仪插件?我需要一些可以扫描书背上找到的条形码的东西; 我见过使用libdmtx的DataMatrix插件,它可以扫描其他类型的条形码.

plugins barcode-scanner quartz-composer

11
推荐指数
1
解决办法
517
查看次数

在C之前用于OS开发的语言

我知道C是操作系统开发的标准编程语言,但出于好奇,我想知道它之前是什么.C之前用于操作系统开发的主要编程语言是什么?

c history operating-system

9
推荐指数
4
解决办法
1331
查看次数

打印后执行下一条指令的Python尾随逗号

如果将尾随逗号添加到print语句的末尾,则首先执行下一个语句.为什么是这样?例如,10000 ** 10000它在打印之前执行"Hi ":

print "Hi",
print 10000 ** 10000
Run Code Online (Sandbox Code Playgroud)

打印"嗨你好"之前需要一段时间:

def sayHello():
    for i in [0] * 100000000: pass
    print "Hello"
print "Hi",
sayHello()
Run Code Online (Sandbox Code Playgroud)

python text

8
推荐指数
2
解决办法
8917
查看次数

GCC无法找到GMP,MPFR和MPC库

我试图在Mac OS 10.5.7上交叉编译GCC.我在安装GMP,MPFR和MPC后使用此命令配置GCC:

../gcc-4.5.0/configure --target=$i586-elf --prefix=/usr/local/cross \
    --disable-nls \
    --enable-languages=c,c++,fortran,java,objc,obj-c++,treelang,ada \
    --without-headers --with-libiconv-prefix=/opt/local --with-gmp=/usr/local \
    --with-mpfr=/usr/local --with-mpc=/usr/local
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误:

checking for the correct version of gmp.h... buggy but acceptable
checking for the correct version of mpfr.h... yes
checking for the correct version of mpc.h... yes
checking for the correct version of the gmp/mpfr/mpc libraries... no
configure: error: Building GCC requires GMP 4.2+, MPFR 2.3.1+ and MPC 0.8.0+.
Try the --with-gmp, --with-mpfr and/or --with-mpc options to specify
their locations.  Source code for …
Run Code Online (Sandbox Code Playgroud)

configuration gcc makefile mpfr gmp

7
推荐指数
2
解决办法
3万
查看次数

Cython的计算不正确

我实现了Madhava-Leibniz系列来计算Python中的pi,然后在Cython中提高速度.Python版本:

from __future__ import division
pi = 0
l = 1
x = True
while True:
    if x:
        pi += 4/l
    else:
        pi -= 4/l
    x = not x
    l += 2
    print str(pi)
Run Code Online (Sandbox Code Playgroud)

Cython版本:

cdef float pi = 0.0
cdef float l = 1.0
cdef unsigned short x = True
while True:
    if x:
        pi += 4.0/l
    else:
        pi -= 4.0/l
    x = not x
    l += 2
    print str(pi)
Run Code Online (Sandbox Code Playgroud)

当我停止Python版本时,它已正确计算pi为3.141592.Cython版本最终以3.141597结束,有一些我不记得的数字(我的终端崩溃),但是不正确.为什么Cython版本的计算不正确?

python pi cython

7
推荐指数
1
解决办法
1417
查看次数

针对Arduino/AVR的编程语言

除了默认的C++环境之外,哪些编程语言或环境以Arduino或AVR为目标?
PS:我正在使用Mac OS 10.5.

programming-languages avr arduino target

5
推荐指数
2
解决办法
4755
查看次数