Related
I have two lists I want to merge into one like so:
list_0=[[5], [4], [4], [1], [0], [7], [8], [3], [0]]
list_1=[[0], [9], [6], [4], [5], [9], [5], [9], [3]]
mine_position=[[5], [0], [4], [9], [4], [6], [1], [4], [0], [5], [7], [9], [8], [5], [3], [9], [0], [3]]
The code I have for merging them is:
for i in row:
mine_position.append(i)
mine_position.insert(1, list_1[0])
mine_position.insert(3, list_1[1])
mine_position.insert(5, list_1[2])
mine_position.insert(7, list_1[3])
mine_position.insert(9, list_1[4])
mine_position.insert(11, list_1[5])
mine_position.insert(13, list_1[6])
mine_position.insert(15, list_1[7])
mine_position.insert(17, list_1[8])
I want to shorten it so that the second part can be cleaner, but I can't seem to find a method that works.
You can just zip them together. No loops needed.
list_0=[[5], [4], [4], [1], [0], [7], [8], [3], [0]]
list_1=[[0], [9], [6], [4], [5], [9], [5], [9], [3]]
mine_position = list(zip(list_0, list_1))
We can see that your merged list's elements at even indices consists of elements from list_0 and the odd indices consists of elements from list_1. The below code snippet can be used to generate the merged list.
Code Snippet:
# Original Lists
list_0=[[5], [4], [4], [1], [0], [7], [8], [3], [0]]
list_1=[[0], [9], [6], [4], [5], [9], [5], [9], [3]]
# Merged List
result = []
total_elements = len(list_0) + len(list_1)
# Indices of respective lists
list0_idx = list1_idx = 0
for i in range(total_elements):
if i % 2 == 0:
result.append(list_0[list0_idx])
list0_idx += 1
else:
result.append(list_1[list1_idx])
list1_idx += 1
print(result)
How about this?
list_0=[[5], [4], [4], [1], [0], [7], [8], [3], [0]]
list_1=[[0], [9], [6], [4], [5], [9], [5], [9], [3]]
merged_list = []
for i in range(0, len(list_0)):
merged_list.append(list_0[i])
merged_list.append(list_1[i])
print(merged_list)
It shows this.
[[5], [0], [4], [9], [4], [6], [1], [4], [0], [5], [7], [9], [8], [5], [3], [9], [0], [3]]
You can first create a new variable and assign it as list_0. We do this so that list_0 is not altered. Then use a loop and insert the element of list_1 every 2 elements.
list_0=[[5], [4], [4], [1], [0], [7], [8], [3], [0]]
list_1=[[0], [9], [6], [4], [5], [9], [5], [9], [3]]
merged_list = list_0
idx = 1
for j in list_1:
merged_list.insert(idx,j)
idx += 2
print(merged_list)
#Result: [[5], [0], [4], [9], [4], [6], [1], [4], [0], [5], [7], [9], [8], [5], [3], [9], [0], [3]]
NOTE the reason we increment it by 2, is because at the start of the iteration, a new element will be added into the merged_list.
Using list comprehension:
[item for t in [(list_0[i], list_1[i]) for i in range(len(list_0))] for item in t]
I have a nested list say:
lst = [[1,2,3,4], [2,3,4,5], [3,4,5,6]]
And I would like the output to be:
new_list = [[[1], [2], [3], [4]], [[2], [3], [4], [5]], [[3], [4], [5], [6]]]
this is what i am thinking, but I it is outputting a flat, list of list.
new_lst = []
for i in lst:
i = list(i)
for el in i:
new_list.append([el])
print(new_lst)
I would like to maintain the length of each predefined list in lst
Try List comprehension
[[ [e] for e in l] for l in lst]
You could use list comprehension and append every element to another list.
lst = [[1,2,3,4], [2,3,4,5], [3,4,5,6]]
new = [[[numbers] for numbers in elements] for elements in lst]
The above example adjusts for your desired output.
[[[1], [2], [3], [4]], [[2], [3], [4], [5]], [[3], [4], [5], [6]]]
You can use numpy's reshape np.reshape
>>> import numpy as np
>>> lst = np.array([[1,2,3,4], [2,3,4,5], [3,4,5,6]])
>>> lst
array([[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6]])
>>> lst.shape
(3, 4)
>>> lst.reshape(3,4,1)
array([[[1],
[2],
[3],
[4]],
[[2],
[3],
[4],
[5]],
[[3],
[4],
[5],
[6]]])
>>> lst.reshape(3,4,1).tolist()
[[[1], [2], [3], [4]], [[2], [3], [4], [5]], [[3], [4], [5], [6]]]
Another version, using recursion (you can have more level of depths in your input):
lst = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]
def split(i):
if isinstance(i, list):
return [split(v) for v in i]
else:
return [i]
print(split(lst))
Prints:
[[[1], [2], [3], [4]], [[2], [3], [4], [5]], [[3], [4], [5], [6]]]
You could use helper function to have more flexibility later of how you divide the smaller lists. based on an answer from here.
def split_list(alist, wanted_parts=1):
length = len(alist)
return [ alist[i*length // wanted_parts: (i+1)*length // wanted_parts]
for i in range(wanted_parts) ]
lst = [[1,2,3,4], [2,3,4,5], [3,4,5,6]]
ret =[]
for mini_list in lst:
ret.append(split_list(mini_list, len(mini_list)))
I think you had the right idea using multiple for loops. This should work:
list = [[1,2,3,4], [2,3,4,5], [3,4,5,6]]
for i in range(0, 3):
for j in range(0, 4):
(list[i])[j] = [(list[i])[j]]
How can I take a nested List and group it into a tuple of three without importing any module. Please show the expanded for loops so I can understand it better.
For example, I want this nested List. Note this will always multiples of
3 sub lists so there is not a index error.
Thankyou.
[[1], [2], [3], [4], [5], [6], [7], [8], [9]] # Given
[(1, 2, 3), (4, 5, 6), (7, 8, 9)] # Result Wanted
No need for indexes. You can use next() on an iterator even inside a for loop:
xss = [[1], [2], [3], [4], [5], [6], [7], [8], [9]]
it = iter(xss)
answer = []
for x, in it:
answer.append((x, next(it)[0], next(it)[0]))
You can slice with a step size of 3 and zip to make the triples, do a nested unpacking in the for loop, and rebuild the triples without the wrapping lists.
xss = [[1], [2], [3], [4], [5], [6], [7], [8], [9]]
it = zip(xss[::3], xss[1::3], xss[2::3])
answer = []
for [x], [y], [z] in it:
answer.append((x, y, z))
given = [[1], [2], [3], [4], [5], [6], [7], [8], [9]] # because this is a list within a list
output = []
for i in range(0, len(given),3): # step size 3 indicated
temp = (given[i][0], given[i+1][0], given[i+2][0]) # therefore you need the "[0]" behind each given[i]
output.append(temp)
print (output)
>>> print np.array([np.arange(10)]).transpose()
[[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]]
Is there a way to get a vertical arange without having to go through these extra steps?
You can use np.newaxis:
>>> np.arange(10)[:, np.newaxis]
array([[0],
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9]])
np.newaxis is just an alias for None, and was added by numpy developers mainly for readability. Therefore np.arange(10)[:, None] would produce the same exact result as the above solution.
Edit:
Another option is:
np.expand_dims(np.arange(10), axis=1)
numpy.expand_dims
I would do:
np.arange(10).reshape((10, 1))
Unlike np.array, reshape is a light weight operation which does not copy the data in the array.
This follows on from this question:
Algorithm to generate spanning set
Given this input: [1,2,3,4]
I'd like to generate this set of sets in python:
[1] [2] [3] [4]
[1] [2] [3,4]
[1] [2, 3, 4]
[1] [2,3] [4]
[1,2] [3] [4]
[1,2] [3,4]
[1,2,3] [4]
[1,2,3,4]
So unlike the previous question, the order of the list is retained.
Ideally the code would work for n items in the list
Thanks very much
EDIT 2: Could anyone advise me on how to do this if the original input is a string rather than a list (where each word in the string becomes an item in a list). Thanks!
EDIT: added [1] [2, 3, 4] Sorry for the mistake
You might also enjoy a recursive solution:
def span(lst):
yield [lst]
for i in range(1, len(lst)):
for x in span(lst[i:]):
yield [lst[:i]] + x
Explanation
We exploit recursion here to break the problem down. The approach is the following:
For every list, the whole list is a valid spanning: [1,2,3,4] => [[1,2,3,4]].
For every list that is longer than size 1, we can use the first item as a group and then apply the same algorithm on the remaining list to get all the combined results:
[1,2,3] =>
[[1]] + [[2], [3]] # => [[1], [2], [3]]
[[1]] + [[2,3]] # => [[1], [2,3]]
For every list that is longer than size 2, we can just as well use the first two items as a group and then apply the same algorithm on the remaining list and combine the results:
[1,2,3,4,5] =>
[[1,2]] + [[3], [4], [5]] # => [[1,2], [3], [4], [5]]
[[1,2]] + [[3,4], [5]] # => [[1,2], [3,4], [5]]
[[1,2]] + [[3], [4,5]] # => [[1,2], [3], [4,5]]
[[1,2]] + [[3,4,5]] # => [[1,2], [3,4,5]]
We can see that the possible combinations on the right side are indeed all possible groupings of the remainder of the list, [3,4,5].
For every list that is longer than ... etc. Thus, the final algorithm is the following:
yield the whole list (it is always a valid spanning, see above)
For every possible splitting of the list, yield the left-hand part of the list combined with all possible spannings of the right-hand part of the list.
yield is a special keyword in Python that make the function a generator, which means that it returns a iterable object that can be used to enumerate all results found. You can transform the result into a list using the list constructor function: list(span([1,2,3,4])).
Adjusting one of the solution from Python: show all possible groupings of a list:
from itertools import combinations
def cut(lst, indexes):
last = 0
for i in indexes:
yield lst[last:i]
last = i
yield lst[last:]
def generate(lst, n):
for indexes in combinations(list(range(1,len(lst))), n - 1):
yield list(cut(lst, indexes))
data = [1,2,3,4]
for i in range(1, len(data)+1): # the only difference is here
for g in generate(data, i):
print(g)
"""
[[1, 2, 3, 4]]
[[1], [2, 3, 4]]
[[1, 2], [3, 4]]
[[1, 2, 3], [4]]
[[1], [2], [3, 4]]
[[1], [2, 3], [4]]
[[1, 2], [3], [4]]
[[1], [2], [3], [4]]
"""
import itertools
a = [1, 2, 3, 4]
n = len(a)
for num_splits in range(n):
for splits in itertools.combinations(range(1, n), num_splits):
splices = zip([0] + list(splits), list(splits) + [n])
print([a[i:j] for i, j in splices])
prints
[[1, 2, 3, 4]]
[[1], [2, 3, 4]]
[[1, 2], [3, 4]]
[[1, 2, 3], [4]]
[[1], [2], [3, 4]]
[[1], [2, 3], [4]]
[[1, 2], [3], [4]]
[[1], [2], [3], [4]]