查找lis中每个元素中某个字符的数量

2024-04-26 04:12:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想找出每个句子中有多少' '(空格)恰好是列表中的元素。因此,对于: ['this is a sentence', 'this is one more sentence'] 调用元素0将返回值3,调用元素1将返回值4。我真的很难找到空白,以及循环通过每一个元素找到一个有最多的空白。你知道吗


Tags: 元素列表ismorethisonesentence空白
3条回答

声明“whitespace”,通常包括这些字符'\t\n\x0b\x0c\r ',再加上任何unicode字符,例如u'\u3000'(表意字符空格)。你知道吗

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

import re

def count_whitespace(s):
    return len(re.findall(r'\s', s, re.UNICODE))

l = ['this is a sentence',
     'this is one more sentence',
     '',
     u'\u3000\u2029    abcd\t\tefghi\0xb  \n\r\nj k  l\tm    \n\n',
     'nowhitespaceinthisstring']

for s in l:
    print count_whitespace(s)

输出

3
4
0
23
0

一种简单的、非正则表达式的方法是使用str.split(),它可以自然地拆分任何空格字符,是从字符串中删除所有空格的有效方法。这也适用于unicode空白字符:

def count_whitespace(s):
    return len(s) - len(''.join(s.split()))

for s in l:
    print count_whitespace(s)

输出

3
4
0
23
0

最后,选出空格字符最多的句子:

>>> max((count_whitespace(s), s) for s in l)[1]
u'\u3000\u2029    abcd\t\tefghi\x00xb  \n\r\nj k  l\tm    \n\n'

使用^{}进行简单的列表理解

>>> lst = ['this is a sentence', 'this is one more sentence']
>>> [i.count(' ') for i in lst]
[3, 4]

其他方法包括使用^{}

>>> map(lambda x:x.count(' '),lst)
[3, 4]

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

>>> def countspace(x):
...     return x.count(' ')
... 

并作为

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

这可以用regex来解决,regex使用^{} module,如下所述Grijesh

>>> import re
>>> [len(re.findall(r"\s", i)) for i in lst]
[3, 4]

后期编辑

正如您所说的,您还需要找到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]

你可以用^{}。我不知道它是否比.count()费时

from collections import Counter
lst = ['this is a sentence', 'this is one more sentence']
>>>[Counter(i)[' '] for i in lst]
[3, 4]

相关问题 更多 >

    热门问题