繁体   English   中英

从 python 列表创建子列表

[英]create a sublist from a python list

我有一个 python 列表如下:

list_ = [-100, 1, 3, 5, 7, 100]

我想创建另一个列表,如下所示; (注意最后100不包括在内!!)

new_list = [[-100], [-100, 1], [-100, 1, 3], [-100, 1, 3, 5], [-100, 1, 3, 5, 7]]

我尝试如下:

new_list = []

for i in list_:
    new_list.append(list([i]))

它生成[[-100], [1], [3], [5], [7], [100]] ,这不是我想要的。

这是你想要的吗?

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]]

或者更简单:

print([l[:i] for i in range(1, len(l))])

这可能是你的意思:

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]]

在每次迭代中,我们将列表切片直到索引i ,从索引 0 开始直到len(list_) - 1 ,使用range生成索引。

您可能想要在这里做的是使用这样的列表切片:

new_list = []

for i in range(1,len(list_)):
    new_list.append(list_[:i])
    

打印此循环生成的列表会产生:

[[-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

递归 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_)

output

[[-100, 1, 3, 5, 7], [-100, 1, 3, 5], [-100, 1, 3], [-100, 1], [-100]]

如果需要,然后将其反转:

matrix_.reverse()
print(matrix_)
# [[-100], [-100, 1], [-100, 1, 3], [-100, 1, 3, 5], [-100, 1, 3, 5, 7]]

额外的

1. 使用 While 循环

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.使用发电机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.枚举

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]]

4. 范围 function

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. 使用 itertools takewhile只是为了好玩

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]]

尝试这个

lst=[-100,1,3,5,7,100]
new_list = []

for i in range(len(lst):
    new_list.append(lst[:i])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM