newbie list question

  • Thread starter Kamus of Kadizhar
  • Start date
K

Kamus of Kadizhar

I have two lists, fav and oldfav. Each has the same number of elements.
Each is a list of lists:

fav: [['dcp_0229.jpg', 'dcp_0230.jpg', 'dcp_0231.jpg', 'dcp_0235.jpg',
'dcp_0237.jpg', 'dcp_0238.jpg', 'dcp_0240.jpg', 'dcp_0243.jpg'],
['dcp_0244.jpg', 'dcp_0252.jpg', 'dcp_0257.jpg', 'dcp_0275.jpg',
'dcp_0276.jpg', 'dcp_0280.jpg', 'dcp_0283.jpg', 'dcp_0284.jpg'],
['dcp_0287.jpg', 'dcp_0293.jpg', 'dcp_0294.jpg', 'dcp_0303.jpg',
'dcp_0308.jpg', 'dcp_0314.jpg', 'dcp_0319.jpg', 'dcp_0331.jpg'],
['dcp_0336.jpg', 'dcp_0346.jpg', 'dcp_0347.jpg', 'dcp_0349.jpg',
'dcp_0350.jpg', 'dcp_0353.jpg', 'dcp_0355.jpg', 'dcp_0360.jpg']]

oldfav: [['dcp_0278.jpg', 'dcp_0348.jpg', 'dcp_0349.jpg',
'dcp_0275.jpg', 'dcp_0347.jpg', 'dcp_0318.jpg', 'dcp_0280.jpg',
'dcp_0315.jpg', 'dcp_0240.jpg'], [], [], []]

I want to remove those elements that are in the sub-lists of fav that
also appear in the corresponding sub-list of oldfav:

for the 1st sub-list of fav
difffav[0:1] = Set(fav[0:1]) - Set(oldfav[0:1])
for the 2nd sub-list of fav
difffav[1:2] = Set(fav[1:2]) - Set(oldfav[1:2])

and so on.

The number of sub-lists in fav and oldfav may change, but they will
always be the same.

I can hack this up using C-style indexes, but I have to imagine there is
some neat python way that eliminates indexes altogether.

-Kamus
 
P

Paul Rubin

Kamus of Kadizhar said:
I want to remove those elements that are in the sub-lists of fav that
also appear in the corresponding sub-list of oldfav:

I can hack this up using C-style indexes, but I have to imagine there
is some neat python way that eliminates indexes altogether.

Does this work?

difffav = [[f for f in new if f not in old] for new,old in zip(fav,oldfav)]
 
K

Kamus of Kadizhar

Paul said:
Kamus of Kadizhar said:
I want to remove those elements that are in the sub-lists of fav that
also appear in the corresponding sub-list of oldfav:

I can hack this up using C-style indexes, but I have to imagine there
is some neat python way that eliminates indexes altogether.


Does this work?

difffav = [[f for f in new if f not in old] for new,old in zip(fav,oldfav)]

Yes it does. But I have no idea how. :-(

I think it's going to take me a while to figure out what the above
does.... :) Where do I find that in the FM?

-Kamus
 
P

Paul Rubin

Kamus of Kadizhar said:
difffav = [[f for f in new if f not in old] for new,old in
zip(fav,oldfav)]

Yes it does. But I have no idea how. :-(

I think it's going to take me a while to figure out what the above
does.... :) Where do I find that in the FM?

Look up "list comprehension" in the index... also the "zip" function
in the library reference. List comprehensions are a "functional" way
of expressing lists, like

oddsquares = [x*x for x in (1,2,3,4,5) if x%2 == 1]

would set oddsquares to [1,9,25]. It will likely take a little
practice before this stuff rattles off your fingertips easily.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,172
Messages
2,570,933
Members
47,473
Latest member
ChristelPe

Latest Threads

Top