I've done my research already and I can't find anything similar. I need to split a list of two lists in two separated lists, for example: [ [[1,2],[2],[3]], [[3], [2,3], [5]] ] -> results in [[1,2],[2],[3]] and [[3], [2,3], [5]] .
I tried this but I can't seem to find where I need to change the code:
split([],[],[]).
split([H|T],X,Y):-
X = H,
Y = T.
You can just unpack the list in two parameters:
split([L1, L2], L1, L2).
Here we thus unify the first parameter with [L1, L2] such that the list of length two is unified in that way that L1 unifies with the first item (sublist), and L2 with the second item (sublist).
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]
First of all, I cannot remember the name of this repetition of list.
I have a list:
myList = [0, 1, 2]
I want to repeat list of list:
[[0,1,2],[1,2,0],...]
I know that I can do permutations myList
But it won't cover the repeated parts such as [[0,0,0],[1,1,1],[1,1,0],...]
So, my questions are what is the name given for such kind of list.
It is not permutations and it definitely is not combinations
In logic, we call it truth table, I believe.
And is there a builtin function for that in haskell?
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
Prelude> :m +Control.Monad
Prelude Control.Monad> replicateM 3 [0,1,2]
[[0,0,0],[0,0,1],[0,0,2],[0,1,0],[0,1,1],[0,1,2],[0,2,0],[0,2,1],[0,2,2],[1,0,0],[1,0,1],[1,0,2],[1,1,0],[1,1,1],[1,1,2],[1,2,0],[1,2,1],[1,2,2],[2,0,0],[2,0,1],[2,0,2],[2,1,0],[2,1,1],[2,1,2],[2,2,0],[2,2,1],[2,2,2]]
Note that basically, the length of the list of permitted values needs in no way be connected to the length of each list of choices.
with list comprehension
x = [0,1,2]
[[a,b,c] | a<-x, b<-x, c<-x]
[[0,0,0],[0,0,1],[0,0,2],[0,1,0],[0,1,1],[0,1,2],[0,2,0],[0,2,1],[0,2,2],
[1,0,0],[1,0,1],[1,0,2],[1,1,0],[1,1,1],[1,1,2],[1,2,0],[1,2,1],[1,2,2],
[2,0,0],[2,0,1],[2,0,2],[2,1,0],[2,1,1],[2,1,2],[2,2,0],[2,2,1],[2,2,2]]
I have a set my_set = ("a","b","c","d","z") and a list my_list=[{"a",0.5},{"c",0.6},{"b",0.9},{"z",0.5},{"m",0.0}]. I would like to have a list with items containing keys in my_set only. In this case the result I would like to have is new_list=[{"a",0.5},{"c",0.6},{"b",0.9},{"z",0.5}]
The list and set is large. Is there an efficient way to accomplish this?
Assuming that that's actually a set and a list of dicts, as stated in the question, you can try this:
In [1]: my_set = set(["a","b","c","d","z"])
In [2]: my_list=[{"a":0.5},{"c":0.6},{"b":0.9},{"z":0.5},{"m":0.0}]
In [3]: [d for d in my_list if all(k in my_set for k in d)]
Out[3]: [{'a': 0.5}, {'c': 0.6}, {'b': 0.9}, {'z': 0.5}]
This simply uses a list comprehension to check that all the keys in the dicts are contained in the set. This will have complexity of O(nm), for n dicts in the list, with m keys each (m being 1 in your case) and assuming that set-lookup is always O(1).
Note, however, that you do not really need a list of dictionaries, since all the keys seem to be different (in this example, at least), so a single dictionary would be enough.
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.