473,686 Members | 3,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to access the individual elements of a matrix in python

111 New Member
my file is of the form

01 "\t" 10.19 "\t" 0.00 "\t" 10.65
02 "\t" 11.19 "\t" 10.12 "\t" 99.99


and i need to access the individual floating point numbers from it!
say for ex. the first no is 10.19.. i want to access this and add one to it.
Expand|Select|Wrap|Line Numbers
  1. filename=open("half.transfac","r")
  2. file_content=filename.readlines()
  3. sam=""
  4. for line in file_content:
  5.     for char in line:
  6.         if char=="\tchar\t\n":
  7.             sam+=char
  8.             print sam
  9.  
for char accesss every digit and not the numbers{"10.19" ,"0.00")etc. . how do i do this..help
Jul 5 '07 #1
60 4701
dshimer
136 Recognized Expert New Member
There are powerful ways to do this all in one line, but by way of explanation, start by using split() to separate each line into individual data lists for further manipulation.

Expand|Select|Wrap|Line Numbers
  1. >>> for line in file_content:
  2. ...     line.split()
  3. ... 
  4. ['01', '10.19', '0.00', '10.65']
  5. ['02', '11.19', '10.12', '99.99']
Each of which could be appended to an empty list, forming a multi-dimensional data set. Note that all the elements are strings and would have to be converted to numbers before the math.
Expand|Select|Wrap|Line Numbers
  1. >>> datalist=[]
  2. >>> for line in file_content:
  3. ...     datalist.append(line.split())
  4. ... 
  5. >>> datalist
  6. [['01', '10.19', '0.00', '10.65'], ['02', '11.19', '10.12', '99.99']]
  7. >>> int(datalist[0][0])
  8. 1
  9. >>> float(datalist[0][1])
  10. 10.19
  11.  

my file is of the form

01 "\t" 10.19 "\t" 0.00 "\t" 10.65
02 "\t" 11.19 "\t" 10.12 "\t" 99.99


and i need to access the individual floating point numbers from it!
say for ex. the first no is 10.19.. i want to access this and add one to it.
Expand|Select|Wrap|Line Numbers
  1. filename=open("half.transfac","r")
  2. file_content=filename.readlines()
  3. sam=""
  4. for line in file_content:
  5.     for char in line:
  6.         if char=="\tchar\t\n":
  7.             sam+=char
  8.             print sam
  9.  
for char accesss every digit and not the numbers{"10.19" ,"0.00")etc. . how do i do this..help
Jul 5 '07 #2
bvdet
2,851 Recognized Expert Moderator Specialist
my file is of the form

01 "\t" 10.19 "\t" 0.00 "\t" 10.65
02 "\t" 11.19 "\t" 10.12 "\t" 99.99


and i need to access the individual floating point numbers from it!
say for ex. the first no is 10.19.. i want to access this and add one to it.
Expand|Select|Wrap|Line Numbers
  1. filename=open("half.transfac","r")
  2. file_content=filename.readlines()
  3. sam=""
  4. for line in file_content:
  5.     for char in line:
  6.         if char=="\tchar\t\n":
  7.             sam+=char
  8.             print sam
  9.  
for char accesss every digit and not the numbers{"10.19" ,"0.00")etc. . how do i do this..help
Here is another way to access the numbers from a dictionary:
Expand|Select|Wrap|Line Numbers
  1. >>> lineList = open(file_name).readlines()
  2. >>> dataDict = {}
  3. >>> for line in lineList:
  4. ...     line = line.split()
  5. ...     dataDict[line[0]] = [float(r) for r in line[1:]]
  6. ...     
  7. >>> dataDict
  8. {'02': [11.19, 10.119999999999999, 99.989999999999995], '01': [10.19, 0.0, 10.65]}
  9. >>> dataDict['01'][0]
  10. 10.19
  11. >>> dataDict['02'][2]
  12. 99.989999999999995
