split list in sub lists by number of elements - python

In python, if I have the list of elements
l = ['a', 'b', 'c', 'd', 'e', 'f']
and a list of numbers
n = [2, 1, 3]
How I can split the list l by the numbers in n ?
And get this list of lists
[['a', 'b'], ['c'], ['d', 'e', 'f']]

You could use islice:
>>> from itertools import islice
>>> l = ['a', 'b', 'c', 'd', 'e', 'f']
>>> n = [2, 1, 3]
>>> it = iter(l)
>>> out = [list(islice(it, size)) for size in n]
>>> out
[['a', 'b'], ['c'], ['d', 'e', 'f']]

It's a bit obfuscated, but still:
ll = [[l.pop(0) for _ in range(k)] for k in n]
Note that this traversal will not leave the list intact because of the pop() thingy.

You can create an iterator out of the list. Then call next the appropriate number of times.
>>> l = ['a', 'b', 'c', 'd', 'e', 'f']
>>> n = [2, 1, 3]
>>> it = iter(l)
>>> [[next(it) for i in xrange(k)] for k in n]
[['a', 'b'], ['c'], ['d', 'e', 'f']]

Yet another way
if __name__ == '__main__':
l = ['a', 'b', 'c', 'd', 'e', 'f']
n = [2, 1, 3]
result = []
for i in n:
j = l[:i]
result.append(j)
l = l[i:]
print result
Gives
[['a', 'b'], ['c'], ['d', 'e', 'f']]
It's not as short as some other solutions, but it sure as hell is readable

cuts = [sum(n[:i]) for i in range(len(n) + 1)]
>>> [l[cuts[i]:cuts[i + 1]] for i in range(len(cuts) - 1)]
[['a', 'b'], ['c'], ['d', 'e', 'f']]
This leaves the list intact:
>>> l
['a', 'b', 'c', 'd', 'e', 'f']

I think this would be most optimized as it will only required len(n) number of iterations.
l = ['a', 'b', 'c', 'd', 'e', 'f']
n = [2, 1, 3]
res = []
temp = 0
for i in n:
res.append(l[temp:temp+i])
temp = temp+i
print res
Returns:
[['a', 'b'], ['c'], ['d', 'e', 'f']]

You can use numpy.split :
>>> np.split(l,[sum(n[:i]) for i in range(len(n))])
[array([], dtype=float64), array(['a', 'b'],
dtype='|S1'), array(['c'],
dtype='|S1'), array(['d', 'e', 'f'],
dtype='|S1')]

Related

How to efficiently split a list that has a certain periodicity, into multiple lists?

For example the original list:
['k','a','b','c','a','d','e','a','b','e','f','j','a','c','a','b']
We want to split the list into lists started with 'a' and ended with 'a', like the following:
['a','b','c','a']
['a','d','e','a']
['a','b','e','f','j','a']
['a','c','a']
The final ouput can also be a list of lists. I have tried a double for loop approach with 'a' as the condition, but this is inefficient and not pythonic.
One possible solution is using re (regex)
import re
l = ['k','a','b','c','a','d','e','a','b','e','f','j','a','c','a','b']
r = [list(f"a{_}a") for _ in re.findall("(?<=a)[^a]+(?=a)", "".join(l))]
print(r)
# [['a', 'b', 'c', 'a'], ['a', 'd', 'e', 'a'], ['a', 'b', 'e', 'f', 'j', 'a'], ['a', 'c', 'a']]
You can do this in one loop:
lst = ['k','a','b','c','a','d','e','a','b','e','f','j','a','c','a','b']
out = [[]]
for i in lst:
if i == 'a':
out[-1].append(i)
out.append([])
out[-1].append(i)
out = out[1:] if out[-1][-1] == 'a' else out[1:-1]
Also using numpy.split:
out = [ary.tolist() + ['a'] for ary in np.split(lst, np.where(np.array(lst) == 'a')[0])[1:-1]]
Output:
[['a', 'b', 'c', 'a'], ['a', 'd', 'e', 'a'], ['a', 'b', 'e', 'f', 'j', 'a'], ['a', 'c', 'a']]
Firstly you can store the indices of 'a' from the list.
oList = ['k','a','b','c','a','d','e','a','b','e','f','j','a','c','a','b']
idx_a = list()
for idx, char in enumerate(oList):
if char == 'a':
idx_a.append(idx)
Then for every consecutive indices you can get the sub-list and store it in a list
ans = [oList[idx_a[x]:idx_a[x + 1] + 1] for x in range(len(idx_a))]
You can also get more such lists if you take in-between indices also.
You can do this with a single iteration and a simple state machine:
original_list = list('kabcadeabefjacab')
multiple_lists = []
for c in original_list:
if multiple_lists:
multiple_lists[-1].append(c)
if c == 'a':
multiple_lists.append([c])
if multiple_lists[-1][-1] != 'a':
multiple_lists.pop()
print(multiple_lists)
[['a', 'b', 'c', 'a'], ['a', 'd', 'e', 'a'], ['a', 'b', 'e', 'f', 'j', 'a'], ['a', 'c', 'a']]
We can use str.split() to split the list once we str.join() it to a string, and then use a f-string to add back the stripped "a"s. Note that even if the list starts/ends with an "a", this the split list will have an empty string representing the substring before the split, so our unpacking logic that discards the first + last subsequences will still work as intended.
def split(data):
_, *subseqs, _ = "".join(data).split("a")
return [list(f"a{seq}a") for seq in subseqs]
Output:
>>> from pprint import pprint
>>> testdata = ['k','a','b','c','a','d','e','a','b','e','f','j','a','c','a','b']
>>> pprint(split(testdata))
[['a', 'b', 'c', 'a'],
['a', 'd', 'e', 'a'],
['a', 'b', 'e', 'f', 'j', 'a'],
['a', 'c', 'a']]

