小编jul*_*ria的帖子

相同的正则表达式在Java和Python中有不同的结果

Java代码:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExpTest {
    public static void main(String[] args) {

        String str = "X-Value = -0.525108, Y-Value = 7.746691, Z-Value = 5.863008, Timestamp(milliseconds) = 23001";
        String p = "Value = (.*?), ";
        Pattern pattern = Pattern.compile(p);
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()){
             System.out.println(matcher.group(1));
             System.out.println(matcher.group(2));
             System.out.println(matcher.group(3));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Java代码的输出:

$ java RegExpTest 
-0.525108
Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 2
        at java.util.regex.Matcher.group(Matcher.java:487)
        at RegExpTest.main(RegExpTest.java:15)
$ 
Run Code Online (Sandbox Code Playgroud)

Python代码(在Interpreter中):

>>> import re
>>> re.findall("Value = (.*?), ", …
Run Code Online (Sandbox Code Playgroud)

python java regex

0
推荐指数
1
解决办法
1728
查看次数

从列表中删除元素

我有一个变量:

x = 4
Run Code Online (Sandbox Code Playgroud)

我有一个清单:

list = [{'name': u'A', 'value': '1'}, {'name': u'B', 'value': '4'}, {'name': u'C', 'value': '2'}]
Run Code Online (Sandbox Code Playgroud)

如何排除/删除列表中value = x的元素?

python list-comprehension filter

0
推荐指数
1
解决办法
154
查看次数

java中各种形式的声明之间的区别

我是Java中的菜鸟,是在python之后学习Java的人.无论如何,我很难搞清楚这一点.假设我有班级

class Bicycle{
      ....
 }
Run Code Online (Sandbox Code Playgroud)

 public class Bicycle{
    ....}
Run Code Online (Sandbox Code Playgroud)

有什么不同.那怎么样?

  public static class Bicycle{
              // if this can be a valid class def in first place
   }
Run Code Online (Sandbox Code Playgroud)

然后,在此之后..让我们谈谈变量.

    class Bicycle{
     int Gear or public int Gear // whats the difference
    }
Run Code Online (Sandbox Code Playgroud)

什么时候使用哪一个?

java variable-declaration

0
推荐指数
1
解决办法
99
查看次数

查找列表中的项目

所以我试图在python的列表中找到一个项目.这是我的功能:

def operator(input):
    operatorlist = ['+', '-', '*', '/', '^', 'sin', 'cos']

    for i in operatorlist:
        if input is operatorlist[i]:
            return True
Run Code Online (Sandbox Code Playgroud)

我的代码破了,我无法弄清楚为什么......任何想法?

我改变了我的代码:

def operator(input):
    if input is '+' or input is '-' or input is '*' or input is '/' or input is '^' or input is 'sin' or input is 'cos':
    return True
Run Code Online (Sandbox Code Playgroud)

因为我被告知,基本上,风格上是愚蠢的,以这种方式写它.

python parsing list

0
推荐指数
1
解决办法
229
查看次数

如何确保第一个随机值总是大于第二个?

码:

def Division():

    print "************************\n""********DIVISION********\n""************************"
    counter = 0
    import random
    x = random.randint(1,10)
    y = random.randint(1,10)
    answer = x/y
    print "What will be the result of " + str(x) + '/' + str(y) + " ?"
    print "\n"
    userAnswer = input ("Enter result: ")
    if userAnswer == answer:
        print ("Well done!")
        print "\n"
        userInput = raw_input ("Do you want to continue? \n Enter 'y' for yes or 'n' for no.")
        if userInput == "y":
            print "\n"
            Division()
        else:
            print "\n" …
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
126
查看次数

如何在纸质,摇滚,剪刀游戏中获胜?

循环完成后如何计算胜利.

print 'PLAY ROCK PAPER SCISSORS'

for roundd in range(1,6):
    print 'Round: ' + str(roundd)
    human = raw_input('Whats your draw: ')
    from random import choice
    cpu = choice(('rock', 'paper', 'scissors'))

    if human == cpu:
        print 'CPU: ' + cpu
        print 'Draw'

    if cpu == 'rock' and human == 'scissors':
        print 'CPU: ' + cpu
        print 'You Lose'
    if cpu == 'scissors'and human == 'paper':
        print 'CPU: ' + cpu
        print 'You Lose'
    if cpu == 'paper'and human == 'rock':
        print …
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
2159
查看次数

如何从字符串中提取子字符串?

假设我有一个字符串,/Apath1/Bpath2/Cpath3/0-1-2-3-4-5-something.otherthing我只想提取'0-1-2-3-4-5'部分.我试过这个:

str='/Apath1/Bpath2/Cpath3/0-1-2-3-4-5-something.otherhing'

print str[str.find("-")-1:str.find("-")]
Run Code Online (Sandbox Code Playgroud)

但是,结果只有0.如何提取'0-1-2-3-4-5'部分?

python

0
推荐指数
1
解决办法
129
查看次数

以下代码中的错误是什么?

这是我的Django模型

class author(models.Model):
    name = models.CharField('name',max_length=140)
    datamode = models.CharField(max_length=1, default='A', choices=DATAMODE_CHOICE)

    def __str__(self):
        return '%s' % (self.name)
Run Code Online (Sandbox Code Playgroud)

虽然我author在另一个类中使用此类作为外键,如下所示:

class books(models.Model):
    NEW_FLAG=(('N','New'),('O','Old'))
    name = models.CharField('name',max_length=140)
    author = models.ForeignKey(author, blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)

当我在管理员中输入新书详细信息时,它显示:

IntegrityError: Column 'author_id' cannot be null.
Run Code Online (Sandbox Code Playgroud)

python django django-models django-admin

-1
推荐指数
1
解决办法
298
查看次数

Python期望一个缩进块

我是Python的新手,想根据几何分布生成一些数字.我发现这个代码在互联网上但不起作用:

  import random
from math import ceil, log

def geometric(p):

# p should be in (0.0, 1.0].
if ((p <= 0.0) or (p >=1.0)):
raise ValueError("p must be in the interval (0.0, 1.0]")
elif p == 1.0:
# If p is exactly 1.0, then the only possible generated value is 1.
# Recognizing this case early means that we can avoid a log(0.0) later.
# The exact floating point comparison should be fine. log(eps) works just
# dandy.
return 1 …
Run Code Online (Sandbox Code Playgroud)

python indentation

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

健康的编程实践

我需要建议.我正在写一个计算器; 我已经掌握了它的基本功能,并最终转向使用它的处理功能.在这方面,我发现Python提供的标准数学函数库不令人满意.

虽然定义了更常用的函数,但一些较为模糊的函数缺乏适当的提及.这方面的例子包括弧双曲余切,弧双曲余割,双曲余割,割线等.虽然其中一些只是对所提供功能的操纵,但似乎更好的选择是提供我们的定义并将程序引用到它遇到了他们.

这是我的问题所在.我应该在主脚本中定义这些函数,还是应该使用单独的文件?如果我是后者,我该如何引用单独的文件?我对Python比较陌生,所以如果我缺少常用技术或其他东西,请告诉我.提前致谢.

python math module function

-1
推荐指数
1
解决办法
1286
查看次数

Python内部

任何人都可以指导我在线文档或书籍,我可以在其中找到并了解C语言中的Python实现,例如Perl的 这个:http://perldoc.perl.org/index-internals.html或本书:扩展和Simon Cozen嵌入Perl.

python

-1
推荐指数
1
解决办法
261
查看次数

如何使用正则表达式获取html

#<link rel='canonical' href='http://www.samplewebsite.com/image/5434553/' />

#I am trying to grab the text in href

image = str(Soup)

image_re = re.compile('\<link rel=\'cononical\' href=')

image_pat = re.findall(image_re, image)

print image_pa

#>> []

#Thanks!
Run Code Online (Sandbox Code Playgroud)

python regex

-2
推荐指数
1
解决办法
890
查看次数

将PHP数组转换为Python字典格式的字符串

如何将PHP多维数组转换为Python字典格式的字符串?

var_dump($myarray);

array(2) { ["a1"]=> array(2) { ["29b"]=> string(0) "" ["29a"]=> string(0) "" } ["a2"]=> array(2) { ["29b"]=> string(0) "" ["29a"]=> string(0) "" } }
Run Code Online (Sandbox Code Playgroud)

php python arrays dictionary function

-2
推荐指数
1
解决办法
5623
查看次数