0

次のような値を含むファイルを読んでいます。

-0.68285 -6.919616
-0.7876 -14.521115
-0.64072 -43.428411
-0.05368 -11.561341
-0.43144 -34.768892
-0.23268 -10.793603
-0.22216 -50.341101
-0.41152 -90.083377
-0.01288 -84.265557
-0.3524 -24.253145

ビン幅が 0.1 の列 1 の値に基づいて、これを個々の配列に分割するにはどうすればよいですか?

次のような出力が必要です。

array1=[[-0.05368, -11.561341],[-0.01288, -84.265557]]
array2=[[-0.23268, -10.79360] ,[-0.22216, -50.341101]]
array3=[[-0.3524, -24.253145]]
array4=[[-0.43144, -34.768892], [-0.41152, -90.083377]]
array5=[[-0.68285, -6.919616],[-0.64072, -43.428411]]
array6=[[-0.7876, -14.521115]]
4

1 に答える 1

0

Python のラウンド関数とディクショナリ クラスを使用した簡単なソリューションを次に示します。

lines = open('some_file.txt').readlines()

dictionary = {}
for line in lines:
    nums = line[:-1].split(' ')     #remove the newline and split the columns
    k = round(float(nums[0]), 1)    #round the first column to get the bucket
    if k not in dictionary:         
        dictionary[k] = []          #add an empty bucket
    dictionary[k].append([float(nums[0]), float(nums[1])])
                                    #add the numbers to the bucket
print dictionary    

特定のバケット (.3 など) を取得するには、次のようにします。

x = dictionary[0.3]

また

x = dictionary.get(0.3, [])

空のバケットに対して空のリストを返すだけの場合。

于 2012-07-06T14:29:38.293 に答える