Replace elements in list based on a dictionary mapping table

I want to replace the elements in a list of lists based on a dictionary mapping table, and tried below:
lists_before = [['A', 'B', 'C'], ['A', 'D'], ['D', 'E']]
mapped_dictionary = {'A': 'G', 'B': 'G', 'C':'F'}
Below is the code I use:
lists_after = []
for element in lists_before:
new_element = []
for letter in element :
if letter in list(mapped_dictionary.values()):
letter = repl_dic.get(letter)
new_element.append(letter)
lists_after.append(new_element)
The output expected for lists_after is:
[['G', 'G', 'F'],['G','D'],['D','E']]
However, the output I got is still the same as lists_before.
I cannot figure out what went wrong. Could someone help me?
You can do it like this:
Input:
l = [['A', 'B', 'C'], ['A', 'D'], ['D', 'E']]
m = {'A': 'G', 'B': 'G', 'C': 'F'}
Code:
l_new = list()
for lst in l:
lst_new = list()
for ele in lst:
lst_new.append(m.get(ele, ele))
l_new.append(lst_new)
Output:
[['G', 'G', 'F'], ['G', 'D'], ['D', 'E']]
Or use a 1-liner:
[[m.get(ele, ele) for ele in lst] for lst in l]
[['G', 'G', 'F'], ['G', 'D'], ['D', 'E']]

Combining elements in a list of list based on having common elements

I have a list of lists like so
list=[[a], [b], [c], [d],[e],[f], [a,f], [b,c], [c,e], [b, d,f]]
note that it includes singles and combinations.
What I would like to do is to iterate the list so that a new list of every possible combination of these sub-components is appended to the list of lists if and only if they share at least one common entry.
so the product is as follows
list2=[[a], [b], [c], [d],[e],[f], [a,f], [b,c], [c,e], [b, d,f], **[b,c,e], [a,b,d,f], [b,c,d,f], [b,c,d,e,f], [a,b,c,d,e,f]]**
Note the new part of the list contains the original list of lists
def similar(x):
prev_l = 0
while prev_l != len(x):
to_add = []
prev_l = len(x)
for i in x:
if len(i) == 1:
continue
for j in x:
if len(j) == 1:
continue
if any([_ in j for _ in i]) and not any([set(i+j) == set(_) for _ in x]) and not any([set(i+j) == set(_) for _ in to_add]) and i != j:
to_add.append(list(set(i+j)))
x += to_add
return x
Input:
>>> l = [['a'], ['b'], ['c'], ['d'],['e'],['f'], ['a','f'], ['b','c'], ['c','e'], ['b', 'd','f']]
>>> similar(l)
Output:
>>> l
[['a'], ['b'], ['c'], ['d'], ['e'], ['f'], ['a', 'f'], ['b', 'c'], ['c', 'e'], ['b', 'd', 'f'], ['b', 'a', 'd', 'f'], ['b', 'e', 'c'], ['b', 'd', 'c', 'f'], ['b', 'a', 'f', 'd', 'c'], ['b', 'f', 'e', 'd', 'c'], ['b', 'a', 'f', 'e', 'd', 'c']]
I should note this has O(n^3) in worst case. If you're using this for something floyd warshall I wouldn't be too worried as that has O(n^3) anyway, but if not you should definitely populate a distance matrix and then look for adjacency in that.

