Python “拆分字符串”;aabbcc“-&燃气轮机;[“aa”、“bb”、“cc”]无需重新拆分

Python “拆分字符串”;aabbcc“-&燃气轮机;[“aa”、“bb”、“cc”]无需重新拆分,python,Python,我想在一次调用中根据标题拆分字符串。我正在寻找使用列表理解的简单语法,但我还没有: s = "123456" 结果是: ["12", "34", "56"] 我不想要的是: re.split('(?i)([0-9a-f]{2})', s) s[0:2], s[2:4], s[4:6] [s[i*2:i*2+2] for i in len(s) / 2] 编辑: 好的,我想解析一个十六进制RGB[a]颜色(以及可能的其他颜色/组件格式),以提取所有组件。 似乎最快的方法是斯文·马纳奇的最后一

我想在一次调用中根据标题拆分字符串。我正在寻找使用列表理解的简单语法,但我还没有:

s = "123456"
结果是:

["12", "34", "56"]
我不想要的是:

re.split('(?i)([0-9a-f]{2})', s)
s[0:2], s[2:4], s[4:6]
[s[i*2:i*2+2] for i in len(s) / 2]
编辑

好的,我想解析一个十六进制RGB[a]颜色(以及可能的其他颜色/组件格式),以提取所有组件。 似乎最快的方法是斯文·马纳奇的最后一种方法:

  • sven marnach X范围:每圈0.883 usec

    python -m timeit -s 's="aabbcc";' '[int(s[i:i+2], 16) / 255. for i in xrange(0, len(s), 2)]'
    
    python -m timeit -s 's="aabbcc"' '["%c%c" % pair for pair in zip(* 2 * [iter(s)])]'
    
    python -m timeit -s 'import re; s="aabbcc"; c=re.compile("(?i)([0-9a-f]{2})"); 
    split=re.split' '[int(x, 16) / 255. for x in split(c, s) if x != ""]'
    
  • 对/国际热核实验堆:每回路1.38 usec

    python -m timeit -s 's="aabbcc";' '[int(s[i:i+2], 16) / 255. for i in xrange(0, len(s), 2)]'
    
    python -m timeit -s 's="aabbcc"' '["%c%c" % pair for pair in zip(* 2 * [iter(s)])]'
    
    python -m timeit -s 'import re; s="aabbcc"; c=re.compile("(?i)([0-9a-f]{2})"); 
    split=re.split' '[int(x, 16) / 255. for x in split(c, s) if x != ""]'
    
  • 正则表达式:每个循环2.55 usec

    python -m timeit -s 's="aabbcc";' '[int(s[i:i+2], 16) / 255. for i in xrange(0, len(s), 2)]'
    
    python -m timeit -s 's="aabbcc"' '["%c%c" % pair for pair in zip(* 2 * [iter(s)])]'
    
    python -m timeit -s 'import re; s="aabbcc"; c=re.compile("(?i)([0-9a-f]{2})"); 
    split=re.split' '[int(x, 16) / 255. for x in split(c, s) if x != ""]'
    
  • 请参阅:有关同一
    str
    语法上奇怪的“2-
    iter
    的解释


    你在评论中说你想“拥有最快的执行速度”,我不能保证通过这个实现,但是你可以使用。当然记得。对于手头的问题(既然你已经揭示了它),我想你会发现
    r,g,b=s[0:2],s[2:4],s[4:6]
    很难打败

    $ python3.2 -m timeit -c '
    s = "aabbcc"
    ["".join(pair) for pair in zip(* 2 * [iter(s)])]
    '
    100000 loops, best of 3: 4.49 usec per loop
    
    比照


    阅读这些评论,实际的问题是:以十六进制
    RRGGBBAA
    格式解析颜色定义字符串的最快方法是什么。以下是一些选项:

    def rgba1(s, unpack=struct.unpack):
        return unpack("BBBB", s.decode("hex"))
    
    def rgba2(s, int=int, xrange=xrange):
        return [int(s[i:i+2], 16) for i in xrange(0, 8, 2)]
    
    def rgba3(s, int=int, xrange=xrange):
        x = int(s, 16)
        return [(x >> i) & 255 for i in xrange(0, 32, 8)]
    
    正如我所料,第一个版本的速度最快:

    In [6]: timeit rgba1("aabbccdd")
    1000000 loops, best of 3: 1.44 us per loop
    
    In [7]: timeit rgba2("aabbccdd")
    100000 loops, best of 3: 2.43 us per loop
    
    In [8]: timeit rgba3("aabbccdd")
    100000 loops, best of 3: 2.44 us per loop
    
    比单个查找的首选解决方案更糟糕:

    $ python -m timeit -s 'import numpy as np; s="aabbccdd"' 'a = np.fromstring(s.decode("hex"), dtype="uint32"); a.dtype = "uint8"; list(a)'
    100000 loops, best of 3: 5.14 usec per loop
    $ python -m timeit -s 's="aabbcc";' '[int(s[i:i+2], 16) / 255. for i in xrange(0, len(s), 2)]'
    100000 loops, best of 3: 2.41 usec per loop
    
    但如果您同时进行多个转换,numpy的速度要快得多:

    $ python -m timeit -s 'import numpy as np; s="aabbccdd" * 100' 'a = np.fromstring(s.decode("hex"), dtype="uint32"); a.dtype = "uint8"; a.tolist()'
    10000 loops, best of 3: 59.6 usec per loop
    $ python -m timeit -s 's="aabbccdd" * 100;' '[int(s[i:i+2], 16) / 255. for i in xrange(0, len(s), 2)]'
    1000 loops, best of 3: 240 usec per loop
    
    在我的计算机上,对于大于2的批处理程序,Numpy速度更快。通过将
    a.shape
    设置为
    (颜色数,4)
    ,您可以轻松地对值进行分组,尽管这会使
    tolist
    方法慢50%

    事实上,大部分时间都花在将数组转换为列表上。根据您希望对结果执行的操作,您可以跳过此编辑间步骤,并获得一些好处:

    $ python -m timeit -s 'import numpy as np; s="aabbccdd" * 100' 'a = np.fromstring(s.decode("hex"), dtype="uint32"); a.dtype = "uint8"; a.shape = (100,4)'
    100000 loops, best of 3: 6.76 usec per loop
    

    “aaabb”
    是否应分为
    [“aaa”、“bbb”]
    [“aa”、“ab”、“bb”]
    ?为什么您不喜欢您给出的示例实现,尤其是最后一个呢?@S.Lott:我不认为它是完美的,因为它甚至不是有效的Python<代码>[s[i:i+2]对于范围(0,len(s),2)]内的i更好。@tito:想澄清一下你实际上想做什么吗?(见我上面的问题。)@tito:你可能应该在一个新问题中问这个问题,因为答案将与这个问题中的答案完全不同。(提示:使用
    “aabbcc”.decode(“hex”)
    struct.unpack()
    )当然最快的方法是在C扩展中维护一个包含所有可能的6位十六进制字符串的哈希表。不过,这听起来像是过早的优化。对于范围(0,len(s),2)内的i,你真的认为这比
    [s[i:i+2]更可取吗?@SvenMarnach:“更可取”?不,只是另一种选择(也是我小小的脑子里想到的第一件事)。