Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Character_Element - Fatal编程技术网

Python 查找列表中每个元素中某个字符的数量

Python 查找列表中每个元素中某个字符的数量,python,list,character,element,Python,List,Character,Element,我想找出这些句子中有多少恰好是列表中的元素。因此,对于: [“这是一个句子”,“这是另一个句子”] 调用元素0将返回值3,调用元素1将返回值4。我在查找空白以及循环遍历每个元素以查找空白数量最多的元素时遇到了困难。使用 其他方法包括使用 如果您想要一个可调用的(正如您所提到的,这是一个遍历列表的函数),那么它可以实现为 >>> def countspace(x): ... return x.count(' ') ... 并作为 >>> for i

我想找出这些句子中有多少恰好是列表中的元素。因此,对于:
[“这是一个句子”,“这是另一个句子”]

调用元素0将返回值3,调用元素1将返回值4。我在查找空白以及循环遍历每个元素以查找空白数量最多的元素时遇到了困难。

使用

其他方法包括使用


如果您想要一个可调用的(正如您所提到的,这是一个遍历列表的函数),那么它可以实现为

>>> def countspace(x):
...     return x.count(' ')
... 
并作为

>>> for i in lst:
...     print countspace(i)
... 
3
4

这可以通过使用正则表达式解决,如下所述


后期编辑

>>> s = 'this is a sentence. this is one more sentence'
>>> lst = s.split('. ')
>>> [i.count(' ') for i in lst]
[3, 4]
3 4 0 23 0 3 4 0 23 0 正如您所说,您还需要找到max元素,您可以这样做

>>> vals = [i.count(' ') for i in lst] 
>>> lst[vals.index(max(vals))]
'this is one more sentence'
这可以通过使用

>>> def getmax(lst):
...     vals = [i.count(' ') for i in lst]
...     maxel = lst[vals.index(max(vals))]
...     return (vals,maxel)
并将其用作

>>> getmax(lst)
([3, 4], 'this is one more sentence')

评论后编辑

>>> s = 'this is a sentence. this is one more sentence'
>>> lst = s.split('. ')
>>> [i.count(' ') for i in lst]
[3, 4]
3 4 0 23 0 3 4 0 23 0
使用

其他方法包括使用


如果您想要一个可调用的(正如您所提到的,这是一个遍历列表的函数),那么它可以实现为

>>> def countspace(x):
...     return x.count(' ')
... 
并作为

>>> for i in lst:
...     print countspace(i)
... 
3
4

这可以通过使用正则表达式解决,如下所述


后期编辑

>>> s = 'this is a sentence. this is one more sentence'
>>> lst = s.split('. ')
>>> [i.count(' ') for i in lst]
[3, 4]
3 4 0 23 0 3 4 0 23 0 正如您所说,您还需要找到max元素,您可以这样做

>>> vals = [i.count(' ') for i in lst] 
>>> lst[vals.index(max(vals))]
'this is one more sentence'
这可以通过使用

>>> def getmax(lst):
...     vals = [i.count(' ') for i in lst]
...     maxel = lst[vals.index(max(vals))]
...     return (vals,maxel)
并将其用作

>>> getmax(lst)
([3, 4], 'this is one more sentence')

评论后编辑

>>> s = 'this is a sentence. this is one more sentence'
>>> lst = s.split('. ')
>>> [i.count(' ') for i in lst]
[3, 4]
3 4 0 23 0 3 4 0 23 0 您声明“空白”,通常包括这些字符
'\t\n\x0b\x0c\r'
,加上任何unicode字符,例如u'\u3000'(表意空间)

正则表达式解决方案是更好的解决方案之一,因为它很容易支持除通常的ascii码外的任何unicode空白码点。只需使用并设置标志:

输出

>>> s = 'this is a sentence. this is one more sentence'
>>> lst = s.split('. ')
>>> [i.count(' ') for i in lst]
[3, 4]
3 4 0 23 0 3 4 0 23 0 输出

>>> s = 'this is a sentence. this is one more sentence'
>>> lst = s.split('. ')
>>> [i.count(' ') for i in lst]
[3, 4]
3 4 0 23 0 3 4 0 23 0 您声明“空白”,通常包括这些字符
'\t\n\x0b\x0c\r'
,加上任何unicode字符,例如u'\u3000'(表意空间)

正则表达式解决方案是更好的解决方案之一,因为它很容易支持除通常的ascii码外的任何unicode空白码点。只需使用并设置标志:

输出

>>> s = 'this is a sentence. this is one more sentence'
>>> lst = s.split('. ')
>>> [i.count(' ') for i in lst]
[3, 4]
3 4 0 23 0 3 4 0 23 0 输出

>>> s = 'this is a sentence. this is one more sentence'
>>> lst = s.split('. ')
>>> [i.count(' ') for i in lst]
[3, 4]
3 4 0 23 0 3 4 0 23 0 您可以使用。我不知道它是否比
.count()

您可以使用。我不知道它是否比
.count()


您可以添加到模块的链接,以便OP可以learn@BhargavRao:也许
re
是唯一提到的外部模块,doco不难找到。我添加了对
re.findall()
的引用,以防万一:)好兄弟!始终链接到文档是我们能提供的最好的帮助:)@BhargavRao:是的,先生!:你可以添加到模块的链接,这样OP就可以learn@BhargavRao:也许
re
是唯一提到的外部模块,doco不难找到。我添加了对
re.findall()
的引用,以防万一:)好兄弟!始终链接到文档是我们能提供的最好的帮助:)@BhargavRao:是的,先生!:你可以添加到模块的链接,这样OP就可以learn@BhargavRao更新:),我也是你的堆栈迷:)你在Python方面很棒,这就是为什么:)谢谢兄弟。不过我还是个业余爱好者:)你可以添加模块的链接,这样OP就可以learn@BhargavRao更新:),我也是你的堆栈迷:)你在Python方面很棒,这就是为什么:)谢谢兄弟。不过我还是个业余爱好者:)