久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

遍歷字符串的行

Iterate over the lines of a string(遍歷字符串的行)
本文介紹了遍歷字符串的行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個這樣定義的多行字符串:

I have a multi-line string defined like this:

foo = """
this is 
a multi-line string.
"""

我們將這個字符串用作我正在編寫的解析器的測試輸入.解析器函數接收一個 file 對象作為輸入并對其進行迭代.它還直接調用 next() 方法來跳過行,所以我真的需要一個迭代器作為輸入,而不是一個可迭代的.我需要一個迭代器來迭代該字符串的各個行,就像 file-object 將遍歷文本文件的行一樣.我當然可以這樣做:

This string we used as test-input for a parser I am writing. The parser-function receives a file-object as input and iterates over it. It does also call the next() method directly to skip lines, so I really need an iterator as input, not an iterable. I need an iterator that iterates over the individual lines of that string like a file-object would over the lines of a text-file. I could of course do it like this:

lineiterator = iter(foo.splitlines())

有沒有更直接的方法?在這種情況下,字符串必須遍歷一次以進行拆分,然后再由解析器遍歷.在我的測試用例中沒關系,因為那里的字符串很短,我只是出于好奇而問.Python 為這些東西提供了很多有用且高效的內置函數,但我找不到適合這種需要的東西.

Is there a more direct way of doing this? In this scenario the string has to traversed once for the splitting, and then again by the parser. It doesn't matter in my test-case, since the string is very short there, I am just asking out of curiosity. Python has so many useful and efficient built-ins for such stuff, but I could find nothing that suits this need.

推薦答案

這里有三種可能:

foo = """
this is 
a multi-line string.
"""

def f1(foo=foo): return iter(foo.splitlines())

def f2(foo=foo):
    retval = ''
    for char in foo:
        retval += char if not char == '
' else ''
        if char == '
':
            yield retval
            retval = ''
    if retval:
        yield retval

def f3(foo=foo):
    prevnl = -1
    while True:
      nextnl = foo.find('
', prevnl + 1)
      if nextnl < 0: break
      yield foo[prevnl + 1:nextnl]
      prevnl = nextnl

if __name__ == '__main__':
  for f in f1, f2, f3:
    print list(f())

將其作為主腳本運行可確認這三個功能是等效的.使用 timeit (以及 * 100 用于 foo 以獲得大量字符串以進行更精確的測量):

Running this as the main script confirms the three functions are equivalent. With timeit (and a * 100 for foo to get substantial strings for more precise measurement):

$ python -mtimeit -s'import asp' 'list(asp.f3())'
1000 loops, best of 3: 370 usec per loop
$ python -mtimeit -s'import asp' 'list(asp.f2())'
1000 loops, best of 3: 1.36 msec per loop
$ python -mtimeit -s'import asp' 'list(asp.f1())'
10000 loops, best of 3: 61.5 usec per loop

請注意,我們需要調用 list() 來確保遍歷迭代器,而不僅僅是構建迭代器.

Note we need the list() call to ensure the iterators are traversed, not just built.

IOW,天真的實現快得多,甚至都不好笑:比我嘗試使用 find 調用的速度快 6 倍,而后者又比較低級別的方法快 4 倍.

IOW, the naive implementation is so much faster it isn't even funny: 6 times faster than my attempt with find calls, which in turn is 4 times faster than a lower-level approach.

要記住的教訓:測量總是一件好事(但必須準確);像 splitlines 這樣的字符串方法以非常快的方式實現;通過在非常低的級別編程(尤其是通過非常小片段的 += 循環)將字符串放在一起可能會很慢.

Lessons to retain: measurement is always a good thing (but must be accurate); string methods like splitlines are implemented in very fast ways; putting strings together by programming at a very low level (esp. by loops of += of very small pieces) can be quite slow.

編輯:添加了@Jacob 的建議,稍作修改以提供與其他建議相同的結果(保留一行尾隨空格),即:

Edit: added @Jacob's proposal, slightly modified to give the same results as the others (trailing blanks on a line are kept), i.e.:

from cStringIO import StringIO

def f4(foo=foo):
    stri = StringIO(foo)
    while True:
        nl = stri.readline()
        if nl != '':
            yield nl.strip('
')
        else:
            raise StopIteration

測量給出:

$ python -mtimeit -s'import asp' 'list(asp.f4())'
1000 loops, best of 3: 406 usec per loop

不如基于 .find 的方法好——仍然值得牢記,因為它可能不太容易出現小錯誤(任何你看到出現+1 和 -1,就像我上面的 f3 一樣,應該自動觸發一對一的懷疑——許多缺乏這種調整的循環也應該有這些調整——盡管我相信我的代碼是也是正確的,因為我能夠使用其他功能檢查它的輸出').

not quite as good as the .find based approach -- still, worth keeping in mind because it might be less prone to small off-by-one bugs (any loop where you see occurrences of +1 and -1, like my f3 above, should automatically trigger off-by-one suspicions -- and so should many loops which lack such tweaks and should have them -- though I believe my code is also right since I was able to check its output with other functions').

但基于拆分的方法仍然適用.

But the split-based approach still rules.

順便說一句:f4 可能更好的樣式是:

An aside: possibly better style for f4 would be:

from cStringIO import StringIO

def f4(foo=foo):
    stri = StringIO(foo)
    while True:
        nl = stri.readline()
        if nl == '': break
        yield nl.strip('
')

至少,它不那么冗長了.不幸的是,需要去除尾隨 的需要禁止用 return iter(stri) 更清晰、更快速地替換 while 循環(>iter 部分在現代版本的 Python 中是多余的,我相信從 2.3 或 2.4 開始,但它也是無害的).也許也值得一試:

at least, it's a bit less verbose. The need to strip trailing s unfortunately prohibits the clearer and faster replacement of the while loop with return iter(stri) (the iter part whereof is redundant in modern versions of Python, I believe since 2.3 or 2.4, but it's also innocuous). Maybe worth trying, also:

    return itertools.imap(lambda s: s.strip('
'), stri)

或其變體——但我在這里停下來,因為它幾乎是一個基于 strip 的理論練習,最簡單,最快,一個.

or variations thereof -- but I'm stopping here since it's pretty much a theoretical exercise wrt the strip based, simplest and fastest, one.

這篇關于遍歷字符串的行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 黄色成人毛片 | 精品福利在线观看 | 四虎久久 | 亚洲精品18在线观看 | 欧美在线观看一区二区 | 蜜桃91丨九色丨蝌蚪91桃色 | 久久久久亚洲精品 | 日韩视频一区二区三区 | 成人做爰69片免费 | 色婷婷导航| 免费黄色av | 亚洲欧美中文字幕 | 欧美日韩在线精品 | 国产午夜麻豆影院在线观看 | 夜夜操影院 | 最新日韩av| 精品国产视频 | 国产高清免费 | 日韩免费看片 | 亚洲黄色大片 | 亚洲国产网站 | 国产精品一区二区在线播放 | 欧美一级做性受免费大片免费 | 精品视频免费 | 男女h黄动漫啪啪无遮挡软件 | 国产精品一区二区视频 | 手机在线播放av | 国产在线成人 | 欧美黑人猛交 | 久久99精品久久久久久国产越南 | 日韩影院在线观看 | 国产一区在线视频 | 成人黄色在线视频 | 国产激情在线 | 欧美黄色片网站 | 综合久久久久 | 久久久久国产精品夜夜夜夜夜 | 国产黄a三级三级看三级 | 国产精品免费一区 | 欧美久久网| 国产91在线观看 |