Hi,
I have a 2D array,
maybe irregular, like
arr = [[2,2,2,2],
[2,2,2,2],
[2,2,2,2]]
if tried to pull an index list
(tuples or array elements) of
all positions - via the map funtion,
but failed.
I tried to get sth. like
[
[0,0],
[0,1],
[0,2],
...
]
for each element which really exists
in the 2D array above.
What I really got, was another in-
direction for each row of arr
[
[(0, 0), (0, 1), (0, 2), (0, 3)],
[(1, 0), (1, 1), (1, 2), (1, 3)],
...
]
How can I flatten the list in place by map(),
so that the outer map hull would collect
[i,j] flat?
This is what I tried:
id = map(lambda i: map(lambda j: (i,j), range(len(arr[i]))), range(len(arr)) )
print id
I tried the map solution because I'd make a
shot at another posting (find min of 2D array),
what I intended to solve by decorate/undecorate
sort.
But I didn't manage to create the flat
coordinate list in one map(map)-stroke ;-)
Regards
Mirco 3 2086
Maybe you want something like this (but this doesn't use map):
def indexes(m):
return [(r,c) for r, row in enumerate(m) for c in xrange(len(row) )]
m1 = [[2,2,5],
[2,2],
[2,2,2,2]]
m2 = [[],
[2],
[1,2,3,4]]
print indexes(m1)
print indexes(m2)
Output:
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (2, 0), (2, 1), (2, 2), (2,
3)]
[(1, 0), (2, 0), (2, 1), (2, 2), (2, 3)]
Bye,
bearophile
Thus spoke be************@ lycos.com (on 2006-06-23 00:57): Maybe you want something like this (but this doesn't use map): [(r,c) for r, row in enumerate(m) for c in xrange(len(row) )]
Ahh, its a 'list comprehension', nice. Now,
lets see how the decorate/undecorate sort
turns out to look in Python:
arr = [
[3,3,3,3],
[3,3,3,1],
[3,3,3,3] ]
print \
sorted(
[ (j,i) for j, row in enumerate(arr) for i in xrange(len(row) ) ],
lambda a,b: (arr[a[0]][a[1]] - arr[b[0]][b[1]])
)[ 0 ]
==> prints indices: (1,3)
He, this looks more like Haskell than like
Python (for me, it looks awful ;-)
I'll try to come up with at least one
map inside the comprehension, if that
works - just to avoid the dual for ;-)
Reagrds and thanks
Mirco
Mirco: He, this looks more like Haskell than like Python (for me, it looks awful ;-)
Maybe this is more readable:
ar = [[3,3,3,3],
[3,3,3,1],
[3,3,4,3]]
print sorted( [(r,c) for r,row in enumerate(ar) for c in
xrange(len(row) )],
key=lambda (r,c): ar[r][c]
)[0]
I don't know if operator.itemge tter can be used here, I think it's too
much complex.
With python 2.5 you can probably simplify it a little (and speed it up)
with something like:
print min( [ (r,c) for r,row in enumerate(ar) for c in xrange(len(row) )
],
key=lambda (r,c): arr[r][c]
)
Bye,
bearophile This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: .d.hos |
last post by:
ok, fairly new to python, relatively familiar w/ ms-sql. here's my
issue:
my .py script parses the contents of a (tab delim.) flat file, then
attempts to insert the information into the db. I've been fighting
this for a day or so, and i'm stuck on the db insertion...
basically the script uses .readlines() to capture the flat file
contents, and stick the *record* into a container list...
|
by: Tom Anderson |
last post by:
hi all,
i've got a few proposals to do, in one way or another, with the map
builtin. i'd like to hear what people think.
firstly, collections, and things that look like collections, should
support the call operator; the implementation should look like:
def __call__(self, arg):
return self
|
by: Mandus |
last post by:
Hi there,
inspired by a recent thread where the end of reduce/map/lambda in Python was
discussed, I looked over some of my maps, and tried to convert them to
list-comprehensions.
This one I am not sure how to conver:
Given three tuples of length n, b,i and d, I now do:
|
by: Tom Anderson |
last post by:
Comrades,
During our current discussion of the fate of functional constructs in
python, someone brought up Guido's bull on the matter:
http://www.artima.com/weblogs/viewpost.jsp?thread=98196
He says he's going to dispose of map, filter, reduce and lambda. He's
going to give us product, any and all, though, which is nice of him.
|
by: jmoy |
last post by:
I am a C programmer graduating to C++. As an exercise I wrote a
program to count the number of times that different words occur in a
text file. Though a hash table might have been a better choice, I
chose to use std::map. However, the program runs at less than half the
speed of an equivalent program that uses ordinary handcoded binary
trees. The result is not changed very much if I replace std::string as
the key with a simple string class...
| |
by: David Isaac |
last post by:
Newbie question:
I have been generally open to the proposal that list comprehensions
should replace 'map', but I ran into a need for something like
map(None,x,y)
when len(x)>len(y). I cannot it seems use 'zip' because I'll lose
info from x. How do I do this as a list comprehension? (Or,
more generally, what is the best way to do this without 'map'?)
Thanks,
|
by: metiu uitem |
last post by:
Say you have a flat list:
How do you efficiently get
, , ]
I was thinking of something along the lines of:
for (key,number) in list:
print key, number
|
by: Phillip Sitbon |
last post by:
Hello there,
I have a situation where a list of functions need to be called with a
single set of parameters and the result constructed into a tuple. I
know there's simple ways to do it via list comprehension:
Result = tuple( )
I'd hope there's a more efficient way to do this with a built-in
function, so that I could call:
|
by: sugarsmack |
last post by:
Hi folks,
Using XSL, I'm trying to take an XML file containing a flat list of "topics" and generate a hierarchical topic map. The topic nodes include a role attribute that indicates their position in the hierarchy (role="1" being the top node).
Here is the original XML file structure:
<NoName>
<topic id="id1" role="1">
<title>Heading 1 Title</title>
<body>
|
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...
|
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...
| |
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,...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
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
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |