Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将列表拆分为1?_Python - Fatal编程技术网

Python 如何将列表拆分为1?

Python 如何将列表拆分为1?,python,Python,如果我有以下列表: a = [0.0, 0.968792, 1.0, 0.904219, 0.920049, 1.0, 0.738674, 0.760266, 1.0] 我希望: a = [[0.0, 0.968792, 1.0], [0.904219, 0.920049, 1.0], [ 0.738674, 0.760266, 1.0]] 这应该可以完成你的任务。有关进一步说明,请参见代码中的注释: #!/usr/bin/env python3 # coding: utf-8 # sam

如果我有以下列表:

a = [0.0, 0.968792, 1.0, 0.904219, 0.920049, 1.0, 0.738674, 0.760266, 1.0]
我希望:

a = [[0.0, 0.968792, 1.0], [0.904219, 0.920049, 1.0], [ 0.738674, 0.760266, 1.0]]

这应该可以完成你的任务。有关进一步说明,请参见代码中的注释:

#!/usr/bin/env python3
# coding: utf-8

# sample list
a = [0.0, 0.968792, 1.0, 0.904219, 0.920049, 1.0, 0.738674, 0.760266, 1.0]

# empty list to store sublists in
l = list()

# variable to store index (position) of found '1.0' in given list 'a'
last_idx = 0

# walk through list a, i represents the index of the element, e the element itself
for i, e in enumerate(a):
    # find elements equal to 1.0 and append a slice of the list a to the list l
    if e == 1.0:
        l.append(a[last_idx:i+1])
        last_idx = i+1

print(l)
输出如下:

[[0.0, 0.968792, 1.0], [0.904219, 0.920049, 1.0], [0.738674, 0.760266, 1.0]]
已使用命令的文档可在此处找到:


要拆分列表l,在值v之后,您需要在l中找到v的索引。 您可以使用查找第一个v的索引,并使用拆分此索引后的列表l。以下功能将使用上述功能

def split_at(l, v):
    r = []
    while l:                             # while l is not empty
        try:                             # if v is in l
            ind = l.index(v) + 1
            r.append( l[:ind] )
            l = l[ind:]
        except:                          # if v is not in l
            r.append(l)
            l = []
    return r
以下是一些证明正确性的示例:

>>> split_at([], 1)
[]

>>> split_at([1], 1)
[[1]]

>>> split_at([1,1,1,1,1], 1)
[[1], [1], [1], [1], [1]]

>>> split_at([1,2,1], 1)
[[1], [2, 1]]

>>> split_at([1,2,1,1], 1)
[[1], [2, 1], [1]]

>>> split_at([2,1,2], 1)
[[2, 1], [2]]

空列表示例可能被视为不正确,具体取决于需求。

您是否尝试过实现该示例?发生什么事了?1能出现在任何地方吗?
[[0.0, 0.968792, 1.0], [0.904219, 0.920049, 1.0], [0.738674, 0.760266, 1.0]]
def split_at(l, v):
    r = []
    while l:                             # while l is not empty
        try:                             # if v is in l
            ind = l.index(v) + 1
            r.append( l[:ind] )
            l = l[ind:]
        except:                          # if v is not in l
            r.append(l)
            l = []
    return r
>>> split_at([], 1)
[]

>>> split_at([1], 1)
[[1]]

>>> split_at([1,1,1,1,1], 1)
[[1], [1], [1], [1], [1]]

>>> split_at([1,2,1], 1)
[[1], [2, 1]]

>>> split_at([1,2,1,1], 1)
[[1], [2, 1], [1]]

>>> split_at([2,1,2], 1)
[[2, 1], [2]]