我想在[1,n]范围内生成2n-1个随机整数,每个元素出现两次,除了随机值,只出现一次.
例如:
n = 3
seq = [1, 2, 3, 1, 3]
Run Code Online (Sandbox Code Playgroud)
在这个例子中,2只出现一次.
我的算法是使用字典,如下所示:
------------- | num |times| | 1 | 2 | | 2 | 1 | | 3 | 2 |
其中键从1到n,值表示键的出现次数.我用两个值填充字典,并将一个随机密钥的值减小为1.
我读了一个只包含一行的文件.但是,在循环结束之前,我无法停止读取文件.即python不会抛出EOFError异常.我的代码有什么问题?
for x in range(5):
try:
line = file.readlines()
except EOFError:
break
print "Line:",line
Run Code Online (Sandbox Code Playgroud)
输出是:
Line: ['nice\n']
Line: []
Line: []
Line: []
Line: []
Run Code Online (Sandbox Code Playgroud) 在我的例子中,插入到lucene索引中的每个文档都有其唯一的ID.在lucene索引中添加新文档时,如果文档已存在于索引中,则不应将该文档插入到索引中.如何实施这一战略?
我想我应该首先使用docId搜索文档,如果lucene找不到文档,那么我插入它.但是,因为我有3个线程共享唯一的indexWriter索引,我想应该有一些错误的情况.例如:线程1和线程2正在处理具有相同docId的两个文档,如果thread1搜索docId并且什么都没找到,它会将文档插入到索引中,但是thread2可以在thread1读取索引之后将其文档插入索引.结果,索引中存在两个文档.我怎么能避免这个?
例如,文档根目录virtualHost是/var/www,服务器名称是aaa.com.但是,apache会告诉我forbidden是否使用localhost和访问服务器aaa.com.如果我将Directory选项更改http.conf为/var/www,则apache将运行良好.我不知道为什么?
我想Directory在每一个中设置选项httpd-vhosts.conf,而不是在httpd.conf我如何做到这一点?
这是我的http.conf:

这是我的httpd-vhosts.conf:

文件内容如下:
##this is the comment
but this is not comment '####', LOL # this is a normal comment
Run Code Online (Sandbox Code Playgroud)
我只是想过滤所有评论并获得过滤后的内容,但我不知道如何避免过滤不是评论的英镑符号.
fileLines = [line.strip() for line in file if '#' != line[0] ]
Run Code Online (Sandbox Code Playgroud)
这段代码只是可以过滤注释符号,这是一行的第一个字符.
我想要的结果就像下面一行:
but this is not comment '####', LOL
Run Code Online (Sandbox Code Playgroud) 我想得到一个数字列表,如10,100,1000,10000 ...
我可以使用以下代码通过Java执行此操作:
for (int i = 10;i<100001;i*=10){
.....
}
Run Code Online (Sandbox Code Playgroud)
我如何通过Python实现这一点?
for i in range(what?)
Run Code Online (Sandbox Code Playgroud) 在Java中,我可以这样使用:
for (index=candate*2;index<=topCandidate;index+=candidate)
Run Code Online (Sandbox Code Playgroud)
我想知道如何用Python做到这一点?
我用matcher实现了replaceAll()方法,用""替换所有标点符号.但它总是抛出异常:" java.lang.StringIndexOutOfBoundsException:String index out of range:6 "
private static StringBuilder filterPunctuation(StringBuilder sb){
Pattern pattern = Pattern.compile("(\\.)");
Matcher matcher = pattern.matcher(sb);
while(matcher.find()){
sb.replace(matcher.start(), matcher.end(), "");
// if sb.replace(matcher.start(),matcher.end()," "), it wil be right, but I want replace all punction with ""
}
return sb;
}
public static void main(String[] args){
System.out.println(filterPunctuation(new StringBuilder("test.,.")));
}
Run Code Online (Sandbox Code Playgroud)