English 中文(简体)
利用 p[闭门]从矩阵中提取数值
原标题:Extracting pair of values from matrix using python [closed]
  • 时间:2011-08-24 17:54:02
  •  标签:
  • python

我的矩阵如下。

           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

.....

人人都能告诉我如何这样做。

预 收

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)

生产:

  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

该代码假定矩阵的x和 y指数相同(如你的例子)。 否则,你需要编制两个不同的关键清单。

<>说明: 我的测试.csv文件只是一份文本文件,我复印了你的矩阵(实际上不是文件)。 如果你有 com子分离的价值观,那么你就应当相应修改分法。

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签