在字符串中匹配'('的正则表达式是什么?
以下是场景:
我有一个字符串
str = "abc(efg)";
Run Code Online (Sandbox Code Playgroud)
我想'('用正则表达式拆分字符串.对于我正在使用的
Arrays.asList(Pattern.compile("/(").split(str))
Run Code Online (Sandbox Code Playgroud)
但我得到以下例外.
java.util.regex.PatternSyntaxException: Unclosed group near index 2
/(
Run Code Online (Sandbox Code Playgroud)
逃避'('似乎不起作用.
我尝试运行以下使用 python 3.2 的程序,出现错误:'module' object has no attribute 'atoi' 谁能告诉我我该怎么做才能解决这个问题?对此,我真的非常感激 !
import string
def converttoint(str):
try:
value = string.atoi(str)
return value
except ValueError:
return None
Run Code Online (Sandbox Code Playgroud) 在尝试回答有关is关键字使用的问题时,我发现这段代码:
脚本:
a = 123456
b = 123456
print a is b # True
Run Code Online (Sandbox Code Playgroud)
互动模式:
>>> a = 123456
>>> b = 123456
>>> a is b
False
Run Code Online (Sandbox Code Playgroud)
在Python交互模式下以及从脚本运行时提供不同的输出.
从这个答案:
当前实现为-5到256之间的所有整数保留一个整数对象数组,当您在该范围内创建一个int时,实际上只返回对现有对象的引用.
所以,我希望只a is b返回True范围内的整数[-5, 256].但它只适用于交互模式,而不是从脚本运行时.
问题:为什么a is b在交互模式下以及何时从脚本运行时表现不同?
注意:在Python 2.7和Python 3中测试
目前,我已经制作了这段代码
def grid_maker(h,w):
grid = [[["-"] for i in range(w)] for i in range(h)]
grid[0][0] = ["o"]
print grid
>>>grid_maker(3,5) => [[['o'], ['-'], ['-'], ['-'], ['-']], [['-'], ['-'], ['-'], ['-'], ['-']], [['-'], ['-'], ['-'], ['-'], ['-']]]
Run Code Online (Sandbox Code Playgroud)
我想制作另一个函数,它将接收生成的2d数组并将其打印出来,使其采用以下格式:
o----
-----
-----
Run Code Online (Sandbox Code Playgroud)
但是,我不确定从哪里开始.
为什么说在java中使用枚举更好.如果你有不同数据类型的常量,我认为最好使用带常量的类而不是枚举?
喜欢
Class A {
public static int A = 1;
public static int B = 2;
public static String c = "RADIUS";
public static String d = "LENGTH";
}
Run Code Online (Sandbox Code Playgroud)
代替
enum ofInts {A(1), B(2)}
enum ofStrings{c("RADUIS"), d("LENGTH")}
Run Code Online (Sandbox Code Playgroud) 所以我刚刚开始学习python并为项目创建一个刽子手游戏.我被卡住了.我给你一些背景知识.
我得到了程序来摆脱字母表中的字母,并将它们添加到正在猜测的单词的空白处,但它只会找到第一个字母的索引.所以我要说的是,我试图猜测这个词是安全的.现在让我说我猜字母f.它返回f _ _ _ _ _ _ _而不是f _ _ _ _ _ f _.在我看来,一旦找到列表中的第一个字母实例并在那里打破,for循环就会停止.我需要查找并显示该字母的所有实例.
码:
def makechoice(list)
# defines the word trying to be guessed as a list of letters
Global listword
#defines the amount of blanks in listword as a list "_ "
global blanks
#user input to guess a letter
current = raw_input("Please enter your guess:")
for a in listword:
if a == current:
t = listword.index(a)
#puts the letter and a blank in place of …Run Code Online (Sandbox Code Playgroud) 我的代码:
int a = 1;
int b = 6;
int c = 5;
double x1 = (-b + Math.sqrt(b^2 - 4*a*c))/(2*a);
double x2 = (-b - Math.sqrt(b^2 - 4*a*c))/(2*a);
System.out.println("x=" + x1 + x2 );
Run Code Online (Sandbox Code Playgroud)
输出:
x=NaNNan
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用String数组实现ArrayList.
实现我在我的日食中得到删除类型参数错误.
ArrayList.java
import java.util.List;
public class ArrayList {
public static void main(String [] a)
{
String [] things = {"eggs","chicken","milk","butter"};
List<String> list1 = new ArrayList<String>();
for(String s: things)
list1.add(s);
String [] morethings = {"chicken","milk"};
List<String> list2 = (List<String>) new ArrayList ();
for(String y: morethings)
list2.add(y);
for(int i=0; i<list1.size();i++)
{
System.out.printf("%s ", list1.get(i));
}
}
}
Run Code Online (Sandbox Code Playgroud) 我从服务器上得到这个ips:
"/177.127.101.68:53964"
"/201.80.15.100:54263"
"/177.67.38.54:51309"
Run Code Online (Sandbox Code Playgroud)
我需要它像只是"177.127.101.68",我打算删除最后5个字符串字符,但有时它来"/186.213.186.40:4625"所以我不知道到底该怎么做...是吗任何方式这样做?