473,320 Members | 1,744 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,320 software developers and data experts.

Lining Up and PaddingTwo Similar Lists

Maybe there's some function like zip or map that does this. If not, it's
probably fairly easy to do with push and pop. I'm just checking to see if
there's not some known simple single function that does what I want. Here's
what I'm trying to do.

I have a list dat like (assume the items are strings even thought I'm
omitting quotes.):
[a.dat, c.dat, g.dat, k.dat, p.dat]

I have another list called txt that looks like:
[a.txt, b.txt, g.txt, k.txt r.txt, w.txt]

What I need is to pair up items with the same prefix and use "None", or some
marker, to indicate the absence of the opposite item. That is, in non-list
form, I want:
a.dat a.txt
None b.txt
c.dat None
g.dat g.txt
k.dat k.txt
p.dat None
None r.txt
None w.txt

Ultimately, what I'm doing is to find the missing member of pairs.
--
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: <www.speckledwithstars.net/>
Aug 29 '08 #1
5 990
On Aug 28, 10:50*pm, "W. eWatson" <notval...@sbcglobal.netwrote:
Maybe there's some function like zip or map that does this. If not, it's
probably fairly easy to do with push and pop. I'm just checking to see if
there's not some known simple single function that does what I want. Here's
what I'm trying to do.

I have a list dat like (assume the items are strings even thought I'm
omitting quotes.):
[a.dat, c.dat, g.dat, k.dat, p.dat]

I have another list called txt that looks like:
[a.txt, b.txt, g.txt, k.txt r.txt, w.txt]

What I need is to pair up items with the same prefix and use "None", or some
marker, to indicate the absence of the opposite item. That is, in non-list
form, I want:
a.dat a.txt
None *b.txt
c.dat None
g.dat g.txt
k.dat k.txt
p.dat *None
None *r.txt
None *w.txt

Ultimately, what I'm doing is to find the missing member of pairs.
--
* * * * * * Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

* * * * * * * (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std.time)
* * * * * * * *Obz Site: *39° 15' 7" N, 121° 2' 32"W, 2700 feet

* * * * * * * * * * *Web Page: <www.speckledwithstars.net/>
This gets you your list. What do you mean by 'missing member of
pairs'? If you mean, 'set of elements that appear in both' or 'set
that appears in one but not both', you can short circuit it at line
14.

-warning, spoiler-

dat= ['a.dat', 'c.dat', 'g.dat', 'k.dat', 'p.dat']
dat.sort()
txt= ['a.txt', 'b.txt', 'g.txt', 'k.txt', 'r.txt', 'w.txt']
txt.sort()
import os.path
datD= {}
for d in dat:
r,_= os.path.splitext( d )
datD[ r ]= d
txtD= {}
for d in txt:
r,_= os.path.splitext( d )
txtD[ r ]= d
both= sorted( list( set( datD.keys() )| set( txtD.keys() ) ) )

print datD
print txtD
print both

for i, x in enumerate( both ):
both[ i ]= datD.get( x, None ), txtD.get( x, None )

print both

OUTPUT:

