开发者

Extracting pair of values from matrix using python [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I have matrix as follows..

           31348   439352   6077    4619722    60825
31348      1        0.304   0.126    0.12      0.162
439352     0.304    1       0.101    0.095     0.316
6077       0.126    0.101   1        0.473     0.219
4619722    0.12     0.095   0.473    1         0.256
60825      0.162    0.316   0.219    0.256     1

Now i have to write python script to extract pairs which are having >0.2 the result should 开发者_运维百科be as follows

439352, 31348  0.304
60825, 439352  0.316

.....

Can anybody tell me how to do this..

Thanks in advance

NI


mylist = []

with open('test.csv') as f:
    keys = f.readline()
    keys = keys.split()

    for line in f:
        a = line.split()
        mylist.append(a[1:])

for idx1, item in enumerate(mylist):
    for idx2, number in enumerate(item):
        if float(number) > 0.2:
            print "%7s, %7s  --> %7s" %(keys[idx1], keys[idx2], number)

Produces:

  31348,   31348  -->       1
  31348,  439352  -->   0.304
 439352,   31348  -->   0.304
 439352,  439352  -->       1
 439352,   60825  -->   0.316
   6077,    6077  -->       1
   6077, 4619722  -->   0.473
   6077,   60825  -->   0.219
4619722,    6077  -->   0.473
4619722, 4619722  -->       1
4619722,   60825  -->   0.256
  60825,  439352  -->   0.316
  60825,    6077  -->   0.219
  60825, 4619722  -->   0.256
  60825,   60825  -->       1

This code suposes x and y indexes of the matrix are the same (as in your example). Otherwise you need to prepare two different keys lists.

Note: My test.csv file is just a text file where I copy-pasted your matrix (so actually it is not a .csv file). If you have comma separated values you should modify the split methods accordingly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