2012-03-20 8 views
2

私は、このようなファイルがあります:別のリストからの情報を使用して数値のリストを生成

100 2 
300 3 
50 1 
500 5 

を、私は最初の数は数のoccuranceある番号のプールを生成したいですリストはx回発生する必要があります(xはリストの2番目の数字です)。

これは私が出力になりたいものです。

[100, 100, 300, 300, 300, 50, 500, 500, 500, 500, 500] 

私はこのような関数を書いた:

def Pool(pos, count): 
    pool = pos*int(count) 
    return pool 

を、各行のために、私はbigpoolという変数にすべての数字を付加し

bigpool = [] 
for line in weightposfile: 
    line = line.rstrip() 
    f = line.split('\t') 
    pos = f[0] 
    count = int(f[1]) 
    pool = Pool(pos, count) 
    bigpool.append(pool) 

しかし、これは次のようなリストを返します:

[100100, 300300300, 50, 500500500500500] 

数字を分けて、私が望む出力を得るにはどうすればいいですか(上記参照)?

+0

Pythonの規約では、クラス名に大文字の大文字のみを使用することに留意してください。それは単なる大会ですが、ほとんどの設定であなたのような機能のために 'pool'が期待される名前になります。 –

+0

そのコードでその出力を生成することはできません。あなたの "pos"は文字列なので、出力は '['100100'、 '300300300'、 '50'、 '500500500500500']'のようになります。コードをコピーして貼り付け、コメントを出力してください。 – DSM

+0

あなたはそうですね、それはそのように見えます。ごめんなさい!次回は貼り付けてコピーします。 – edg

答えて

3

これは動作するはずです:

def Pool(pos, count): 
    return [pos] * count 

bigpool = [] 
for line in weightposfile: 
    line = line.rstrip() 
    f = line.split('\t') 
    pos = f[0] 
    count = int(f[1]) 
    pool = Pool(pos, count) 
    bigpool += pool 

私は2つの行を変更しました。 return [pos] * countposの配列を作成します。

bigpool += poolは、poolの要素をbigpoolに追加します。

+0

ありがとう!うまく動作します。 – edg

+0

私はこのbigpool xから多くの値を1000回サンプリングしたいと考えています: – edg

+0

randomsample = random.sample(bigpool、x)、それを1000回行う方法は分かりませんか? – edg

0
def Pool(pos, count): 
    pool = [int(pos) for x in range(int(count))] 
    return pool 
0

あなたはとても近いです!ただ、やる:

bigpool = [] 
for line in weightposfile: 
    line = line.rstrip() 
    f = line.split('\t') 
    pos = [] 
    pos.append(f[0]) 
    count = int(f[1]) 
    pool = Pool(pos, count) 
    bigpool.extend(pool) 

乗算を整数aリストは、リストの各要素のa回を追加します。

0

これはいかがですか?

fromfile = "100 2\n300 3\n50 1\n500 5" 
result = [] 
for entry in fromfile.split("\n"): 
    num, count = entry.split() 
    for i in range(int(count)): 
     result.append(num) 
print result 
0

それが期待どおりに動作し、少し簡単ですが、この実装を試してみてください:

def pool(pos, count): 
    return [pos] * int(count) 

bigpool = [] 
for line in weightposfile: 
    pos, count = line.strip().split() 
    bigpool.extend(pool(pos, count)) 
0

ちょうどあなたが可変間隔を持っている場合には、これは何が必要に動作するはずです:

import re 
results = [] 
pre = re.compile('^(\d+)\s+(\d+)',re.M) 

for line in weightposfile.split("\n"): 
    matchline = pre.match(line) 
    for i in range(int(matchline.group(1))): 
     results.append(matchline.group(0)) 
print results 
1

list comprehensionitertools.repeat()の機能でこれを行うことができます。私たちを与える

from itertools import repeat, chain 
with open("file.dat", "r") as f: 
    output = list(chain.from_iterable(repeat(int(number), int(count)) for (number, count) in (line.split() for line in f))) 
print(output) 

[100, 100, 300, 300, 300, 50, 500, 500, 500, 500, 500] 

は今、これはかなり複雑なリスト内包(まあ、技術的には発電機の理解)ですので、のはそれを打破しましょう。最初にファイルを開くことから始めます(ベストプラクティスとして withステートメントを使用します)。最初に行うことは、すべての行を取り出し、空白で分割して、数字、カウントのペアのリストを提供することです。私たちは今、リピートジェネレータ(リストの本質的リスト)の発電機を持っているので、我々はこれらを展開する

repeat(int(number), int(count)) for (number, count) in ... 

(line.split() for line in f) 

我々はそれらのペアを取り、数を所定の回数を繰り返し

list(chain.from_iterable(...)) 

これを実際に実行すると、これは実際には1行のコードで実行するのに非常に便利です。それは意味があり、実際には非常に読みやすいです。

関連する問題