Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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_Python 3.x - Fatal编程技术网

Python 如何从多行字符串中计算特定行中的项目

Python 如何从多行字符串中计算特定行中的项目,python,python-3.x,Python,Python 3.x,我想知道如何从多行字符串中计算某一行中的项目 示例:第一行将返回7 第二个将返回6 最后一个将返回2(因为“苹果派”之间只包含一个空格)。这里有一个小例子,打印出一行中的元素数 >>> itms="""banana cookie cream soda lime limes apples desert plate cake orange milk beans juice apple pi

我想知道如何从多行字符串中计算某一行中的项目

示例:第一行将返回7

第二个将返回6


最后一个将返回2(因为“苹果派”之间只包含一个空格)。

这里有一个小例子,打印出一行中的元素数

>>> itms="""banana  cookie  cream  soda   lime  limes  apples
              desert  plate   cake  orange  milk   beans   
              juice   apple pie"""
1) 首先,您要将每一行字符串放入一个
字符串列表

2) 然后将其分解为单个元素,以便一行中的每个单词都在其自己的列表中


3) 从那以后,我们只需要得到包含每行单词的列表长度,这里有一个小例子,打印出一行中的元素数

>>> itms="""banana  cookie  cream  soda   lime  limes  apples
              desert  plate   cake  orange  milk   beans   
              juice   apple pie"""
import re
lines = [re.split(r'\s{2,}',x.strip()) for x in itms.split("\n")]
for index, line in enumerate(lines):
    print "line number : {0} has {1} elements".format(index, len(line))

line number : 0 has 7 elements
line number : 1 has 6 elements
line number : 2 has 3 elements
1) 首先,您要将每一行字符串放入一个
字符串列表

2) 然后将其分解为单个元素,以便一行中的每个单词都在其自己的列表中

3) 从那以后,我们只需要得到包含每行单词的列表长度

import re
lines = [re.split(r'\s{2,}',x.strip()) for x in itms.split("\n")]
for index, line in enumerate(lines):
    print "line number : {0} has {1} elements".format(index, len(line))

line number : 0 has 7 elements
line number : 1 has 6 elements
line number : 2 has 3 elements
把你的绳子分成几行

In [12]: itms="""banana  cookie  cream  soda   lime  limes  apples
    ...:               desert  plate   cake  orange  milk   beans
    ...:               juice   apple pie"""
在两个以上的空格上拆分项目

In [13]: itms = [line.strip() for line in itms.split('\n')]

In [14]: itms
Out[14]:
['banana  cookie  cream  soda   lime  limes  apples',
 'desert  plate   cake  orange  milk   beans',
 'juice   apple pie']
计算每行中的元素

In [15]: import re

In [16]: itms =  [re.split(r'\s\s+', line) for line in itms]

In [17]: itms
Out[17]:
[['banana', 'cookie', 'cream', 'soda', 'lime', 'limes', 'apples'],
 ['desert', 'plate', 'cake', 'orange', 'milk', 'beans'],
 ['juice', 'apple pie']]
把你的绳子分成几行

In [12]: itms="""banana  cookie  cream  soda   lime  limes  apples
    ...:               desert  plate   cake  orange  milk   beans
    ...:               juice   apple pie"""
在两个以上的空格上拆分项目

In [13]: itms = [line.strip() for line in itms.split('\n')]

In [14]: itms
Out[14]:
['banana  cookie  cream  soda   lime  limes  apples',
 'desert  plate   cake  orange  milk   beans',
 'juice   apple pie']
计算每行中的元素

In [15]: import re

In [16]: itms =  [re.split(r'\s\s+', line) for line in itms]

In [17]: itms
Out[17]:
[['banana', 'cookie', 'cream', 'soda', 'lime', 'limes', 'apples'],
 ['desert', 'plate', 'cake', 'orange', 'milk', 'beans'],
 ['juice', 'apple pie']]

假设分词器总是两个空格,那么下面将返回
[7,6,2]

In [18]: for x in itms:
    ...:     print(len(x))
    ...:
7
6
2
如果分词器是任意数量的空白字符,则可以使用regex模块

[len(line.strip().split('  ')) for line in itms.split('\n')]

假设分词器总是两个空格,那么下面将返回
[7,6,2]

In [18]: for x in itms:
    ...:     print(len(x))
    ...:
7
6
2
如果分词器是任意数量的空白字符,则可以使用regex模块

[len(line.strip().split('  ')) for line in itms.split('\n')]

您可以只使用两行代码:

import re
[len(re.split(r'\s\s+', line.strip())) for line in itms.split('\n')]

您可以只使用两行代码:

import re
[len(re.split(r'\s\s+', line.strip())) for line in itms.split('\n')]

查看.split方法请参见
str.split
str.strip
str.split
接受一个参数,该参数指示它应该拆分的内容。这两种方法使这项任务相对容易。请查看.split方法参见
str.split
str.strip
str.split
采用一个参数,该参数指示它应该在什么基础上进行拆分。这两种方法使这项任务相对容易。除了它计算最后一行错误。。。它算作3而不是2。除了最后一行算错了。。。它算作3而不是2。