473,411 Members | 2,196 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,411 software developers and data experts.

Pythonic way to condese my array

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
Sep 20 '06 #1
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

Sep 20 '06 #2
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/
Sep 20 '06 #3
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
Sep 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
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...
1
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...
12
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...
15
by: Ville Vainio | last post by:
Pythonic Nirvana - towards a true Object Oriented Environment ============================================================= IPython (by Francois Pinard) recently (next release - changes are...
10
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...
11
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...
4
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...
14
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...
3
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...
26
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...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.