You can perform mathematical operations on elements of the dictionary:
Expand|Select|Wrap|Line Numbers
  1. >>> dataDict
  2. {'02': [11.19, 10.119999999999999, 99.989999999999995], '01': [10.19, 0.0, 10.65]}
  3. >>> dataDict['01'][0] += 1
  4. >>> dataDict
  5. {'02': [11.19, 10.119999999999999, 99.989999999999995], '01': [11.19, 0.0, 10.65]}
  6. >>> 
To access individual characters:
Expand|Select|Wrap|Line Numbers
  1. >>> for key in dataDict:
  2. ...     for item in dataDict[key]:
  3. ...         print ' '.join([ch for ch in '%0.2f' % item])
  4. ...         
  5. 1 1 . 1 9
  6. 1 0 . 1 2
  7. 9 9 . 9 9
  8. 1 0 . 1 9
  9. 0 . 0 0
  10. 1 0 . 6 5
OR
Expand|Select|Wrap|Line Numbers
  1. >>> [ch for ch in '%0.2f' % dataDict['01'][0]]
  2. ['1', '1', '.', '1', '9']
Jul 5 '07 #3
aboxylica
111 New Member
THis is my file! AS u can see the first column gives the position,the second third ..etc are the part of the matrix.my problem here is that.I have to write a code to access the individual values.like if i say A[01] i should have the value 0.00 or say A[06] i should have a value 3.46. or C[04]=3.67 etc. and i have to add one to each of these elements.can you gimme the code. my code is not working.

NA bap

PO A C G T

01 0.00 3.67 0.00 0.00

02 0.00 0.00 3.67 0.00

03 0.00 0.00 0.00 3.67

04 0.00 3.67 0.00 0.00

05 3.67 0.00 0.00 0.00

06 3.46 0.00 0.22 0.00

07 0.00 0.00 3.67 0.00

08 0.00 0.00 0.00 3.67

09 0.00 0.00 0.00 3.67

10 0.00 3.67 0.00 0.00

11 3.67 0.00 0.00 0.00

12 3.67 0.00 0.00 0.00

13 0.00 0.00 3.67 0.00

14 0.00 0.00 0.00 3.67

15 0.00 0.00 3.67 0.00

16 0.00 3.67 0.00 0.00

//

//

NA bcd
Jul 6 '07 #4
bartonc
6,596 Recognized Expert Expert
Files operate much like lists, so I alway practice with a list, then go to a file:
Here's one way to look at such data in python:
Expand|Select|Wrap|Line Numbers
  1. rawdata = \
  2. ['01 0.00 3.67 0.00 0.00',
  3.  
  4. '02 0.00 0.00 3.67 0.00',
  5.  
  6. '03 0.00 0.00 0.00 3.67',
  7.  
  8. '04 0.00 3.67 0.00 0.00',
  9.  
  10. '05 3.67 0.00 0.00 0.00',
  11.  
  12. '06 3.46 0.00 0.22 0.00',
  13.  
  14. '07 0.00 0.00 3.67 0.00',
  15.  
  16. '08 0.00 0.00 0.00 3.67',
  17.  
  18. '09 0.00 0.00 0.00 3.67',
  19.  
  20. '10 0.00 3.67 0.00 0.00',
  21.  
  22. '11 3.67 0.00 0.00 0.00',
  23.  
  24. '12 3.67 0.00 0.00 0.00',
  25.  
  26. '13 0.00 0.00 3.67 0.00',
  27.  
  28. '14 0.00 0.00 0.00 3.67',
  29.  
  30. '15 0.00 0.00 3.67 0.00']
  31.  
  32. datadictionary = {} # usually shorten the name to dd
  33.  
  34. for line in rawdata:
  35.     items = line.split()
  36.     key = items[0]
  37.     datadictionary[key] = [float(item) for item in items[1:]]
  38.  
  39. print datadictionary['09'][3]
