Related
I got list like:
[1,1,5,4,6,6,5]
and I want to drop the element of list, if its get repeated.
Output:
[1,5,4,6,5]
I can only find solution for "normal" Duplicate-Problems.
You can use itertools.groupby and just pull the key for each group.
from itertools import groupby
[k for k, _ in groupby([1,1,5,4,6,6,5])]
# returns:
[1, 5, 4, 6, 5]
You can iterate over pairs with zip and build a new list
lst = [1, 1, 5, 4, 6, 5, 5]
lst = [x for x, y in zip(lst, lst[1:]) if x != y] + [lst[-1]]
print(lst) # [1, 5, 4, 6, 5]
I am trying to use recursion on a nested list. Here I am trying to get the innermost element.
input_list = [1,2,3,4,[5,6,7,[8,9,[10]]]]
list1 = input_list
def sublist1(list, sublist=[]):
if len(list) == 0:
return sublist
else:
sublist.append(list)
sublist1(list[1:], sublist)
print(sublist)
sublist1(list1)
The output I am getting is this:
[[1, 2, 3, 4, [5, 6, 7, [8, 9, [10]]]], [2, 3, 4, [5, 6, 7, [8, 9, [10]]]], [3, 4, [5, 6, 7, [8, 9, [10]]]], [4, [5, 6, 7, [8, 9, [10]]]], [[5, 6, 7, [8, 9, [10]]]]]
I tried changing the index but it's not giving me the expected output [10].
Any help would be appreciated.
You can make a recursive call only if any item in the current given list is a list; otherwise it means the current list is already the innermost list, so you can return the list as is:
def inner_most(lst):
for i in lst:
if isinstance(i, list):
return inner_most(i)
return lst
input_list = [1,2,3,4,[5,6,7,[8,9,[10]]]]
print(inner_most(input_list))
This outputs:
[10]
Demo: https://replit.com/#blhsing/SwelteringFlakyDividend
This will answer your nested travels
input_list = [1,2,3,4,[5,6,7,[8,9,[10]]]]
list1 = input_list
def sublist1(list):
if(len(list) > 1):
for l in list:
#print(l)
# print(type(l))
if type(l) == type([]) :
return sublist1(l)
else:
continue
return list
print(sublist1(list1))
print(list1[-1][-1][-1])
This would get you [10]
Note: I know there is probably an answer for this on StackOverflow already, I just can't find it.
I need to do this:
>>> lst = [1, 2, 3, 4, 5, 6]
>>> first_two = lst.magic_pop(2)
>>> first_two
[1, 2]
>>> lst
[3, 4, 5, 6]
Now magic_pop doesn't exist, I used it just to show an example of what I need. Is there a method like magic_pop that would help me to do everything in a pythonic way?
Do it in two steps. Use a slice to get the first two elements, then remove that slice from the list.
first_list = lst[:2]
del lst[:2]
If you want a one-liner, you can wrap it in a function.
def splice(lst, start = 0, end = None):
if end is None:
end = len(lst)
partial = lst[start:end]
del lst[start:end]
return partial
first_list = splice(lst, end = 2)
One option would be using slices
def func(lst: list, n: int) -> tuple:
return lst[:n], lst[n:]
lst = [1, 2, 3, 4, 5, 6]
first_two, lst = func(lst, 2)
print(lst)
# [3, 4, 5, 6]
Alternative, here is a kafkaesque approach using builtins:
def func(lst: list, i: int) -> list:
return list(map(lambda _: lst.pop(0), range(i)))
lst = list(range(10))
first_two = func(lst, 2)
print(first_two)
# [0, 1]
print(lst)
# [2, 3, 4, 5, 6, 7, 8, 9]
Try:
lst = [1, 2, 3, 4, 5, 6]
first_two, lst = lst[:2], lst[2:]
print(first_two)
print(lst)
Prints:
[1, 2]
[3, 4, 5, 6]
You can just create a lambda function to do this.
magic_pop = lambda x,y:(x[:y],x[y:]) if y<len(lst) else x
magic_pop(lst,3)
Out[8]: ([1, 2, 3], [4, 5, 6])
Im trying to figure out how to take a list of lists of integers and create a new list that contains combinations from the list of lists. I want the combination to start with a value from the first list and then respectively take 1 from each of the subsequent lists, only if the value is greater than the previous list.
l=[[1,2,3],[4,8],[5,10]]
# the answer would look like this
correct=[[1,4,5],[1,4,10],[1,8,10],[2,4,5],[2,4,10],[2,8,10],[3,4,5],[3,4,10],[3,8,10]]
>>> from itertools import product
...
...
... def combos(lst):
... result = []
... for p in product(*lst):
... if all(a < b for a, b in zip(p, p[1:])):
... result.append(list(p))
... return result
...
>>> lst = [[1, 2, 3], [4, 8], [5, 10]]
>>> correct = [[1, 4, 5], [1, 4, 10], [1, 8, 10], [2, 4, 5], [2, 4, 10],
... [2, 8, 10], [3, 4, 5], [3, 4, 10], [3, 8, 10]]
>>> combos(lst) == correct
True
List comprehension is probably a great way to go. It works nicely because of your constraints. You probably want something like:
[[i,j,k] for i in l[0] for j in l[1] if j>i for k in l[2] if k>j]
>>> [[1, 4, 5],
[1, 4, 10],
[1, 8, 10],
[2, 4, 5],
[2, 4, 10],
[2, 8, 10],
[3, 4, 5],
[3, 4, 10],
[3, 8, 10]]
This makes a list of lists of the form [i,j,k] for all the i's in l[0] for all the j's in l[1] if j>i and for all the k's in l[2] if k>j (since we already know that j>i at this point)
However, the code above only works for an input list of list of length 3. Took me a little bit, but this recursive approach should work for a input list of any length
def list_of_lists(in_list):
full_list=[]
def recurse(so_far, l):
if l==len(in_list):
return so_far
next_list = in_list[l]
for i in next_list:
if i>so_far[-1]:
new_list = recurse(so_far.copy()+[i], l+1)
if new_list:
full_list.append(new_list)
for i in in_list[0]:
recurse([i],1)
return full_list
l=[[1,2,3],
[4,8],
[5,10]]
ansList = []
for i in range(len(l[0])):
for j in range(len(l[1])):
for k in range(len(l[2])):
if l[0][i]<l[1][j] and l[1][j]<l[2][k]:
ansList.append([l[0][i],l[1][j],l[2][k]])
print(ansList)
How can i create a multidimensional list while iterating through a 1d list based upon some condition.
I am iterating over a 1d list and whenever i find a '\n' i should append the so created list with a new list, for example,
a = [1,2,3,4,5,'\n',6,7,8,9,0,'\n',3,45,6,7,2]
so I want it to be as,
new_list = [[1,2,3,4],[6,7,8,9,0],[3,45,6,7,2]]
how should i do it? Please help
def storeData(k):
global dataList
dlist = []
for y in k:
if y != '\n':
dlist.append(y)
else:
break
return dlist
This is what i have tried.
example code:
lst = [[]]
for x in a:
if x != '\n':
lst[-1].append(x)
else:
lst.append([])
print(lst)
output:
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 0], [3, 45, 6, 7, 2]]
Using itertools.groupby would do the job (grouping by not being a linefeed):
import itertools
a = [1,2,3,4,5,'\n',6,7,8,9,0,'\n',3,45,6,7,2]
new_list = [list(x) for k,x in itertools.groupby(a,key=lambda x : x!='\n') if k]
print(new_list)
We compare the key truth value to filter out the occurrences of \n
result:
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 0], [3, 45, 6, 7, 2]]
This is how I did it but there must be better solutions.
x = 0
output = [[]]
for item in a:
if item != "\n":
output[x].append(item)
else:
x += 1
output.append([])
print(output)
Here is the basic approach:
EDIT: Good heavens! Silly bug... here's a fix:
>>> sub = []
>>> final = []
>>> for e in a:
... if e == '\n':
... final.append(sub)
... sub = []
... else:
... sub.append(e)
... else:
... final.append(sub)
...
>>> final
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 0], [3, 45, 6, 7, 2]]
>>>
There are other ways, but this is the naive imperative way.