1

一组数字

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)

这没有意义。我正在研究一个更复杂的功能,但这是让我失望的一部分。任何帮助将不胜感激

4

2 回答 2

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]]
于 2012-12-02T06:17:04.830 回答
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)
于 2012-12-03T18:09:56.697 回答