append list of values to sublists

How do you append each item of one list to each sublist of another list?
a = [['a','b','c'],['d','e','f'],['g','h','i']]
b = [1,2,3]
Result should be:
[['a','b','c',1],['d','e','f',2],['g','h','i',3]]
Keep in mind that I want to do this to a very large list, so efficiency and speed is important.
I've tried:
for sublist,value in a,b:
sublist.append(value)
it returns 'ValueError: too many values to unpack'
Perhaps a listindex or a listiterator could work, but not sure how to apply here
a = [['a','b','c'],['d','e','f'],['g','h','i']]
b = [1,2,3]
for ele_a, ele_b in zip(a, b):
ele_a.append(ele_b)
Result:
>>> a
[['a', 'b', 'c', 1], ['d', 'e', 'f', 2], ['g', 'h', 'i', 3]]
The reason your original solution did not work, is that a,b does create a tuple, but not what you want.
>>> z = a,b
>>> type(z)
<type 'tuple'>
>>> z
([['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']], [1, 2, 3])
>>> len(z[0])
3
>>> for ele in z:
... print ele
...
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']] #In your original code, you are
[1, 2, 3] #unpacking a list of 3 elements
#into two values, hence the
#'ValueError: too many values to unpack'
>>> zip(a,b) # using zip gives you what you want.
[(['a', 'b', 'c'], 1), (['d', 'e', 'f'], 2), (['g', 'h', 'i'], 3)]
Here is a simple solution:
a = [['a','b','c'],['d','e','f'],['g','h','i']]
b = [1,2,3]
for i in range(len(a)):
a[i].append(b[i])
print(a)
One option, using list comprehension:
a = [(a[i] + b[i]) for i in range(len(a))]
Just loop through the sublists, adding one item at a time:
for i in range(0,len(listA)):
listA.append(listB[i])
You can do:
>>> a = [['a','b','c'],['d','e','f'],['g','h','i']]
>>> b = [1,2,3]
>>> [l1+[l2] for l1, l2 in zip(a,b)]
[['a', 'b', 'c', 1], ['d', 'e', 'f', 2], ['g', 'h', 'i', 3]]
You can also abuse a side effect of list comprehensions to get this done in place:
>>> [l1.append(l2) for l1, l2 in zip(a,b)]
[None, None, None]
>>> a
[['a', 'b', 'c', 1], ['d', 'e', 'f', 2], ['g', 'h', 'i', 3]]

Move Beginning of List Up To Index To Back in Python

Let's say I had a list:
[a, b, c, d, e, f]
Given an index, say 3, what is a pythonic way to remove everything before
that index from the front of the list, and then add it to the back.
So if I was given index 3, I would want to reorder the list as
[d, e, f, a, b, c]
>>> l = ['a', 'b', 'c', 'd', 'e', 'f']
>>>
>>> l[3:] + l[:3]
['d', 'e', 'f', 'a', 'b', 'c']
>>>
or bring it into a function:
>>> def swap_at_index(l, i):
... return l[i:] + l[:i]
...
>>> the_list = ['a', 'b', 'c', 'd', 'e', 'f']
>>> swap_at_index(the_list, 3)
['d', 'e', 'f', 'a', 'b', 'c']
use the slice operation
e.g.,
myList = ['a', 'b','c', 'd', 'e', 'f']
myList[3:] + myList[:3]
gives
['d', 'e', 'f', 'a', 'b', 'c']
def foo(myList, x):
return myList[x:] + myList[:x]
Should do the trick.
Call it like this:
>>> aList = ['a', 'b' ,'c', 'd', 'e', 'f']
>>> print foo(aList, 3)
['d', 'e', 'f', 'a', 'b', 'c']
EDIT Haha all answers are the same...
The pythonic way it's that's sdolan said, i can only add the inline way:
>>> f = lambda l, q: l[q:] + l[:q]
so, you can use like:
>>> f([1,2,3,4,5,6], 3)
[4, 5, 6, 1, 2, 3]

Categories

Resources