Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么是只打印字符串的某些行的最具python风格的方法?_Python_String_Line - Fatal编程技术网

什么是只打印字符串的某些行的最具python风格的方法?

什么是只打印字符串的某些行的最具python风格的方法?,python,string,line,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 sn

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

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()
方法将列表重新连接在一起。更不用说,它看起来很丑,可读性也不强。有没有一种更优雅的/类似蟒蛇的方法来实现这一点?

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

  • 使用正则表达式捕获8行之后的3行
  • 计算换行符的位置,并使用正确的位置对字符串进行一次切片
您将原谅我在下面的代码中所做的一次性错误

正则表达式

import re

print(re.sub("^(.*\n){8}((?:.*\n){3})(.*\n){1,}",r"\2",multiline_string))
linefeed_pos = [i for i,c in enumerate(multiline_string) if c=="\n"]
print(multiline_string[linefeed_pos[7]:linefeed_pos[11]])
(创建一组8行,然后创建一组3行,然后创建其余的,替换为第二组)

位置提取+切片

import re

print(re.sub("^(.*\n){8}((?:.*\n){3})(.*\n){1,}",r"\2",multiline_string))
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


如果性能/内存很重要,那么上面介绍的两种方法都应该比您的方法更快。如果没有,请坚持使用您的解决方案。

regex?(哎哟):
re.sub(^(.*\n){8}((?:.*\n){3})(.*\n){1,}”,r“\2”,多行字符串)
要按行号引用行,需要将它们作为行进行寻址;这需要类似于拆分线的方法和索引。然后,您所需的输出格式包含单独的行,因此在前面也会有一些内容。您可以在原始字符串中搜索换行符以减少总操作,但这看起来比您现有的代码更糟糕。事实上这很好。因为您使用行号信息打印所需内容,所以沿换行符拆分是有意义的。我认为这件事再简单不过了。任何其他的方法,你都是在字符级工作。你有不规则的行长度,无论你用什么方法都必须找到那些换行符
.splitlines
以C速度运行,正则表达式也是如此。我想你会同意,
.splitlines
方法比Jean François的正则表达式可读性强得多,即使他也承认它很难看
.islice
并不会真正为您购买任何东西,因为它仍然需要跳过不需要的初始行。很聪明,但请记住。。。问题是
有没有一种更优雅的/类似蟒蛇的方法来实现这一点?
。我会说答案很简单,
No
。我完全同意这一点。FWIW,
linefeed\u pos
list comp大约比
慢10到30倍。拆分行
,这取决于输入字符串的大小和它包含的换行的比例。我认为如果行位于文本的开头,我们可以缩短它,我来编辑