Lining Up and PaddingTwo Similar Lists
W. eWatson
notvalid2 at sbcglobal.net
Fri Aug 29 01:29:13 EDT 2008
castironpi wrote:
> On Aug 28, 10:50 pm, "W. eWatson" <notval... at sbcglobal.net> 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.
>> --
>> 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
(a.dat, a.txt) is a pair. (None, a.txt) has a.dat missing. I just need to
issue a msg to the user that one member of a file pair is missing. Both
files need to be present to make sense of the data.
> 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-
It looks like you went beyond the call of duty, but that's fine. It looks
like I have a few new features to learn about in Python. In particular,
dictionaries. Thanks.
Actually, the file names are probably in order as I pick them up in XP. I
would think if someone had sorted the folder, that as one reads the folder
they are in alpha order, low to high.
>
> 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')]
--
W. Watson
(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
More information about the Python-list
mailing list