Jul 6 '07 #5
bartonc
6,596 Recognized Expert Expert
<Moderator NOTE: Merged threads by OP on a single topic (after posting to the second)>
Jul 6 '07 #6
aboxylica
111 New Member
i have to consider the columns also:
like..this is my file
A T G C
01 1.00 2.00 3.00 4.00
02 2.00 3.00 4.00 5.00

now if i say A[01] i should have a value 1.00 or C[01]=4.00
those column names A,T,G,C. i am not able to format my query file properly hope it is understood













Files operate much like lists, so I alway practice with a list, then go to a file:
Here's one way to look at such data in python:
Expand|Select|Wrap|Line Numbers
  1. rawdata = \
  2. ['01 0.00 3.67 0.00 0.00',
  3.  
  4. '02 0.00 0.00 3.67 0.00',
  5.  
  6. '03 0.00 0.00 0.00 3.67',
  7.  
  8. '04 0.00 3.67 0.00 0.00',
  9.  
  10. '05 3.67 0.00 0.00 0.00',
  11.  
  12. '06 3.46 0.00 0.22 0.00',
  13.  
  14. '07 0.00 0.00 3.67 0.00',
  15.  
  16. '08 0.00 0.00 0.00 3.67',
  17.  
  18. '09 0.00 0.00 0.00 3.67',
  19.  
  20. '10 0.00 3.67 0.00 0.00',
  21.  
  22. '11 3.67 0.00 0.00 0.00',
  23.  
  24. '12 3.67 0.00 0.00 0.00',
  25.  
  26. '13 0.00 0.00 3.67 0.00',
  27.  
  28. '14 0.00 0.00 0.00 3.67',
  29.  
  30. '15 0.00 0.00 3.67 0.00']
  31.  
  32. datadictionary = {} # usually shorten the name to dd
  33.  
  34. for line in rawdata:
  35.     items = line.split()
  36.     key = items[0]
  37.     datadictionary[key] = [float(item) for item in items[1:]]
  38.  
  39. print datadictionary['09'][3]
Jul 6 '07 #7
bartonc
6,596 Recognized Expert Expert
i have to consider the columns also:
like..this is my file
A T G C
01 1.00 2.00 3.00 4.00
02 2.00 3.00 4.00 5.00

now if i say A[01] i should have a value 1.00 or C[01]=4.00
those column names A,T,G,C. i am not able to format my query file properly hope it is understood
I hope you understand that you should be thinking "row 0n, column X, not the other way around. Rows enclose columns, so that is the first index that you deal with.
Expand|Select|Wrap|Line Numbers
  1. A = 0; T = 1; G = 2; C = 3
  2. rawdata = \
  3. ['01 0.00 3.67 0.00 0.00',
  4.  
  5. '02 0.00 0.00 3.67 0.00',
  6.  
  7. '03 0.00 0.00 0.00 3.67',
  8.  
  9. '04 0.00 3.67 0.00 0.00',
  10.  
  11. '05 3.67 0.00 0.00 0.00',
  12.  
  13. '06 3.46 0.00 0.22 0.00',
  14.  
  15. '07 0.00 0.00 3.67 0.00',
  16.  
  17. '08 0.00 0.00 0.00 3.67',
  18.  
  19. '09 0.00 0.00 0.00 3.67',
  20.  
  21. '10 0.00 3.67 0.00 0.00',
  22.  
  23. '11 3.67 0.00 0.00 0.00',
  24.  
  25. '12 3.67 0.00 0.00 0.00',
  26.  
  27. '13 0.00 0.00 3.67 0.00',
  28.  
  29. '14 0.00 0.00 0.00 3.67',
  30.  
  31. '15 0.00 0.00 3.67 0.00']
  32.  
  33. datadictionary = {} # usually shorten the name to dd
  34.  
  35. for line in rawdata:
  36.     items = line.split()
  37.     key = items[0]
  38.     datadictionary[key] = [float(item) for item in items[1:]]
  39.  
  40. print datadictionary['09'][C]
