一组浮点数,将数字附加到集合中的空列表。蟒蛇

时间:2012-12-02 06:10:44

标签: python list function append max

一组数字

n_set = [1.0,3.2,4.5,8.2,1.3,2.2,5.6,9.8,2.4,5.5,6.7]

所以我正在尝试构建一个带有一组数字的函数,并从数字集中创建一个列表列表。我试图让每个列表都有一个原始集合中的数字子集,直到达到最大值为止

organized_set = [[1.0,3.2,4.5,8.2],[1.3,2.2,5.6,9.8],[2.4,5.5,6.7]]

我正在思考

的内容
for i,k in zip(range(0,len(n_set)),n_set):
    set = []
    d = dict(zip(i,k))
    d2 = dict(zip(k,i))
    if d[i] < d[d2[i]+1]:
        set.append(k)

这没有意义。我正在研究一个更复杂的功能,但这是它的一部分让我失望。任何帮助都将很乐意欣赏

2 个答案:

答案 0 :(得分:2)

尝试类似这种迭代方法:

n_set = [1.0,3.2,4.5,8.2,1.3,2.2,5.6,9.8,2.4,5.5,6.7]

prev = None
result = []
current = []
for x in n_set:
    if prev is not None and x < prev:
        # Next element is smaller than previous element.
        # The current group is finished.
        result.append(current)

        # Start a new group.
        current = [x]
    else:
        # Add the element to the current group.
        current.append(x)

    # Remember the value of the current element.
    prev = x

# Append the last group to the result.
result.append(current)

print result 

结果:

[[1.0, 3.2, 4.5, 8.2], [1.3, 2.2, 5.6, 9.8], [2.4, 5.5, 6.7]]

答案 1 :(得分:0)

     python 3.2

     temp=[]
     res=[]

     for i in n:
          temp.append(i)
          if len(temp)>1 and i<temp[-2]:
                  res.append(temp[:-1])
                  temp=[i]
     res.append(temp)
     print(res)