0

This is list of tile placements. Each integer stands for an id of a tile. Each time an integer is added to a new list it means that a new tile is placed. When a tile is removed, the last integer is removed from a new list. I want that every time a tile is placed the list to be unique. A list dont have to be unique, when a tile is removed. The code is placing these tiles in a for loop. So for this example, the last list of the lists is wrong, because it wasn't unique when a tile was placed. Is there a way to exclude numbers which will make the new list not unique. So for this example is there a way to exclude the id 18, before it adds to the list.

I know this is a very vague question, but I am new to python and can't make the code of this assignment easier. I hope someone could help me with this vague question

    [[1, 2, 3, 13, 4, 5, 6, 7, 17], 
    [1, 2, 3, 13, 4, 5, 6, 7, 17, 8], 
    [1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15],
    [1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9],
    [1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 10],
    [1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 10, 18], 
    [1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 11], 
    [1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 11, 18], 
    [1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 10], 
    [1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 10, 18]] 

The lists must be in this order. So for example i have had these lists:

[[1, 2, 3, 13, 4, 5, 6, 7, 17], 
[1, 2, 3, 13, 4, 5, 6, 7], 
[1, 2, 3, 13, 4, 5, 6, 7, 8],
[1, 2, 3, 13, 4, 5, 6, 7],
[1, 2, 3, 13, 4, 5, 6, 7, 19],
[1, 2, 3, 13, 4, 5, 6, 7]]

I want to exlude the ids 17,8,19

So for [1, 2, 3, 13, 4, 5, 6, 7] the output must look like this ( id ont care if the output is a list or integers)

[17,8,19]

But when i have this list [1, 2, 3, 13, 4, 5, 6] in lists

[[1, 2, 3, 13, 4, 5, 6, 7, 17], 
[1, 2, 3, 13, 4, 5, 6], 
[1, 2, 3, 13, 4, 5, 6, 7, 8],
[1, 2, 3, 13, 4, 5, 6, 7],
[1, 2, 3, 13, 4, 5, 6, 7, 19],
[1, 2, 3, 13, 4, 5, 6, 7]]

The output is this:

[7]

I hope this will make it more clear.

L.B
  • 139
  • 2
  • 12
  • set(list) - is it what you want? set returns unique list(removes duplicates from the list) – pivanchy Jan 24 '16 at 18:57
  • You mention "assignment" So I will give you hints: Look up sets and Counters and dicts. – dawg Jan 24 '16 at 18:57
  • The lists must be in this order. So for example i have had these lists: [[1, 2, 3, 13, 4, 5, 6, 7, 17], [1, 2, 3, 13, 4, 5, 6, 7], [1, 2, 3, 13, 4, 5, 6, 7, 8], [1, 2, 3, 13, 4, 5, 6, 7], [1, 2, 3, 13, 4, 5, 6, 7, 19], [1, 2, 3, 13, 4, 5, 6, 7]], I want to exlude the ids 17,8,19 – L.B Jan 24 '16 at 18:59
  • if `latestlist == list[n]: fixTheProblem()` – Matt Coubrough Jan 24 '16 at 19:01
  • http://stackoverflow.com/a/29639138/566035 here, you can find many ways to remove duplicates from a list. – otterb Jan 24 '16 at 19:03
  • i dont think it is a duplicate problem. i want to recognize the 17, 8 and 19 when i have the id list [1, 2, 3, 13, 4, 5, 6, 7]. The reason that I ask this is to backtrack the tile placements. Otherwise i will be stuck in an infinte loop – L.B Jan 24 '16 at 19:08
  • Is your lists sorted or needs sorting? – Learner Jan 24 '16 at 19:54
  • it isnt sorted and it doesnt need to be sorted – L.B Jan 24 '16 at 19:55
  • I see you need to extract only FIRST extended part of each list but it needs sorted beforehand – Learner Jan 24 '16 at 19:58
  • Edited question please see- need to sleep.. – Learner Jan 24 '16 at 20:43

3 Answers3

1

I tried with itertools and collections- pass a list, a list element index and to be added value to the adder function if uniquness is kept the adder will add that passed value otherwise return intact list.compare_func return TRUE if list is unique using all.

import collections,itertools
compare_func = lambda x, y: collections.Counter(x) != collections.Counter(y)
lst = [[1, 2, 3],[1, 2, 3,4]] 

def adder(mylist,indx,val):
    mylist[indx].append(val)   
    if  all([compare_func(*i) for i in list(itertools.combinations(lst,2))]):
        print "Added item"
    else:
        print "Did not add item"
        mylist[indx].pop()
    return mylist

Now run print adder(lst,0,4)

Output-

Did not add item
[[1, 2, 3], [1, 2, 3, 4]]

But if run

print adder(lst,1,4)

Output-

Added item
[[1, 2, 3], [1, 2, 3, 4, 4]]

EDIT

After OP cleared question i added this portion-

Try using set as below-

import collections,itertools

data = [[1, 2, 3, 13, 4, 5, 6, 7, 17], 
        [1, 2, 3, 13, 4, 5, 6, 7], 
        [1, 2, 3, 13, 4, 5, 6, 7, 8],
        [1, 2, 3, 13, 4, 5, 6, 7],
        [1, 2, 3, 13, 4, 5, 6, 7, 19],
        [1, 2, 3, 13, 4, 5, 6, 7]]


interscntion = set.intersection(*map(set,data))

d = collections.Counter([i for j in data for i in j if i not in list(interscntion)])
if len(set(it[1] for it in d.most_common()))>1:
    print [max(d.most_common(),key=lambda x:x[1])[0]]
else:
    print [j[0] for j in d.most_common()]

Output-

[8, 17, 19]
Learner
  • 5,192
  • 1
  • 24
  • 36
0

Here you go:

def tiles(arrOfArrs):
    final = []
    for x in arrOfArrs:
        final += x
    return list(set(final))
AMACB
  • 1,290
  • 2
  • 18
  • 26
  • This is not exactly my problem. The lists doesnt have to be ordered. in my example, it is more that i want to search for the integers after the 1, 2, 3, 13, 4, 5, 6, 7. In that way i can exclude the numbers after this. so that the lists will be unique. – L.B Jan 24 '16 at 19:25
0

Keep another list of lists and at each position add the tiles that appear at that index in any of the primary lists.

Tomas Walch
  • 2,245
  • 1
  • 14
  • 17