Jul 6 '07 #8
bvdet
2,851 Recognized Expert Moderator Specialist
THis is my file! AS u can see the first column gives the position,the second third ..etc are the part of the matrix.my problem here is that.I have to write a code to access the individual values.like if i say A[01] i should have the value 0.00 or say A[06] i should have a value 3.46. or C[04]=3.67 etc. and i have to add one to each of these elements.can you gimme the code. my code is not working.

NA bap

PO A C G T

01 0.00 3.67 0.00 0.00

02 0.00 0.00 3.67 0.00

03 0.00 0.00 0.00 3.67

04 0.00 3.67 0.00 0.00

05 3.67 0.00 0.00 0.00

06 3.46 0.00 0.22 0.00

07 0.00 0.00 3.67 0.00

08 0.00 0.00 0.00 3.67

09 0.00 0.00 0.00 3.67

10 0.00 3.67 0.00 0.00

11 3.67 0.00 0.00 0.00

12 3.67 0.00 0.00 0.00

13 0.00 0.00 3.67 0.00

14 0.00 0.00 0.00 3.67

15 0.00 0.00 3.67 0.00

16 0.00 3.67 0.00 0.00

//

//

NA bcd
For simplicity, let us assume the data file looks like this:

PO A C G T
01 0.00 3.67 0.00 0.00
02 0.00 0.00 3.67 0.00
03 0.00 0.00 0.00 3.67
04 0.00 3.67 0.00 0.00
05 3.67 0.00 0.00 0.00
06 3.46 0.00 0.22 0.00
07 0.00 0.00 3.67 0.00
08 0.00 0.00 0.00 3.67
09 0.00 0.00 0.00 3.67
10 0.00 3.67 0.00 0.00
11 3.67 0.00 0.00 0.00
12 3.67 0.00 0.00 0.00
13 0.00 0.00 3.67 0.00
14 0.00 0.00 0.00 3.67
15 0.00 0.00 3.67 0.00
16 0.00 3.67 0.00 0.00

Create a dictionary of dictionaries:
Expand|Select|Wrap|Line Numbers
  1. fn = r'H:\TEMP\temsys\data7.txt'
  2. lineList = [line.strip().split() for line in open(fn).readlines() if line != '\n']
  3.  
  4. headerList = lineList.pop(0)[1:]
  5.  
  6. # Key list
  7. keys = [i[0] for i in lineList]
  8. # Values list
  9. values = [[float(s) for s in item] for item in [j[1:] for j in lineList]]
  10.  
  11. # Create a dictionary from keys and values
  12. lineDict = dict(zip(keys, values))
  13.  
  14. dataDict = {}
  15.  
  16. for i, item in enumerate(headerList):
  17.     dataDict[item] = {}
  18.     for key in lineDict:
  19.         dataDict[item][key] = lineDict[key][i]
Expand|Select|Wrap|Line Numbers
  1. >>> dataDict['A']['05']
  2. 3.6699999999999999
  3. >>> globals().update(dataDict)
  4. >>> A['05']
  5. 3.6699999999999999
  6. >>> 
Jul 6 '07 #9
aboxylica
111 New Member
hey,
I don really understand what this line does.can u please explain?
values = [[float(s) for s in item] for item in [j[1:] for j in lineList]]
and

for i, item in enumerate(heade rList):
dataDict[item] = {}
for key in lineDict:
dataDict[item][key] = lineDict[key]
I dont understand this either.
for this code,I am getting an error which says
value error:invalid literal for float():A



For simplicity, let us assume the data file looks like this:

PO A C G T
01 0.00 3.67 0.00 0.00
02 0.00 0.00 3.67 0.00
03 0.00 0.00 0.00 3.67
04 0.00 3.67 0.00 0.00
05 3.67 0.00 0.00 0.00
06 3.46 0.00 0.22 0.00
07 0.00 0.00 3.67 0.00
08 0.00 0.00 0.00 3.67
09 0.00 0.00 0.00 3.67
10 0.00 3.67 0.00 0.00
11 3.67 0.00 0.00 0.00
12 3.67 0.00 0.00 0.00
13 0.00 0.00 3.67 0.00
14 0.00 0.00 0.00 3.67
15 0.00 0.00 3.67 0.00
16 0.00 3.67 0.00 0.00

