I have a list of dictionaries where each dictionary defines, among other
things, a row and column number. So, my list might look like this:
[{'row':1, 'column':1, otherdata}, {'row':1, 'column':2, 'otherdata}...]
This data is passed to flash and used there to create a grid of objects that
are placed based on the row and column values.
For a given set of data I may have values column values in the range of 1, 9
inclusive.
What I would like to do is take my list of dictionaries (like the one listed
above) and shift the column numbers down to fill in missing values...
example (only the column key,value pair is show for simplification):
[{'column':1}, {'column':2}, {'column', 4}, {'column': 8}, {'column':2},
{'column', 4}]
If this were the dataset... I would want to change all 4s to 3 and all 8s to
4. That way I end up with 1 ... 4 instead of 1, 2, 4, and 8.
Is there some slick way to do this?
Thanks 3 1201
Dean Card wrote:
I have a list of dictionaries where each dictionary defines, among other
things, a row and column number. So, my list might look like this:
[{'row':1, 'column':1, otherdata}, {'row':1, 'column':2, 'otherdata}...]
This data is passed to flash and used there to create a grid of objects that
are placed based on the row and column values.
For a given set of data I may have values column values in the range of 1, 9
inclusive.
What I would like to do is take my list of dictionaries (like the one listed
above) and shift the column numbers down to fill in missing values...
example (only the column key,value pair is show for simplification):
[{'column':1}, {'column':2}, {'column', 4}, {'column': 8}, {'column':2},
{'column', 4}]
If this were the dataset... I would want to change all 4s to 3 and all 8s to
4. That way I end up with 1 ... 4 instead of 1, 2, 4, and 8.
Is there some slick way to do this?
Untested, but intended for Python 2.4:
list_o_dicts = [{'column': 1}, ...]
columns = set(x['column'] for x in list_o_dicts)
column_map = dict((column, i+1) for i, column in enumerate(sorted(columns)))
for d in list_o_dicts:
d['column'] = column_map[d['column']]
Of course, code that I wouldn't get fired for would have a slightly greater line
count and fewer one-letter variable names.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
Dean Card wrote:
I have a list of dictionaries where each dictionary defines, among other
things, a row and column number. So, my list might look like this:
[{'row':1, 'column':1, otherdata}, {'row':1, 'column':2, 'otherdata}...]
This data is passed to flash and used there to create a grid of objects that
are placed based on the row and column values.
For a given set of data I may have values column values in the range of 1, 9
inclusive.
What I would like to do is take my list of dictionaries (like the one listed
above) and shift the column numbers down to fill in missing values...
example (only the column key,value pair is show for simplification):
[{'column':1}, {'column':2}, {'column', 4}, {'column': 8}, {'column':2},
{'column', 4}]
If this were the dataset... I would want to change all 4s to 3 and all 8s to
4. That way I end up with 1 ... 4 instead of 1, 2, 4, and 8.
Is there some slick way to do this?
Thanks
Not sure how slick this is, but I would do it thus:
dicts = [{'column':1}, {'column':2}, {'column': 4}, {'column': 8},
{'column':4}]
vals = list(set(d['column'] for d in dicts))
vals.sort()
amap = dict((b,a) for (a,b) in enumerate(vals))
for d in dicts:
d['column'] = amap[d['column']] + 1
The "1" in the last line comes from your requirement to start numbering
at 1 instead of 0.
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095 http://www.jamesstroud.com/
Robert Kern wrote:
Of course, code that I wouldn't get fired for would have a slightly greater line
count and fewer one-letter variable names.
Great :D!
That one made me laugh!
wildemar This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tom Evans |
last post by:
My basic question:
If I have a specific interface which I know is going to be implemented
by a number of classes, but there is no implementation commonality
between them, what is the preferred...
|
by: asdf sdf |
last post by:
i need some advice. i'm a back end programmer historically, but have
been exploring python for webapps and enjoying it.
i need to build some simple Win client-server or standalone apps.
the...
|
by: Nickolay Kolev |
last post by:
Hi all,
I would like to find a more pythonic way of solving the following:
Having a string consisting of letters only, find out the total sound
score of the string. The sound score is...
|
by: Ville Vainio |
last post by:
Pythonic Nirvana - towards a true Object Oriented Environment
=============================================================
IPython (by Francois Pinard) recently (next release - changes are...
|
by: Bulba! |
last post by:
Hello everyone,
I'm reading the rows from a CSV file. csv.DictReader puts
those rows into dictionaries.
The actual files contain old and new translations of software
strings. The dictionary...
|
by: Charles Krug |
last post by:
I've a function that needs to maintain an ordered sequence between
calls.
In C or C++, I'd declare the pointer (or collection object) static at
the function scope.
What's the Pythonic way to...
|
by: Carl J. Van Arsdall |
last post by:
It seems the more I come to learn about Python as a langauge and the way
its used I've come across several discussions where people discuss how
to do things using an OO model and then how to design...
|
by: Pythor |
last post by:
I wrote the following code for a personal project. I need a function
that will plot a filled circle in a two dimensional array. I found
Bresenham's algorithm, and produced this code. Please tell...
|
by: jnair |
last post by:
My Tead Lead my object counter code seen below is not pythonic
class E(object):
_count = 0
def __init__(self):
E._count += 1
count = property(lambda self: E._count )
def test():
if...
|
by: Frank Samuelson |
last post by:
I love Python, and it is one of my 2 favorite
languages. I would suggest that Python steal some
aspects of the S language.
-------------------------------------------------------
1. Currently...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
|
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...
|
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...
| |