0

Suppose unordered listA = [1 2 3] s.t all elements distinct
and unordered listB = [1 2 3 4 5] s.t all elements distinct

original question was:
How do i choose len(listA) times, elements of listB that,
are NOT in listA
and are distinct from eachother.

updated to:
How do i choose len(listB)-len(listA) times, elements of listB that,
are NOT in listA
and are distinct from eachother.

For the updated case, the answer given by Pynchia becomes:
newList = list(set(bRightbLeft) - set(aLeft))[:len(bRightbLeft)-len(aLeft))]

jsky
  • 2,225
  • 5
  • 38
  • 54
  • 1
    please [read this](http://stackoverflow.com/help/mcve) – Pynchia Nov 13 '15 at 13:52
  • not sure where it fails the MVC? – jsky Nov 13 '15 at 13:53
  • please provide a Minimal, Complete, and Verifiable Example – Pynchia Nov 13 '15 at 13:55
  • I would say the logic of the question is a little malformed, as @Pynchia points out: it might not be possible/reasonable to choose `len(a)` times to get distinct elements that are NOT in `a`. Unless you know for a fact that there will be at least `len(a)` distinct, not-in-`a` elements in `b`. That sounds like a bizarre constraint, so more out of curiosity, I'd love to hear the motivating real-world use-case. – dwanderson Nov 13 '15 at 14:38
  • the motivating real world use case is: `A = [1 2 3 4 5]` is an allelle `B = [5 4 3 2 1]` is an allele and i want to cross the allelles at a locus. so i get `a = [1 2]` from A and then make `k = len(a)` distinct selections from B that are not already in A, because for the specific application of the crossover, C needs to be distinct child as its parents are distinct. – jsky Nov 13 '15 at 14:42

2 Answers2

2

this should do the trick, if I understand correctly

listA = [1,2,3]
listB = [1,2,3,4,5]

newlist = list(set(listB) - set(listA))[:len(listA)]
print(newlist)

it produces

[4, 5]

As you can see, there is no guarantee the list has enough number of elements (i.e. the size of listA)

note: initially (and even now partially) the description said the elements in the input lists could be repeated.

In that case, use

listA = [1,2,3,2,1]
listB = [1,2,3,4,5,4,3,2,1]

set_A = set(listA)
newlist = list(set(listB) - set_A)[:len(set_A)]

which produces

[4, 5]
Pynchia
  • 10,996
  • 5
  • 34
  • 43
  • `as` is keyword ? on renaming as and bs i get None result. Im not sure i fully understand your example, thanks for the answer though. Please read my question update. – jsky Nov 13 '15 at 14:17
  • 1
    it makes sense now. its late and i couldnt think of set difference to solve this. gah :( thanks again. – jsky Nov 13 '15 at 14:21
  • see my edit, which solves the problem even for lists with repeated elements – Pynchia Nov 13 '15 at 14:33
  • updated question to match my usecase and to take out vacuities. might make ur answer incorrect. apologies. – jsky Nov 13 '15 at 14:57
1

If you don't care which elements you could do

newList = [ b for b in bList if set(b) not in aList][0:k]
Mr. E
  • 2,070
  • 11
  • 23