{'a': 'a.dat', 'p': 'p.dat', 'c': 'c.dat', 'k': 'k.dat', 'g': 'g.dat'}
{'a': 'a.txt', 'b': 'b.txt', 'g': 'g.txt', 'k': 'k.txt', 'r': 'r.txt',
'w': 'w.t
xt'}
['a', 'b', 'c', 'g', 'k', 'p', 'r', 'w']
[('a.dat', 'a.txt'), (None, 'b.txt'), ('c.dat', None), ('g.dat',
'g.txt'), ('k.d
at', 'k.txt'), ('p.dat', None), (None, 'r.txt'), (None, 'w.txt')]
Aug 29 '08 #2
"W. eWatson" <no*******@sbcglobal.netwrites:
[a.dat, c.dat, g.dat, k.dat, p.dat]
[a.txt, b.txt, g.txt, k.txt r.txt, w.txt]

What I need is to pair up items with the same prefix and use "None",
or some marker, to indicate the absence of the opposite item.
This is functionally influenced but should be straightforward:

dat = ['a.dat', 'c.dat', 'g.dat', 'k.dat', 'p.dat']
txt = ['a.txt', 'b.txt', 'g.txt', 'k.txt', 'r.txt', 'w.txt']

# just get the portion of the filename before the first period
def prefix(filename):
return filename[:filename.find('.')]

# make a dictionary mapping prefixes to filenames
def make_dict(plist):
return dict((prefix(a),a) for a in plist)

pdat = make_dict(dat)
ptxt = make_dict(txt)

# get a list of all the prefixes, use "set" to remove
# duplicates, then sort the result and look up each prefix.
for p in sorted(set(pdat.keys() + ptxt.keys())):
print pdat.get(p), ptxt.get(p)
Aug 29 '08 #3
D,T=[dict((x.split('.')[0],x) for x in X) for X in (dat,txt)]
for k in sorted(set(D).union(T)) :
for S in D,T :
print '%-8s' % S.get(k,'None'),
print

HTH

W. eWatson wrote:
Maybe there's some function like zip or map that does this. If not, it's
probably fairly easy to do with push and pop. I'm just checking to see
if there's not some known simple single function that does what I want.
Here's what I'm trying to do.

I have a list dat like (assume the items are strings even thought I'm
omitting quotes.):
[a.dat, c.dat, g.dat, k.dat, p.dat]

I have another list called txt that looks like:
[a.txt, b.txt, g.txt, k.txt r.txt, w.txt]

What I need is to pair up items with the same prefix and use "None", or
some marker, to indicate the absence of the opposite item. That is, in
non-list form, I want:
a.dat a.txt
None b.txt
c.dat None
g.dat g.txt
k.dat k.txt
p.dat None
None r.txt
None w.txt

Ultimately, what I'm doing is to find the missing member of pairs.
Aug 29 '08 #4
castironpi wrote:
....
>
I don't think that's guaranteed by anything. I realized that
'dat.sort()' and 'txt.sort()' weren't necessary, since their contents
are moved to a dictionary, which isn't sorted.
Actually, I'm getting the file names from listdir, and they appear to be
sorted low to high. I tried it on a folder with lots of dissimilar files.
>
both= set( datD.keys() )& set( txtD.keys() )

This will get you the keys (prefixes) that are in both. Then for
every prefix if it's not in 'both', you can report it.

Lastly, since you suggest you're guaranteed that 'txt' will all share
the same extension, you can do away with the dictionary and use sets
entirely. Only if you can depend on that assumption.
Each dat file contains an image, and its description and related parameters
are in the corresponding txt file.
>
I took a look at this. It's probably more what you had in mind, and
the dictionaries are overkill.
....
Aug 29 '08 #5
On Aug 29, 1:29 am, "W. eWatson" <notval...@sbcglobal.netwrote:
It looks like I have a few new features to learn about in Python. In particular,
dictionaries.
In Python it's hard to think of many non-trivial problems that you
*don't* have to know about dictionaries.

George
Aug 29 '08 #6

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

Similar topics

13
by: Paulo Pinto | last post by:
Hi, does anyone know of a Python package that is able to load XML like the XML::Simple Perl package does? For those that don't know it, this package maps the XML file to a dictionary.
2
by: Shalafi | last post by:
Hi, I've modified an old library, flip (fuzzy logic stuff), to work with new gcc compiler, I've made operation like substituting #include <strstream.h> with #include <strstream> and inserted...
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
59
by: MotoK | last post by:
Hi Experts, I've just joined this group and want to know something: Is there something similar to smart pointers in C or something to prevent memory leakages in C programs. Regards MotoK
16
by: Michael M. | last post by:
How to find the longst element list of lists? I think, there should be an easier way then this: s1 = s2 = s3 = if len(s1) >= len(s2) and len(s1) >= len(s3): sx1=s1 ## s1 ist längster
4
by: Adam | last post by:
I have four roughly square DIVs, all the same height, which I need to line up horizontally across the page. What would be the best way to do this? At the moment I have a very botched solution where...
1
by: tgmurray | last post by:
Hi everyone. I am getting the results I would like in Firefox and Netscape but unfortunately not in IE7. My problem is that my uordered lists is not lining up with an image that has been...
2
by: alexburi | last post by:
Hi, I'm having a problem with the bullets lining up correctly with text in each <li>. If there is only one line of text, the bullet matches. But if there are two lines of text or more, the bullet...
6
by: Eric | last post by:
I'm learning Python (while coming from MATLAB). One question I have is that if I have a list with say 8 elements, and I want just a few of them how do I select them out. In MATLAB, if I just want...
7
by: milestogofromhere | last post by:
It is late so this is probably something really obvious but I am not seeing it. Can someone please help? Here is the page in question: html - http://www.itsyourplate.com/index2.php css -...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.