这里简单的问题(也许不是一个简单的答案?)
说我有一个二维数组
[0] [1] [2]
[3] [4] [5]
[6] [7] [8]
Run Code Online (Sandbox Code Playgroud)
现在假设我想获得数字6的位置
我知道使用一维数组我可以使用Array.indexOf()但是我的选择是什么?二维数组?
谢谢!
如何获取列表中一个实例的索引?
class Points {
int x, y;
Point(this.x, this.y);
}
void main() {
var pts = new List();
int Lx;
int Ly;
Points pt = new Points(25,55); // new instance
pts.add(pt);
int index = pts.indexOf(25); // Problem !!! How to obtain the index in a list of instances ?
if (index != -1 ){
Lx = lp1.elementAt(index).x;
Ly = lp1.elementAt(index).y;
print('X=$Lx Y=$Ly');
}
Run Code Online (Sandbox Code Playgroud) 当我在我的导航器中使用以下URL" http://www.tuto3d.netai.net/wordpress/ "时,我通常会访问我的wordpress网站,但是当我使用 http://www.tuto3d.netai.net/时,我得到了而不是文件夹的索引和wordpress文件夹的链接.我想将我的.htaccess http://www.tuto3d.netai.net/重定向到" http://www.tuto3d.netai.net/wordpress/ "
这是我的.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
# END WordPress
Run Code Online (Sandbox Code Playgroud)
先感谢您
我正在寻找一种方法来搜索字符串以获得完全匹配或完整的单词匹配. RegEx.Match并且RegEx.IsMatch似乎没有让我在哪里,我想.
请考虑以下情形:
namespace test
{
class Program
{
static void Main(string[] args)
{
string str = "SUBTOTAL 34.37 TAX TOTAL 37.43";
int indx = str.IndexOf("TOTAL");
string amount = str.Substring(indx + "TOTAL".Length, 10);
string strAmount = Regex.Replace(amount, "[^.0-9]", "");
Console.WriteLine(strAmount);
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面代码的输出是:
// 34.37
// Press any key to continue...
Run Code Online (Sandbox Code Playgroud)
问题是,我不想SUBTOTAL的,但IndexOf发现这个词第一次出现TOTAL是在SUBTOTAL然后得到34.37的不正确的值.
所以问题是,有没有办法强制IndexOf只找到一个完全匹配或是否有另一种方法来强制完全匹配的单词,以便我可以找到该完全匹配的索引,然后用它执行一些有用的功能.RegEx.IsMatch并且RegEx.Match是,据我所知道的,只是boolean搜索.在这种情况下,仅知道存在完全匹配是不够的.我需要知道它在字符串中的位置.
任何意见,将不胜感激.
我想编写非常好看的惯用Scala代码,list indexOf foo getOrElse Int.MaxValue但现在我必须满足于寻找愚蠢的Java代码val result = list indexOf foo; if (result < 0) Int.MaxValue else result.是否有充分的理由indexOf在Scala中返回Int而不是Option[Int]
虽然find(["a", "b"], "c")没有问题,但在尝试查找结构数组中的结构索引时出错:
struct Score
{
//...
}
var scores: [Score] = //...
var score: Score = //...
find(self.scores, score) // Error: Cannot invoke 'find' with an argument list of type '([Score], Score)'
Run Code Online (Sandbox Code Playgroud)
我可能是一个问题,结构默认情况下无法相互比较.但改变Scores定义class
给我同样的错误.
如何在保持函数单行代码的同时从结果中删除括号?
day_list = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
def day_to_number(inp):
return [day for day in range(len(day_list)) if day_list[day] == inp]
print day_to_number("Sunday")
print day_to_number("Monday")
print day_to_number("Tuesday")
print day_to_number("Wednesday")
print day_to_number("Thursday")
print day_to_number("Friday")
print day_to_number("Saturday")
Run Code Online (Sandbox Code Playgroud)
输出:
[0]
[1]
[2]
[3]
[4]
[5]
[6]
Run Code Online (Sandbox Code Playgroud) 我有一个字节数组,希望找到一些字节的"出现".
例如,00 69 73 6F 6D在一个非常大的字节数组(> 50/100兆字节)
要么
更好的反向操作:在不知道代码的情况下搜索最常见的模式,代码应该能够从文件中读取并找到它.
所以我试图让我的程序从文本文件中打印出每个单词和标点符号的索引(当它出现时)。我已经完成了那部分。- 但问题是当我尝试使用这些索引位置重新创建带有标点符号的原始文本时。这是我的代码:
with open('newfiles.txt') as f:
s = f.read()
import re
#Splitting string into a list using regex and a capturing group:
matches = [x.strip() for x in re.split("([a-zA-Z]+)", s) if x not in ['',' ']]
print (matches)
d = {}
i = 1
list_with_positions = []
# the dictionary entries:
for match in matches:
if match not in d.keys():
d[match] = i
i+=1
list_with_positions.append(d[match])
print (list_with_positions)
file = open("newfiletwo.txt","w")
file.write (''.join(str(e) for e in list_with_positions))
file.close()
file = open("newfilethree.txt","w") …Run Code Online (Sandbox Code Playgroud) String xStr = "hello/master/yoda";
String remv_last = xStr.substring(0, xStr.lastIndexOf("/"))
System.out.println(remv_last);
Run Code Online (Sandbox Code Playgroud)
输出
hello/master
Run Code Online (Sandbox Code Playgroud)
我的问题是;我怎样才能得到这个输出,谢谢你的帮助。
master/yoda
Run Code Online (Sandbox Code Playgroud)