文本文件编辑和行写入

时间:2018-04-17 20:30:31

标签: python text-files

我的代码有点问题。

首先是代码:

In [397]: np.random.seed(0)
     ...: data = np.random.randint(0,100,(500,500))
     ...: x_0 = 50
     ...: y_0 = 50

In [398]: %timeit func(data, (x_0,y_0))
     ...: %timeit func_return_inverse(data, (x_0,y_0))
     ...: %timeit func_mask_vectorized(data, (x_0,y_0))
1 loop, best of 3: 5.62 s per loop
10 loops, best of 3: 20.7 ms per loop
100 loops, best of 3: 4.28 ms per loop

In [399]: np.random.seed(0)
     ...: data = np.random.randint(0,1000,(500,500))
     ...: x_0 = 500
     ...: y_0 = 500

In [400]: %timeit func(data, (x_0,y_0))
     ...: %timeit func_return_inverse(data, (x_0,y_0))
     ...: %timeit func_mask_vectorized(data, (x_0,y_0))
1 loop, best of 3: 6.87 s per loop
10 loops, best of 3: 21.9 ms per loop
100 loops, best of 3: 5.05 ms per loop

问题:

读取.txt中的某些特定行,但我不能在特定行上写下测量后的状态,

我收到了这个错误:

package com.test;

class MyThread implements Runnable{

    @Override
    public void run() {
        int i=1;
        while(true) {
            String name=Thread.currentThread().getName();
            if(name.equals("task1") && i%2!=0) {
                System.out.println(name+"::::"+i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else if(name.equals("task2") && i%2==0){
                System.out.println(name+"::::"+i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            i++;
        }

    }

    public static void main(String[] args) {

        MyThread task1=new MyThread();
        MyThread task2=new MyThread();

        Thread t1=new Thread(task1,"task1");
        Thread t2=new Thread(task2,"task2");

        t1.start();
        t2.start();

    }

}

希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

当您尝试从该字符串访问某个项目时,您会获得index out of range。这意味着您尝试访问一个项目,在您的情况下是一个字符,其中索引(ip)大于res或小于0的长度。