I have a python list as follows:
list_ = [-100, 1, 3, 5, 7, 100]
I want to create another list such as below; (Note that the last 100
is not included!!)
new_list = [[-100], [-100, 1], [-100, 1, 3], [-100, 1, 3, 5], [-100, 1, 3, 5, 7]]
I tried as follows:
new_list = []
for i in list_:
new_list.append(list([i]))
It generates [[-100], [1], [3], [5], [7], [100]]
, which is not the one I want.
Is this want you want?
l = [-100, 1, 3, 5, 7, 100]
print([l[:i+1] for i in range(0, len(l) - 1)])
Output:
[[-100], [-100, 1], [-100, 1, 3], [-100, 1, 3, 5], [-100, 1, 3, 5, 7]]
Or even simpler:
print([l[:i] for i in range(1, len(l))])
This is probably what you meant:
list_ = [-100, 1, 3, 5, 7, 100]
new_list = []
for i in range(1, len(list_)):
new_list.append(list_[:i])
print(new_list)
# outputs [[-100], [-100, 1], [-100, 1, 3], [-100, 1, 3, 5], [-100, 1, 3, 5, 7]]
In each iteration, we take the list slice up to index i
, starting at index 0 up until len(list_) - 1
, using range
to generate the indexes.
What you probably want to do here is work with list slices like so:
new_list = []
for i in range(1,len(list_)):
new_list.append(list_[:i])
printing the list produced by this loop yields:
[[-100], [-100, 1], [-100, 1, 3], [-100, 1, 3, 5], [-100, 1, 3, 5, 7]]
For more details on how list slicing works, see the slicing section in the informal introduction to Python here: https://docs.python.org/3/tutorial/introduction.html
What about a recursive function ?
list_ = [-100, 1, 3, 5, 7, 100]
matrix_ = []
def matrix(lst):
if not lst:
return
length = len(lst)-1
if length:
matrix_.append(lst[:length])
matrix(lst[:length])
x = matrix(list_)
print(matrix_)
[[-100, 1, 3, 5, 7], [-100, 1, 3, 5], [-100, 1, 3], [-100, 1], [-100]]
Then reverse it if you want:
matrix_.reverse()
print(matrix_)
# [[-100], [-100, 1], [-100, 1, 3], [-100, 1, 3, 5], [-100, 1, 3, 5, 7]]
1. Using a While loop
list_ = [-100, 1, 3, 5, 7, 100]
def matrix(lst):
matrix_ = []
count = 0
prev = []
while count < len(lst)-1:
prev.append(lst[count])
matrix_.append(prev.copy())
count += 1
return matrix_
x = matrix(list_)
print(x)
# [[-100], [-100, 1], [-100, 1, 3], [-100, 1, 3, 5], [-100, 1, 3, 5, 7]]
2. Using Generator Function
list_ = [-100, 1, 3, 5, 7, 100]
def matrix(lst):
matrix_ = []
count = 0
prev = []
while count < len(lst)-1:
prev.append(lst[count])
matrix_.append(prev.copy())
count += 1
yield matrix_
x = list(matrix(list_))
print(x[0])
# [[-100], [-100, 1], [-100, 1, 3], [-100, 1, 3, 5], [-100, 1, 3, 5, 7]]
3. Enumerate
list_ = [-100, 1, 3, 5, 7, 100]
def matrix(lst):
result = []
for idx, _ in enumerate(lst):
if idx:
list_block = lst[:idx]
result.append(list_block)
return result
x = matrix(list_)
print(x)
# [[-100], [-100, 1], [-100, 1, 3], [-100, 1, 3, 5], [-100, 1, 3, 5, 7]]
list_ = [-100, 1, 3, 5, 7, 100]
def matrix(lst):
result = []
for i in range(len(lst)):
if i:
list_block = lst[:i]
result.append(list_block)
return result
x = matrix(list_)
print(x)
# [[-100], [-100, 1], [-100, 1, 3], [-100, 1, 3, 5], [-100, 1, 3, 5, 7]]
5. Using itertools takewhile just for fun
from itertools import takewhile
list_ = [-100, 1, 3, 5, 7, 100]
def make_list(item):
matrix = []
for i in range(1, len(item)):
wanted_items = list(takewhile(lambda x: x != item[-i], item))
matrix.append(wanted_items)
matrix.reverse()
return matrix
result = make_list(list_)
print(result)
# [[-100], [-100, 1], [-100, 1, 3], [-100, 1, 3, 5], [-100, 1, 3, 5, 7]]
try this
lst=[-100,1,3,5,7,100]
new_list = []
for i in range(len(lst):
new_list.append(lst[:i])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.