Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.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

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

Python 比较字符串中的前两个字符

Python 比较字符串中的前两个字符,python,string,compare,Python,String,Compare,所以我有一个字符串列表: list1 = ["1thing", "2thing", "3thing", "1thing"] 我想知道每一个都在列表中有多少次。问题是,我只想比较前两个字符,因为我知道如果前三个字符相同,那么整个字符串是相同的。我想我可以修改内置的list.count(x)方法,或者我可以重写\uuuu eq\uuu操作符,但我不确定如何执行这两个操作 为什么要检查所有的搭扣..使用集合.计数器模块查找频率 >>> import collections >

所以我有一个字符串列表:

list1 = ["1thing", "2thing", "3thing", "1thing"]

我想知道每一个都在列表中有多少次。问题是,我只想比较前两个字符,因为我知道如果前三个字符相同,那么整个字符串是相同的。我想我可以修改内置的list.count(x)方法,或者我可以重写
\uuuu eq\uuu
操作符,但我不确定如何执行这两个操作

为什么要检查所有的搭扣..使用
集合.计数器
模块查找频率

>>> import collections
>>> x=['1thing', '2thing', '1thing', '3thing']
>>> y=collections.Counter(x)
>>> y
Counter({'1thing': 2, '2thing': 1, '3thing': 1})

使用生成器提取前两个字符,并使用内置的
集合。计数器
类:

Counter(item[:2] for item in list1)

可能不如@Marcin的解决方案好,但使用
itertools.groupby
可能会使其更具可读性和灵活性

from itertools import groupby

def group_by_startswith(it, n):
    """Get a dict mapping the first n characters to the number of matches."""

    def first_n(str_):
        return str_[:n]

    startswith_sorted = sorted(it, key=first_n)
    groups = groupby(startswith_sorted, key=first_n)

    return {key: len(list(grouped)) for key, grouped in groups}
示例输出:

>>> list1 = ["1thing", "2thing", "3thing", "1thing"]
>>> print(group_by_startswith(list1, 3))
{'3th': 1, '2th': 1, '1th': 2}

此解决方案允许您在结果方面具有更大的灵活性。例如,将返回行修改为return
grouped
list(grouped)
,可以轻松获得匹配的对象。

“我只想比较前两个字符,因为我知道如果前三个字符相同,则整个字符串相同。”听起来像是一种假设,将来可能会被打破。为什么不只是避免风险,只是检查整个字符串?你从额外的复杂性和风险中得到了什么?当你可以使用内置集合时,为什么还要经历所有的检查呢,我正在检查一个列表,其中有100000多个字符串,大约80个字符长,检查前25个字符可能比检查所有字符更快。我不知道这是不是真的,但我正在尝试测试它。你们做过任何基准测试来证明使用collections.Counter不够快。钥匙够快的。如果是,为什么要经历优化的压力。注意人们说的过早优化。用于比较字符串的内置函数是C代码。如果您编写自定义Python代码,Python代码是否会降低速度,使运行C代码的速度更快?我不知道,但如果你测量,你就会知道。我必须说,100000个字符串实际上并不是很多,所以即使对它们进行特殊处理更快,您可能也不会获得很多时间。我只是运行了一个快速测试:我生成了100000个长度为80的随机字符串,然后对它们进行计数。计算它们只花了几分之一秒,没有做任何特殊的技巧,只是使用默认的字符串比较。为什么不可能使用任意键函数作为生成器表达式的第一个元素?我想我应该再编辑一点。当你将一个函数映射到一系列值上时(如果你有一个更复杂的映射函数,我会更好),我试图不必要地强调使用更多的函数工具(比如
map
)。除非你需要优化一个热点,否则球场上的马匹更多。