Create a dictionary of dictionaries:
Expand|Select|Wrap|Line Numbers
  1. fn = r'H:\TEMP\temsys\data7.txt'
  2. lineList = [line.strip().split() for line in open(fn).readlines() if line != '\n']
  3.  
  4. headerList = lineList.pop(0)[1:]
  5.  
  6. # Key list
  7. keys = [i[0] for i in lineList]
  8. # Values list
  9. values = [[float(s) for s in item] for item in [j[1:] for j in lineList]]
  10.  
  11. # Create a dictionary from keys and values
  12. lineDict = dict(zip(keys, values))
  13.  
  14. dataDict = {}
  15.  
  16. for i, item in enumerate(headerList):
  17.     dataDict[item] = {}
  18.     for key in lineDict:
  19.         dataDict[item][key] = lineDict[key][i]
Expand|Select|Wrap|Line Numbers
  1. >>> dataDict['A']['05']
  2. 3.6699999999999999
  3. >>> globals().update(dataDict)
  4. >>> A['05']
  5. 3.6699999999999999
  6. >>> 
Jul 7 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1689
by: SUPER_SOCKO | last post by:
Suppose I have a matrix M x N dimension. Normally, to print all of the elements in the matrix. I write the following codes: Suppose M = 5, N = 4: for(int j=0; j<5; ++j) { for(int k=0; k<4; ++k) { cout << matrix << " "; }
4
5031
by: Rick Brown | last post by:
I have a form built in Access2000 that looks like the hardcopy in all ways including color. I now want to move this form to the company Intra-Net via ASP.NET. Is there a way to wizard this form over to .NET or can this form be rebuilt in a .NET editor retaining the same look? OR????? RICK
4
4046
by: sitemap | last post by:
Hello. I have a problem to accessing the right clindNodes from the xml. I have many "step" elements with id. I want to get the attributes from the 5 "matrix" elements, depending on parent "step" id ( example - if some var(MyVariable) = 2, the code should get the 5 matrix attributes that have a parent <step id="2" >). My codea always returning the 5 matrix attributes only from step id="1", and does not mather that i change the MyVariable...
9
5706
by: sean.scanlon | last post by:
can someone help understand how i can could access a struct field dymanically like: foo->fields ? when i try to compile this i get the following error: 'struct pwd' has no member named 'fields' is there a way to treat fields as the member name of the struct?
4
7641
by: deLenn | last post by:
Hi, Does scipy have an equivalent to Matlab's 'find' function, to list the indices of all nonzero elements in a sparse matrix? Cheers.
6
4267
by: suresh4ind | last post by:
Hi i need to copy the upper triangular elements of matrix A to an array B,how to perform this ,can u send me the accessing formula
1
15246
by: dazzler | last post by:
Hi! I have problem with numpy, multiplying with an inversed matrix will crash python :( this works fine: from numpy import matrix A = matrix(,]) B = matrix(,]) print A.I #inverse matrix
3
2124
by: 8rea | last post by:
Hello, I have got a matrix and I want to get his elements so I can work with them in an unsigned int or in a similar way. I have proved to get them with the method Element but it returns an array and I think it don't do what I want, something like this L; Another thing that I have thought is to change this matrix into a String like this, but then I found the same problem. I don't know how to get this elements! ...
12
3122
by: Nezhate | last post by:
Hi all, Is there any way to print a matrix's elements using a simple printf ? what I want as result is for example: if mat ={0,1,2,3} result must be: "0123". I tried this code: /***************************************************************/ #include <stdio.h>
0
8584
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8768
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4308
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2945
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.