0

I have a file with 200000 lines,the third column is velocity which should be sorted.I am just giving first 20 lines

second.txt

0 0 1.200000
0 1 1.200000
0 2 1.200000
0 3 1.200000
0 4 1.200000
0 5 1.200000
0 6 1.200000
0 7 1.200000
0 8 1.200000
0 9 1.200000
0 10 1.200000
0 11 1.200000
0 12 1.200000
0 13 1.200000
0 14 1.200000
0 15 1.200000
0 16 1.200000
0 17 1.200000
0 18 1.200000
0 19 1.200000

Array which should be used as a reference

newindex1.txt 

0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 0
18 0
19 0

What I want is to take third column value and place it to appropriate newindex1.txt index pair.

Desired output

    0 0 1.200000
    1 0 1.200000
    2 0 1.200000
    3 0 1.200000
    4 0 1.200000
    5 0 1.200000
    6 0 1.200000
    7 0 1.200000
    8 0 1.200000
    9 0 1.200000
    10 0 1.200000
    11 0 1.200000
    12 0 1.200000
    13 0 1.200000
    14 0 1.200000
    15 0 1.200000
    16 0 1.200000
    17 0 1.200000
    18 0 1.200000
    19 0 1.200000

All examples are just 20 lines,real files are huge.

I have tried this

import numpy as np
from numpy import loadtxt
from operator import itemgetter

s = loadtxt("second.txt", delimiter=" ", unpack=False)
ni = loadtxt("newindex1.txt", delimiter=" ", unpack=False)

for i, l in enumerate(ni):
    for m,n,v in s:
        s[m,n,v] == ni[i,l]

It does not work

    s[m,n,v] == ni[i,l]
IndexError: too many indices

Any ideas how to solve this?

6
  • 2
    Can you show an example of the output you are trying to achieve? Commented May 12, 2016 at 19:36
  • s is only 2 dimensional, but you're trying to index it with 3 indices (m,n,v). What are you trying to do in the last line? Not to mention the fact that the way you are looping, m,n, and v are the entries in s, none of them are indices (same goes for l). The only index in all of your code is i. Commented May 12, 2016 at 19:44
  • @Racialz Take a look at edited output,pls! Commented May 12, 2016 at 19:54
  • @brettb How to solve this?I want to extract value from one array and allocate it to proper index of the second.I have showed the desired output. Commented May 12, 2016 at 19:56
  • The problem is still underspecified. What you initially say you want to do and later say are different. Here is the key question: can some kind of sort of second.txt give you what you want (without any reference to another file), or is it necessary to base the output on another file (in this case, newindex1.txt)? Commented May 12, 2016 at 20:06

1 Answer 1

1

In case the newindex1.txt is sorted by an unknown rule, I think you might use a dict to get a velocity by an index ():

import numpy as np
from numpy import loadtxt
from operator import itemgetter

s = loadtxt("second.txt", delimiter=" ", unpack=False)
ni = loadtxt("newindex1.txt", delimiter=" ", unpack=False)

index = {}

for m,n,v in s:
    index [(m,n)] = v

for i,l in ni:
    key = (i,l)

    if key in index.keys ():
        print (i, l, index [key])
Sign up to request clarification or add additional context in comments.

1 Comment

Try to use pairs instead of string keys. See updated code above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.