473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to return matrix index

I have a dictionary which is made pretty as a matrix:

Dictionary:
{'1': {'3': 0, '2': 1, '5': 1, '4': 0, '6': 29},
'3': {'1': 0, '2': 0, '5': 0, '4': 1, '6': 1},
'2': {'1': 13, '3': 1, '5': 21, '4': 0, '6': 0},
'5': {'1': 39, '3': 0, '2': 1, '4': 0, '6': 14},
'4': {'1': 1, '3': 1, '2': 17, '5': 2, '6': 0},
'6': {'1': 0, '3': 43, '2': 0, '5': 0, '4': 1}}

The information as a matrix:
Expand|Select|Wrap|Line Numbers
  1.     1    2    3    4    5    6    
  2. 1    -    1    0    0    1    29   
  3. 2    13   -    1    0    21   0    
  4. 3    0    0    -    1    0    1    
  5. 4    1    17   1    -    2    0    
  6. 5    39   1    0    0    -    14   
  7. 6    0    0    43   1    0    -   
From the matrix i can see that the column number 4 has the lowest numbers.
I want to write a function findFristFread(overlaps) that takes the dictionary as argument and returns '4' because that is the column with lowest numbers.

How do i get the function to 'understand' that i want the lowest numbers?

Can anybody get me started?
Jan 3 '13 #1
2 2046
ADezii
8,834 Expert 8TB
You can start by passing to your Function a 2-Dimensional Array containing the Elements of the Matrix.
Jan 3 '13 #2
bvdet
2,851 Expert Mod 2GB
You can create a 2D array from the dictionary like this:
Expand|Select|Wrap|Line Numbers
  1. m = [[0 for _ in range(6)] for _ in range(6)]
  2. for key in dd:
  3.     for subkey in dd[key]:
  4.         m[int(key)-1][int(subkey)-1] = dd[key][subkey]
It looks like this:
Expand|Select|Wrap|Line Numbers
  1. >>> m
  2. [[0, 1, 0, 0, 1, 29], [13, 0, 1, 0, 21, 0], [0, 0, 0, 1, 0, 1], [1, 17, 1, 0, 2, 0], [39, 1, 0, 0, 0, 14], [0, 0, 43, 1, 0, 0]]
  3. >>>

From there, you can sum each column to determine which one has the lowest sum.
Jan 3 '13 #3

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

Similar topics

9
by: kosh | last post by:
I was wondering if there is or there could be some way to pass a generator an optional starting index so that if it supported that slicing could be made more efficient. Right now if you do use a...
9
by: custard_pie | last post by:
I need help sorting a list...I just can't figure out how to sort a list and then return a list with the index of the sorted items in the list for example if the list I want to sort is I need to...
3
by: Tim Wesson | last post by:
Does anyone know if it is possible to choose between the below templates (or similar ones) according to the promotion rules for types T and U? Thanks, Tim Wesson.
6
by: Chris Buckett | last post by:
Hi, I was just wondering if anyone has had any experience in developing a shipping matrix. We are based in the UK, and we need to ship both locally and internationally. Local shipping is...
6
by: jj_frap | last post by:
I'm new to programming in Python and am currently writing a three-card poker simulator. I have completed the entire simulator other than determining who has the best hand (which will be far more...
2
by: DarrenWeber | last post by:
Below is a module (matrix.py) with a class to implement some basic matrix operations on a 2D list. Some things puzzle me about the best way to do this (please don't refer to scipy, numpy and...
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
3
by: robert | last post by:
Often I want to extract some web table contents. Formats are mostly static, simple text & numbers in it, other tags to be stripped off. So a simple & fast approach would be ok. What of the...
1
by: PIERREWHY | last post by:
I have a datagridview containing a datagridviewcombobox. The combobox has a dataset containing city_name and city_id. When I select a new city_name in the combobox I would like to return the...
6
by: atemuoh1991 | last post by:
#include <stdio.h> #include <math.h> #include <malloc.h> #include <stdlib.h> #include <time.h> struct SMatrix { double** pValues;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.