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 に答える