Making two lists identical in python 2.7 - python-2.7

I have two lists in python.
a=[1,4,5]
b=[4,1,5]
What i need is to order b according to a. Is there any methods to do it so simply without any
loops?

The easiest way to do this would be to use zip to combine the elements of the two lists into tuples:
a, b = zip(*sorted(zip(a, b)))
sorted will compare the tuples by their first element (the element from a) first; zip(*...) will "unzip" the sorted list.

or may be just check everything is perfect then..copy list a for b
if all(x in b for x in a) and len(a)==len(b):
b=a[:]

If you want to make list2 identical to list1, you don't need to mess with order or re-arrange anything, just replace list2 with a copy of list1:
list2 = list(list1)
list() takes any iterable and produces a new list from it, so we can use this to copy list1, thus creating two lists that are exactly the same.
It might also be possible to just do list2 = list1, but do note that this will cause any changes to either to affect the other (as they point to the same object), so this is probably not what you want.
If list2 is referenced elsewhere, and thus needs to remain the same object, it's possible to replace every value in the list using list2[:] = list1.
In general, you probably want the first solution.

Sort b based on items' index in a, with all items not in a at the end.
>>> a=[1,4,5,2]
>>> b=[4,3,1,5]
>>> sorted(b, key=lambda x:a.index(x) if x in a else len(a))
[1, 4, 5, 3]

Related

How to find the union of multiple lists of sub-lists

i have 6 different lists of list similar to
list1=[['hello',1,2,'b3'],['world',1,2,'b4']]
list2=[['yo',4,5,'ba'],['lolz',1,4.35,'b4']]
list3=[['yo',4,5,'ba'],['world',3,4.35,'b6']]
list4=[['test',4,5,'b6'],['test',4,5,'b6']]
they can have around 100 sub-lists in each list but they always have the 4 entries in the sub-list. I want to find all the different sub-list that are all the same and put them into a final list. so it would look something like
final=[['yo',4,5,'ba'],['test',4,5,'b6']]
The pattern is important so the entries in the sub-lists will need to stay in order but the order of the sub-list doesn't matter. what is the best way i could do this? Thank you for your help.
Assuming that there are no unhashable elements of the sublists, I would convert them to tuples and feed them to collections.Counter
from collections import Counter
big_list = [list1, list2, ...]
c = Counter(tuple(sublist) for l in big_list for sublist in l)
final = [list(i) for i in c if c[i] > 1]

Append element to beginning of list in Prolog

How do I append an element to the beginning of a list in Prolog? I need the end result to be called like so:
pushFront(8, [3, 1], NewList). % NewList is now [8, 3, 1].
I have tried to implement it as follows:
pushFront(Item, [], [Item|_]). %Problematic
pushFront(Item, [OldDequeH|OldDequeT], NewDeque) :-
leftPush(OldDequeH, OldDequeT, [Item|NewDeque]).
But it does not work, and I'm out of ideas tbh. Can anyone describe what is wrong with my implementation and what changes it needs to work properly?
To add an element at the beginning of a list, just use list notation:
pushFront(Item, List, [Item|List]).
The list representation uses internally the cons functor (.), so a list [b,c,d] is just syntactic sugar for '.'(b,'.'(c, '.'(d, []))).
This representation allows you to add an item at the front just by wrapping another cons functor, i.e. if you want to a add an item a at the front of a list L you would wrap a '.'(a, L), which we usually write simply as [a|L].

If the set.add() function and list serve for the same purpose in Python?

As what mentioned in the Title, if both of them serve for the same purpose?
Most of the time i will chose to use list, and i don't know when is a better time to use set.add() function.
I try both of them and give me the exact same result...
Personally feel list is better. What do you guys think?
a = set()
a.add('a1')
a.add('a2')
a.add('a3')
for ele in a:
print ele
b = []
b.append('a1')
b.append('a2')
b.append('a3')
for ele in b:
print ele
Please advise...
In terms of general data structures, a set structure tends to allow only one element of each value whereas a list may have more than one of each.
In other words, the pseudo-code set.add(7) executed twice results in the set containing the single element 7 (or an error if it considers adding the same element twice to be invalid).
Using a list instead of a set would result in two elements, both being 7.
For Python specifically, adding duplicates to a set is not an error but it still plainly only allows one of each:
>>> s = set()
>>> s.add(1)
>>> s.add(1)
>>> s.add(2)
>>> s
set([1, 2])
The list on the other hand allows multiples:
>>> l = list()
>>> l.append(1)
>>> l.append(1)
>>> l.append(2)
>>> l
[1, 1, 2]
The reason why you didn't see a difference is simply because you added three unique items to the list and set. In that context, they act the same. Behaviour only diverges when you add duplicate items.

Remove a number in a lis that does not appear in another list - Haskell

I have two lists of numbers. In each are the numbers [1,2,3,4] but in one there is an extra fifth number eg.
list1 = [1,2,3,4]
list2 = [1,2,3,4,5]
I need to enforce that list2 is the same as list1, by either removing the 5th number from list2 or replacing list2 with another copy of list1?
Can this be done?
This question is worded a bit strangely. If you're looking to mutate one of the lists, then the answer is no: that is not possible. Otherwise, you can of course just use list1 instead of list2 anywhere you need it.
Data.List provides the intersect method, which seems exactly what's being requested.
The most easy and performant way to enforce that list2 is the same as list1 would be to say
let list2 = list1 in .......
This would not even require that the list elements are comparable.
[ x | x <- list1, x `elem` list2 ]
and here's another one though I like Louis_Wasserman's solution most
filter (`elem` [1..4]) [1..12]
>>> [1,2,3,4]
or
filter (flip elem [1..4]) [1..12]
>>> [1,2,3,4]
fliter (\x ->x `elem` [1..4]) [1..12]
>>> [1,2,3,4]
if list two is irrelevant why do you use it in the first place ??
and another one came to my mind:
const [1..4] list2
>>> [1,2,3,4]
the last one just ignores the second list and fills in the first.

Prolog Insert list as element into another List

In my program my P = [0,1,2] I want to store it into another LIST, because P will keep changing in a loop so I want to store P into a LIST, so my LIST will be like below :
eg.
LIST = [[0,1,2],[3,4,5],[6,7,8]]
create_list([],[]).
create_list(G, [H|G]).
This is what I did, create_list(P,LIST). I not sure how to do it as it keep return me no. But I am pretty sure I can get different P because I am able to print them out each time P changed.
You need to create a predicate that receives the item (list in this case) you want to append to another input list, and this would give you a new list with the which has all the items of your input list plus the new item.
So, it would be something like:
create_list(Item, List, [Item|List]).
Initially the input List would be an empty list ([]), so you might call it
create_list([0,1,2], [], List1),
create_list([3,4,5], List1, List2),
create_list([6,7,8], List2, List).
This will result in List instantiated with [[0,1,2],[3,4,5],[6,7,8]]