2012-03-20 126 views
2

我有一個看起來像這樣的文件:使用從另一個列表信息號碼列表

100 2 
300 3 
50 1 
500 5 

,我想生成號碼池,其中數字的次數也就是在第一個數字該列表應該發生x次(x是列表中的第二個數字)。

這就是我想要的輸出是:

[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」將是您的功能的預期名稱。 – 2012-03-20 15:00:17

+0

該輸出不能由該代碼生成。你的「pos」是一個字符串,所以輸出應該看起來像'['100100','300300300','50','500500500500500']'。請複製並粘貼代碼並輸出您想要評論的內容。 – DSM 2012-03-20 15:03:55

+0

你是對的,它看起來像那樣。抱歉!我下次複製粘貼。 – edg 2012-03-20 15:10:18

回答

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 

我改變了兩行。 return [pos] * count將使數組pos

bigpool += pool將把pool的元素附加到bigpool

+0

謝謝!很好地工作。 – edg 2012-03-20 15:03:46

+0

我想從這個bigpool的樣本x很多值1000倍,並試過這個: – edg 2012-03-20 15:06:08

+0

randomsample = random.sample(bigpool,x),但不知道如何做到1000倍? – edg 2012-03-20 15:06:27

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聲明)。我們要做的第一件事就是把所有的行都分開,並把它們分配在空白處,給我們一些數字,數字對的列表。

(line.split() for line in f) 

然後我們把這些對和重複號定次數:

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

我們現在有重複發生器(本質上是一個名單列表)的生成,因此我們擴大這些了整合到一個列表中:

list(chain.from_iterable(...)) 

如果你只是按照它,這實際上是一個很好的方式,在一行代碼中完成它。它在意義上有很大的意義,實際上非常可讀。