Dictionary into a table format - Python
Dictionary into a table format - Python
(OP)
Hello everyone,
I am having some problems trying to print the following dictionary:
{('31', '3'): '2.85124e-05', ('31', '23'): '2.85124e-05', ('31', '17'): '2.85124e-05', ('31', '30'): '2.85124e-05', ('31', '19'): '2.85124e-05', ('31', '31'): '2.85124e-05', ('31', '26'): '2.85124e-05', ('31', '10'): '2.85124e-05', ('31', '7'): '2.85124e-05', ('31', '22'): '2.85124e-05', ('31', '12'): '2.85124e-05', ('31', '25'): '2.85124e-05', ('31', '5'): '2.85124e-05', ('31', '8'): '2.85124e-05', ('31', '21'): '2.85124e-05', ('31', '14'): '2.85124e-05', ('31', '16'): '2.85124e-05', ('31', '1'): '2.85124e-05', ('31', '18'): '2.85124e-05', ('31', '24'): '2.85124e-05', ('31', '6'): '2.85124e-05', ('31', '15'): '2.85124e-05', ('31', '11'): '2.85124e-05', ('31', '29'): '2.85124e-05', ('31', '4'): '2.85124e-05', ('31', '13'): '2.85124e-05', ('31', '27'): '2.85124e-05', ('31', '2'): '2.85124e-05', ('31', '9'): '2.85124e-05', ('31', '20'): '2.85124e-05', ('31', '28'): '2.85124e-05'}
into a tabular format.
I would like that the first two keys of the dictionary are the indices of a matrix (the first one is the row number and the second is the column number) and the value is the matrix value.
Anyone has an idea how to print this into a table/matrix with 31 rows x 31 columns, where the 31st row is something like:
2.85124e-05
2.85124e-05
2.85124e-05
2.85124e-05
2.85124e-05
2.85124e-05
2.85124e-05
2.85124e-05
...
2.85124e-05
? Thank you very much for your help!
I am having some problems trying to print the following dictionary:
{('31', '3'): '2.85124e-05', ('31', '23'): '2.85124e-05', ('31', '17'): '2.85124e-05', ('31', '30'): '2.85124e-05', ('31', '19'): '2.85124e-05', ('31', '31'): '2.85124e-05', ('31', '26'): '2.85124e-05', ('31', '10'): '2.85124e-05', ('31', '7'): '2.85124e-05', ('31', '22'): '2.85124e-05', ('31', '12'): '2.85124e-05', ('31', '25'): '2.85124e-05', ('31', '5'): '2.85124e-05', ('31', '8'): '2.85124e-05', ('31', '21'): '2.85124e-05', ('31', '14'): '2.85124e-05', ('31', '16'): '2.85124e-05', ('31', '1'): '2.85124e-05', ('31', '18'): '2.85124e-05', ('31', '24'): '2.85124e-05', ('31', '6'): '2.85124e-05', ('31', '15'): '2.85124e-05', ('31', '11'): '2.85124e-05', ('31', '29'): '2.85124e-05', ('31', '4'): '2.85124e-05', ('31', '13'): '2.85124e-05', ('31', '27'): '2.85124e-05', ('31', '2'): '2.85124e-05', ('31', '9'): '2.85124e-05', ('31', '20'): '2.85124e-05', ('31', '28'): '2.85124e-05'}
into a tabular format.
I would like that the first two keys of the dictionary are the indices of a matrix (the first one is the row number and the second is the column number) and the value is the matrix value.
Anyone has an idea how to print this into a table/matrix with 31 rows x 31 columns, where the 31st row is something like:
2.85124e-05
2.85124e-05
2.85124e-05
2.85124e-05
2.85124e-05
2.85124e-05
2.85124e-05
2.85124e-05
...
2.85124e-05
? Thank you very much for your help!
RE: Dictionary into a table format - Python
CODE
Output:
CODE
RE: Dictionary into a table format - Python
RE: Dictionary into a table format - Python
Thank you very much for your reply, could you also help me to get the number of rows and columns of a dictionary?
How do I do that? I am used to do "size(matrix)" in matlab to get this information.
Thank you!
RE: Dictionary into a table format - Python
we can get a list of all keys from dictionary - its for my example this
[(1, 2), (1, 3), (2, 1), (2, 3), (2, 2), (1, 1)]
Then from it extract all row-indices , it's first components from (., .) - i.e.:
[1, 1, 2, 2, 2, 1]
and similarly col-indices, it's second components from (., .) - i.e.:
[2, 3, 1, 3, 2, 1]
and then when we take the maximum of the list we will get number of matrix rows and columns. For my example it's 2 x 3 matrix.
The code is here:
CODE
RE: Dictionary into a table format - Python
CODE
RE: Dictionary into a table format - Python
>>print dic[i, j],
does not work.
I think I need to write something like this: print(dic[i,j]+",")
RE: Dictionary into a table format - Python
Look at your version with
CODE
Then instead of
CODE
try to use
CODE
https://docs.python.org/3.0/whatsnew/3.0.html
RE: Dictionary into a table format - Python
Here is my code. Unfortunately I am not having the same result as you had:
CODE --> python
RE: Dictionary into a table format - Python
RE: Dictionary into a table format - Python
What is your input data and what's your result ?
When I look at your code, then for example
CODE
CODE
>>> for i in range (1, rows+1): ... print i ... 1 2 3 4 5
RE: Dictionary into a table format - Python
What I would like to have is a matrix 31x31 with zeros and using the information in "out_matrix2.txt" populate the matrix (substitute the value "0" for the value that is specified by the txt file).
I think I am not going in the correct direction...
If the file "out_matrix2.txt" has the following information:
1,1,2.1
1,4,0.1
3,2,0.4
The script should extract the maximum value in the first key and the value in the second key, i.e., 3 and 4, respectively. Then, it create a matrix 3x4 with zeros. After that, using the data available in the file "out_matrix2.txt", the output should be:
2.1,0.0,0.0,0.1
0.0,0.0,0.0,0.0
0,0,0.4,0.0,0.0
That is exactly what I am needing. I have seen that is very easy using "numpy" library, but I cant use it.
Thank you very much for your support!
RE: Dictionary into a table format - Python
So I have this data file:
matrix.txt
CODE
and this source:
dic2tab.py
CODE
which gives me the output:
CODE
If you set in the script above
CODE
DBG = True
CODE
RE: Dictionary into a table format - Python
I am getting this:
"matrix (3 x 4)
2.10
0.00
0.00
0.10
0.00
0.00
0.00
0.00
0.00
0.40
0.00
0.00
"
Here is your code adapted to the version 3.3:
CODE -->
I think there is a problem with the last print.
RE: Dictionary into a table format - Python
CODE
CODE
RE: Dictionary into a table format - Python
I have another question: since I am not very familiar with python, what I normally do when I have a python script it consists into send the output to a file (in linux), like this:
CODE -->
or
CODE -->
I would like to substitute your print by file.write(...)
For example, using
CODE -->
and then inside the for loop,
CODE -->
The main problem is that this last command is not working due to "wrong syntax".
Could you tell how to put your script printing the matrix for a file?
Thank you for your assistance!
RE: Dictionary into a table format - Python
CODE
RE: Dictionary into a table format - Python
However, I am getting a problem with the space, the attribute end=" " is no longer available for append
I have the following error message:
TypeError: append() takes no keyword arguments
RE: Dictionary into a table format - Python
Coyp the last code I posted. It must work in both Python versions.