I need a list of lists, where entered the first two sublists manually, but the third until 5th list are copies of each other. How can I do the trick having to write the same list three times? This example does not what I want but instead produces a third sublist which is three times as long as I need it:
examplelist=[[1,2],[3,4],3*[5,6,7]]
>>>[[1, 2], [3, 4], [5, 6, 7, 5, 6, 7, 5, 6, 7]]
I want:
>>>[[1, 2], [3, 4], [5, 6, 7], [5, 6, 7], [5, 6, 7]]
You are probably best off doing this in a straightforward way:
l = [[1, 2], [3, 4]]
l.extend([5, 6, 7] for _ in range(3))
If you multiply a list of lists ([[5, 6, 7]] * 3 == [[5, 6, 7], [5, 6, 7], [5, 6, 7]]) you will end up with three references to the same list.
A couple of ways:
>>> [[1,2],[3,4]] + [[5,6,7]]*3
[[1, 2], [3, 4], [5, 6, 7], [5, 6, 7], [5, 6, 7]]
or
>>> examplelist=[[1,2],[3,4]]
>>> examplelist.extend([[5,6,7]]*3)
>>> examplelist
[[1, 2], [3, 4], [5, 6, 7], [5, 6, 7], [5, 6, 7]]
But note that the last 3 lists in the list will be the same:
>>> examplelist[-1][0] = 100
>>> examplelist
[[1, 2], [3, 4], [100, 6, 7], [100, 6, 7], [100, 6, 7]]
probably not what you want.
Python will add lists to get what you expect:
>>> examplelist=[[1,2],[3,4]]
>>> n=3
>>> examplelist+=[[5,6,7] for i in range(n)]
>>> examplelist
[[1, 2], [3, 4], [5, 6, 7], [5, 6, 7], [5, 6, 7]]
You can also create the structure described in one go since expressions are evaluated as the list is constructed:
>>> li=[[1,2,3]] + [[5,6,7] for i in range(n)] + [[7,8,9]]
>>> li
[[1, 2, 3], [5, 6, 7], [5, 6, 7], [5, 6, 7], [7, 8, 9]]
This should work:
>>> from itertools import repeat
>>> ex = [ [1,2], [3,4] ]
>>> ex.extend(repeat([5, 6, 7], 3))
>>> ex
>>> [[1, 2], [3, 4], [5, 6, 7], [5, 6, 7], [5, 6, 7]]
Related
I have three lists as follows.
A = [1, 2, 3]; B = [[3, 4, 5], [4, 5, 6], [4, 5, 7], [7, 4, 3]]; C = [[2, 3, 1], [2, 3, 3], [2, 4, 5], [4, 5, 6], [7, 3, 1]]
I want to create another list containing all the above inner lists starting from A to C.
Desired = [elements of A, elements of B, elements of C] just like this.
Desired = [[1, 2, 3], [3, 4, 5], [4, 5, 6], [4, 5, 7], [7, 4, 3], [2, 3, 1], [2, 3, 3], [2, 4, 5], [4, 5, 6], [7, 3, 1]]
It seems like you know which list is nested (B and C) and which isn't (A). If that's the case then you could simply do:
result = [A] + B + C
If you don't know but can determine the nestedness by looking at the first item, then you could do:
result = [
item
for numbers in (A, B, C)
for item in (numbers if isinstance(numbers[0], list) else [numbers])
]
If that's also not the case, i.e. there could be mixed lists (lists that contain numbers and lists), then you could use itertools.groupby from the standard library:
from itertools import groupby
result = []
for key, group in groupby(A + B + C, key=lambda item: isinstance(item, list)):
if not key:
group = [[*group]]
result.extend(group)
Results for all versions (with A, B and C like provided):
[[1, 2, 3], [3, 4, 5], [4, 5, 6], [4, 5, 7], [7, 4, 3],
[2, 3, 1], [2, 3, 3], [2, 4, 5], [4, 5, 6], [7, 3, 1]]
The method I use below is to see if the inner_list contents are infact a list of themselves.
If they are, then append the inner list.
If they are not, then append the outer_list.
A = [1, 2, 3];
B = [[3, 4, 5], [4, 5, 6], [4, 5, 7], [7, 4, 3]];
C = [[2, 3, 1], [2, 3, 3], [2, 4, 5], [4, 5, 6], [7, 3, 1]]
desired = []
for outer_list in (A,B,C):
list_state = False
for inner_list in outer_list:
if isinstance(inner_list, list):
desired.append(inner_list)
else:
list_state = True
# Add outer_list only if the inner_list was not a list
if list_state:
desired.append(outer_list)
print(desired)
OUTPUT:
[[1, 2, 3], [3, 4, 5], [4, 5, 6], [4, 5, 7], [7, 4, 3], [2, 3, 1], [2, 3, 3], [2, 4, 5], [4, 5, 6], [7, 3, 1]]
I made something to make a list to an nested list if it is not.
A = [1, 2, 3]
B = [[3, 4, 5], [4, 5, 6], [4, 5, 7], [7, 4, 3]]
C = [[2, 3, 1], [2, 3, 3], [2, 4, 5], [4, 5, 6], [7, 3, 1]]
listt = [ 'A', 'B', 'C' ]
des = []
for i in listt:
if type((globals()[i])[0]) != list:
globals()[i] = [[i for i in globals()[i]]] #turns into nested list if not (line 7-9)
for i in (A,B,C):
for x in i:
des.append(x)
print(des)
Output:
[[1, 2, 3], [3, 4, 5], [4, 5, 6], [4, 5, 7], [7, 4, 3], [2, 3, 1], [2, 3, 3], [2, 4, 5], [4, 5, 6], [7, 3, 1]]
How to build data frame out of two lists in a way shown below?
I have tried iterate over lists but I couldn't figure out way add only one element to each list.
like this:
for e in listone:
for list in listtwo:
list.insert(0, e)
Example:
listone = [1, 2, 3]
listtwo = [[4, 5, 6],[7, 8, 9]]
Expected outcome:
[[1, 4, 5, 6],
[1 ,7, 8, 9],
[2, 4, 5, 6],
[2, 7, 8, 9],
[3, 4, 5, 6],
[3, 7, 8, 9]]
A simple list comprehension is enough:
listone = [1, 2, 3]
listtwo = [[4, 5, 6],[7, 8, 9]]
result = [[k, *v] for k in listone for v in listtwo]
print(result)
Output
[[1, 4, 5, 6], [1, 7, 8, 9], [2, 4, 5, 6], [2, 7, 8, 9], [3, 4, 5, 6], [3, 7, 8, 9]]
Let's try a list comprehension on product:
from itertools import product
[[x]+y for x,y in product(listone, listtwo)]
Output:
[[1, 4, 5, 6],
[1, 7, 8, 9],
[2, 4, 5, 6],
[2, 7, 8, 9],
[3, 4, 5, 6],
[3, 7, 8, 9]]
A solution without itertools:
listone = [1, 2, 3]
listtwo = [[4, 5, 6],[7, 8, 9]]
out = []
for i in listone:
for j in listtwo:
out.append([i, *j])
print(out)
Prints:
[[1, 4, 5, 6],
[1, 7, 8, 9],
[2, 4, 5, 6],
[2, 7, 8, 9],
[3, 4, 5, 6],
[3, 7, 8, 9]]
Given a list say [4,5,6,7] i want to write a function that will output
all pairs like ([4,5,7],[6]) ,( [4,5] , [6,7])
length of sub-list are not fixed
i used a for loop and two new lists to append ith element to list one
and append the remaining to list 2 then repeat the process i know this is a silly approach
a = [4,5,6,7]
for x in range(0,len(a)):
b = []
c = []
b.append(a[x])
for y in range(0,len(a)):
if y!=x:
c.append(a[y])
print(b,c)
for x in range(0,len(a)-1):
b = []
c = []
b.append(a[x])
b.append(a[x+1])
for y in range(0,len(a)):
if y!=x and y!=x+1:
c.append(a[y])
print(b,c)
i expect to print all possible sub-lists but always miss the non contiguous ones , i get( [4, 5 ] , [6,7] ) but never generate
([4,6],[5,7])
Here is a solution that does not remove duplicates:
from itertools import combinations
a = [4,4,5,6,7]
result = []
for x in range(len(a) + 1):
for left in combinations(a, x):
right = a.copy()
for ele in left:
right.remove(ele)
result.append([list(left), right])
for res in result:
print(res)
Output:
[[], [4, 4, 5, 6, 7]]
[[4], [4, 5, 6, 7]]
[[4], [4, 5, 6, 7]]
[[5], [4, 4, 6, 7]]
[[6], [4, 4, 5, 7]]
[[7], [4, 4, 5, 6]]
[[4, 4], [5, 6, 7]]
[[4, 5], [4, 6, 7]]
[[4, 6], [4, 5, 7]]
[[4, 7], [4, 5, 6]]
[[4, 5], [4, 6, 7]]
[[4, 6], [4, 5, 7]]
[[4, 7], [4, 5, 6]]
[[5, 6], [4, 4, 7]]
[[5, 7], [4, 4, 6]]
[[6, 7], [4, 4, 5]]
[[4, 4, 5], [6, 7]]
[[4, 4, 6], [5, 7]]
[[4, 4, 7], [5, 6]]
[[4, 5, 6], [4, 7]]
[[4, 5, 7], [4, 6]]
[[4, 6, 7], [4, 5]]
[[4, 5, 6], [4, 7]]
[[4, 5, 7], [4, 6]]
[[4, 6, 7], [4, 5]]
[[5, 6, 7], [4, 4]]
[[4, 4, 5, 6], [7]]
[[4, 4, 5, 7], [6]]
[[4, 4, 6, 7], [5]]
[[4, 5, 6, 7], [4]]
[[4, 5, 6, 7], [4]]
[[4, 4, 5, 6, 7], []]
You can use itertools module to construct all such sublists:
import itertools as it
s = {4,5,6,7}
for length in range(len(s)):
for combination in it.combinations(s, length):
print(list(combination), list(s - set(combination)))
[] [4, 5, 6, 7]
[4] [5, 6, 7]
[5] [4, 6, 7]
[6] [4, 5, 7]
[7] [4, 5, 6]
[4, 5] [6, 7]
[4, 6] [5, 7]
[4, 7] [5, 6]
[5, 6] [4, 7]
[5, 7] [4, 6]
[6, 7] [4, 5]
[4, 5, 6] [7]
[4, 5, 7] [6]
[4, 6, 7] [5]
[5, 6, 7] [4]
You can make a random sublist, then get the elements on the second list that are not iun the random sublist:
def get_from_0_to_len_minus_1_elements_from_original_list():
pass # You implement this
list1 = get_from_0_to_len_minus_1_elements_from_original_list()
list2 = [element for element in original_list if element not in list1]
print(list1, list2)
As to how you partition the original list into random chunks that is up to you to solve
m = [[5,9,1,8],
[2,4,5,7],
[6,3,3,2],
[1,7,6,3]]
rotated_map = []
for i in range(len(m[0])):
rotated_map.append([x[i] for x in m])
print(rotated_map)
"""
my result = [[5, 2, 6, 1], [9, 4, 3, 7], [1, 5, 3, 6], [8, 7, 2, 3]]
desired result = [[8,7,2,3],
[1,5,3,6],
[9,4,3,7],
[5,2,6,1]]
"""
I am trying to rotate the list by putting all the last elements first from the lists into one list then the second to last element into another and so on until i get to the first element.
Transpose the list with zip, then reverse it with the [::-1] syntax.
>>> m = [[5, 9, 1, 8], [2, 4, 5, 7], [6, 3, 3, 2], [1, 7, 6, 3]]
>>> list(map(list, zip(*m)))[::-1]
>>> [[8, 7, 2, 3], [1, 5, 3, 6], [9, 4, 3, 7], [5, 2, 6, 1]]
edit:
If you want pretty printing, it's probably easiest to use numpy arrays all the way.
>>> import numpy as np
>>>
>>> m = [[5, 9, 1, 8], [2, 4, 5, 7], [6, 3, 3, 2], [1, 7, 6, 3]]
>>> m = np.array(m)
>>> m
>>>
array([[5, 9, 1, 8],
[2, 4, 5, 7],
[6, 3, 3, 2],
[1, 7, 6, 3]])
>>>
>>> m.T[::-1]
>>>
array([[8, 7, 2, 3],
[1, 5, 3, 6],
[9, 4, 3, 7],
[5, 2, 6, 1]])
Note that m and m.T[::-1] share the same data, because m.T[::-1] is just another view of m. If you need to duplicate the data, use
result = m.T[::-1].copy()
You could use zip, unpacking your list of lists with the *, and inversing the result with [::-1]:
m = [[5,9,1,8],
[2,4,5,7],
[6,3,3,2],
[1,7,6,3]]
res = [list(i) for i in zip(*m)][::-1]
>>> res
[[8, 7, 2, 3], [1, 5, 3, 6], [9, 4, 3, 7], [5, 2, 6, 1]]
If numpy is an option, transposing is easier:
import numpy as np
>>> np.transpose(m)[::-1]
array([[8, 7, 2, 3],
[1, 5, 3, 6],
[9, 4, 3, 7],
[5, 2, 6, 1]])
# or:
>>> np.flip(np.transpose(m),0)
array([[8, 7, 2, 3],
[1, 5, 3, 6],
[9, 4, 3, 7],
[5, 2, 6, 1]])
You can use numpy module to do it. It has the property to transpose the array. Check the below code:
import numpy as np
m = [[5,9,1,8],
[2,4,5,7],
[6,3,3,2],
[1,7,6,3]]
arr = np.array(m).transpose()
new_list = []
for i in range(arr.shape[0]-1,-1,-1):
new_list.append(list(arr[i]))
print(new_list)
Output:
[[8, 7, 2, 3], [1, 5, 3, 6], [9, 4, 3, 7], [5, 2, 6, 1]]
If you want to rotate the list clockwise:
list(map(list, zip(*m[::-1])))
Else, for anti-clockwise:
list(map(list, zip(*m)))[::-1]
use the reverse keyword
result = [[5, 2, 6, 1], [9, 4, 3, 7], [1, 5, 3, 6], [8, 7, 2, 3]]
result.reverse()
print(result)
output:
[[8, 7, 2, 3], [1, 5, 3, 6], [9, 4, 3, 7], [5, 2, 6, 1]]
I have a nested like this:
[5, 6], [5, 4], [5, 8], [2, 6], [6, 8], [1, 3]
What I want is a list with unique elements joined by the duplicate ones, like this:
[2, 6, 8], [1, 3]
and for
[5, 6], [5, 4], [5, 8]
this:
[5, [6, 4, 8]]
Overall result should look like this:
[[5, [6, 4, 8]], [2, 6, 8], [1, 3]]
What I tried doing was using the following algorithm from this question on the site
pairs_to_link = [[5, 6], [5, 4], [5, 8], [2, 6], [6, 8], [1, 3]]
dict_mapping = dict(pairs_to_link)
final_list = []
for link in dict_mapping.keys() - dict_mapping.values():
links = [link]
while link in dict_mapping:
link = dict_mapping[link]
links.append(link)
final_list.append(links)
print(final_list)
OUTPUT: [[1, 3], [2, 6, 8], [5, 8]]
and it does remove duplicates and also links them but not in a 'tree' manner that I want, i.e. it only works for [1,3],[2,6],[6,8] to make it like this [1,3],[2,6,8]