2015-04-17 31 views
-2

對它們進行排序我有一個名單上的字一個大文件,我想世界上的長度和排序,以把它們放在不同的文件,例如:提取元素,並用長

List1=['example','example1','example12] 

輸出: 文件1:7個字母(例如) 文件2的話:用8個字母(例1) 文件3的話:用9個字母(例12)

+0

File1,File2,File3?三個不同的文件? – dbliss

+0

實際上你是否需要按照長度對它們進行排序,然後對排序後的列表進行分組,或者只需要按照長度對它們進行分區,並且您認爲排序是最簡單的方法呢? – abarnert

+0

我需要通過長度爲 –

回答

0

我不認爲你真的需要這裏的任何排序的話。你只是想把分區的這個單詞按長度分成不同的文件。你可以做到這一點對飛:

with open('file1', 'w') as f1, open('file2', 'w') as f2, open('file3', 'w') as f3: 
    for entry in List1: 
     if len(entry) == 7: 
      f1.write(entry) 
     elif len(entry) == 8: 
      f2.write(entry) 
     elif len(entry) == 9: 
      f3.write(entry) 

如果你將有大量的文件(實際上,3已經是邊緣),我會考慮使這一點更加動感與一個字典而不是一個elif鏈。例如,該航天飛機長度爲0,1,2項,...,9 file1命名file0文件,...,file9

with contextlib.ExitStack as stack: 
    lenmap = {i: stack.enter_context(open('file{}'.format(i), 'w')) 
      for i in range(10)} 
    for entry in List1: 
     f = lenmap.get(len(entry)) 
     if f: 
      f.write(entry) 

對於Python 2.7,你不必ExitStack,所以沒有安全方式使用文件的任意數量的with語句,所以我們必須使用finally代替:

lenmap = {i: open('file{}'.format(i), 'w') for i in range(10)} 
try: 
    for entry in List1: 
     f = lenmap.get(len(entry)) 
     if f: 
      f.write(entry) 
finally: 
    for f in lenmap.values(): 
     f.close() 

我猜你真正想要的字與字之間的某種分離,如'\n'' ',但它應該是顯而易見的如何添加任何你想要的。


一個原因,你可能需要排序是如果它只是沒有理由讓所有的文件打開整個運行(也許你有一個令人難以置信的速度慢的文件系統,以及數百個文件)。在這種情況下,可以進行排序,然後組,然後做一個文件在同一時間:

for key, group in itertools.groupby(sorted(List1, key=len), key=len): 
    with open('file{}'.format(key), 'w') as f: 
     for entry in group: 
      f.write(entry) 
+0

謝謝你,我現在嘗試我現在​​ –

+0

其實它是超過3,3只是一個例子 –

+0

有338990個單詞,如果我把它分成文件,我將只能打電話給單詞需要的時間長度,我認爲這對於系統資源來說會更容易和更少費用 –

-1
List1=['example','example1','example12'] 

for item in List1: 
    fileToWrite = "example{0}".format(len(item)) 
    with open(fileToWrite, 'a') as fileID: 
     fileID.write(item + "\n") 
-1

下面的腳本讀取源文件,用文字的長度將其劃分到字成詞集列表,然後將列表中的每個元素(如果它不是空的)寫入單獨文件

words=[set() for _ in range(40)] 
with open('source.file') as sfile: 
    for line in sfile: 
     for word in line.split(" "): 
      word=word.strip('''\n!"',.:*?;-''') 
      if word != '': 
       words[len(word)].add(word) 
for i in range(len(words)): 
    if len(words[i]) != 0: 
     fname='te st/file' + str(i) 
     with open(fname, 'w') as tfile: 
      tfile.write('\n'.join(words[i])) 
0

這是一種簡短而不失靈活的方式。使用collections.defaultdicthttps://docs.python.org/2/library/collections.html#collections.defaultdict),這樣它將自動爲字長創建+打開的文件。

有關詳細說明,請參閱代碼中的註釋。

from collections import defaultdict 

# default dict that will automatically open/create file 
# if it didn't have one open for it yet 
class newfile(defaultdict): 
    def __missing__(self, key): 
     self[key] = open(str(key)+".txt", 'w') 
     return self[key] 

# helper to transform a line of text into a list of words 
words = lambda line: line.strip().split() 

with open("words.txt", 'r') as inputfile: 
    # process a word: write it in the correct file 
    def procword(filedict, word): 
     return filedict[len(word)].write(word+"\n") or filedict 
    # process a line in the file: get the words and process them 
    def procline(filedict, line): 
     return reduce(procword, words(line), filedict) 
    # process all lines in the inputfile, starting with an empty length -> file dict 
    files = reduce(procline, inputfile, newfile()) 
    # maybe superfluous, but close all files (it's polite) 
    [fd.close() for (_, fd) in files.iteritems()] 
+0

我認爲很多人(尤其是Guido)會反對任何一個術語「pythonic」和函數「reduce」出現在同一篇文章中。更嚴重的是,對於爲副作用而不是爲其值賦值的函數'map',python_definitely不是pythonic。或者使用'lambda'而不是'def'定義一個命名函數。或者使用'lambda x:x.close()'而不是'file.close',或者更好的方法是使用listcomp,這樣你就可以把它寫成表達式而不是函數了...... – abarnert

+0

你是對的,所以編輯過一些部分,thx。我知道這個男人不是減少的粉絲,但我是。我喜歡單線lambda,因爲它比'def'版本更短,我希望儘可能保持代碼儘可能小;不想引入更多的噪音(線條) - 特別是在這些空間有限的SO帖子中(按設計)。注意:最初,我甚至沒有那個'words' helper函數,但是我不得不將它內聯放在'reduce'中,這使得該行非常討厭閱讀。 – haavee

+0

感謝'file.close' - 我沒有意識到'file.close(obj)'與'obj.close()'相同(提供'obj'是一個''file',ofc。 )! – haavee