python - 仅打印字符串的某些行的最pythonic方式是什么?

标签 python string line

假设我有一个跨越多行的字符串(不是文件):

multiline_string = '''I met a traveller from an antique land
Who said: Two vast and trunkless legs of stone
Stand in the desert... near them, on the sand,
Half sunk, a shattered visage lies, whose frown,
And wrinkled lip, and sneer of cold command,
Tell that its sculptor well those passions read
Which yet survive, stamped on these lifeless things,
The hand that mocked them and the heart that fed;

And on the pedestal these words appear:
'My name is Ozymandias, king of kings;
Look on my works, ye Mighty, and despair!'
Nothing beside remains. Round the decay
Of that colossal wreck, boundless and bare
The lone and level sands stretch far away.'''

我只想获取字符串的某些行,作为单个字符串(而不是字符串列表)。一种方法是这样的:

pedestal_lines = "\n".join(multiline_string.splitlines()[9:12])
print(pedestal_lines)

输出:

And on the pedestal these words appear:
'My name is Ozymandias, king of kings;
Look on my works, ye Mighty, and despair!'

但这种方式不是很好:它必须将字符串拆分为一个字符串列表,索引这个列表,然后使用 str.join() 方法将列表重新连接在一起。更不用说,它看起来很丑,而且可读性不强。是否有更优雅/pythonic 的方式来实现这一点?

最佳答案

如果您不想拆分字符串,您可以执行以下操作:

  • 使用正则表达式捕获 8 行后的 3 行
  • 计算换行符的位置并用正确的位置对字符串进行切片

您会原谅我在下面的代码中可能犯的一次性错误。

正则表达式:

import re

print(re.sub("^(.*\n){8}((?:.*\n){3})(.*\n){1,}",r"\2",multiline_string))

(创建一组 8 行,然后创建一组 3 行,然后其余的,替换为第二组)

位置提取+切片:

linefeed_pos = [i for i,c in enumerate(multiline_string) if c=="\n"]
print(multiline_string[linefeed_pos[7]:linefeed_pos[11]])

(在原始字符串上使用列表理解提取换行字符的位置,然后使用这些行索引位置进行切片)。这种方法的缺点是它计算所有 索引,而不仅仅是直到上线边界。这可以通过将生成器理解包装在列表理解中以在不再需要索引时停止来轻松解决:

linefeed_pos = [next (i for i,c in enumerate(multiline_string) if c=="\n") for _ in range(12)]

也许一个切片/提取比拆分和连接的性能更好(我知道看到一个大列表浪费只是为了挑选 3 行是无法忍受的),但我不会称之为 pythonic。

如果性能/内存很重要,如果你有很多行,上面解释的两种方法应该比你的方法更快。如果没有,则坚持使用您的解决方案。

关于python - 仅打印字符串的某些行的最pythonic方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52727251/

相关文章:

string - 如何从文件名中提取值?

c - 用 C 读取文本文件,跳过第一行

delphi - 是最大的点。两条线?

python - Python 中的字符串操作

python - 在Python中从单词中分离/标记点,但不从数字中分离/标记点

python - 为什么 ColumnTransformer 不在其转换器上调用 fit?

javascript - 我如何使用 javascript 将 html 表单的结果输出到桌面上的文本文件中?

c# - 使用 GDI+ 写入多行字符串

linux - 命令行连接到无线网络在 ubuntu 10.04 上不起作用

python - 递归函数 - 无错误且无输出