简体   繁体   English

如何使用 itertools 构建有序字符串列表?

[英]How to build a list of ordered strings with itertools?

I have a Python string: "d4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5 h6 Bf4 0-0 e3 Nbd7 g4 dxc4"我有一个 Python 字符串: "d4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5 h6 Bf4 0-0 e3 Nbd7 g4 dxc4"

I want to split it into:我想把它分成:

["d4", "d4 d5", "d4 d5 c4", ... , "d4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5 h6 Bf4 0-0 e3 Nbd7 g4 dxc4"]

I'm not sure how to run itertools on it.我不确定如何在其上运行itertools

itertools.accumulate used in a plain manner is almost what you want:以普通方式使用的itertools.accumulate几乎是您想要的:

>>> from itertools import accumulate
>>> s = "d4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5 h6 Bf4 0-0 e3 Nbd7 g4 dxc4"
>>> list(accumulate(s.split()))
['d4',
 'd4d5',
 'd4d5c4',
 'd4d5c4e6',
 'd4d5c4e6Nc3',
 'd4d5c4e6Nc3Be7',
 'd4d5c4e6Nc3Be7Nf3',
 'd4d5c4e6Nc3Be7Nf3Nf6',
 'd4d5c4e6Nc3Be7Nf3Nf6Bg5',
 'd4d5c4e6Nc3Be7Nf3Nf6Bg5h6',
 'd4d5c4e6Nc3Be7Nf3Nf6Bg5h6Bf4',
 'd4d5c4e6Nc3Be7Nf3Nf6Bg5h6Bf40-0',
 'd4d5c4e6Nc3Be7Nf3Nf6Bg5h6Bf40-0e3',
 'd4d5c4e6Nc3Be7Nf3Nf6Bg5h6Bf40-0e3Nbd7',
 'd4d5c4e6Nc3Be7Nf3Nf6Bg5h6Bf40-0e3Nbd7g4',
 'd4d5c4e6Nc3Be7Nf3Nf6Bg5h6Bf40-0e3Nbd7g4dxc4']

If you want the spaces in there, you'll need a custom accumulator function to add the spaces, eg:如果您想要其中的空格,则需要一个自定义累加器 function 来添加空格,例如:

>>> list(accumulate(s.split(), '{} {}'.format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0', 'd4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5 h6 Bf4 0-0 e3', 'd4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5 h6 Bf4 0-0 e3 Nbd7', 'd4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5 h6 Bf4 0-0 e3 Nbd7 g4', 'd4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5 h6 Bf4 0-0 e3 Nbd7 g4 dxc4']

This isn't really an itertools problem.这不是真正的itertools问题。 In Haskell, the function is called inits , but Python doesn't have an equivalent built-in.在 Haskell 中, function 被称为inits ,但 Python 没有等效的内置函数。 We can write it ourselves.我们可以自己写。

def inits(xs):
    yield ()
    acc = []
    for x in xs:
        acc.append(x)
        yield tuple(acc)

Note that we return newly-constructed tuples so as not to share any data between iterations.请注意,我们返回新构建的元组,以免在迭代之间共享任何数据。 We also yield the empty tuple first , since it's a valid prefix of a list.我们还先生成空元组,因为它是列表的有效前缀。 If you don't want that in your output, you can filter out the first element.如果您不想在 output 中使用它,您可以过滤掉第一个元素。

Now it's just a bit of fixing up the data with join and split .现在只需使用joinsplit修复数据。

my_string = "d4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5 h6 Bf4 0-0 e3 Nbd7 g4 dxc4"
my_moves = my_string.split(" ")
my_prefixes = map(" ".join, inits(my_moves))
print(list(my_prefixes))

Another with itertools.accumulate :另一个使用itertools.accumulate

list(map(' '.join, accumulate(zip(s.split()))))

Here one using list comprehension, regular expressions ( from re import re.finditer as r ) and slicing ( s is the string to process).这里使用列表理解、正则表达式( from re import re.finditer as r )和切片( s是要处理的字符串)。 This way all the splitting and joining again is not necessary:这样就不需要所有的拆分和加入:

[s[0:m.start()] for m in r(" ",s)]+[s] 

With regex :使用regex

import regex

s = "d4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5 h6 Bf4 0-0 e3 Nbd7 g4 dxc4"
regex.findall(r"(?<=(?: |^)(.*)(?: |$))", s)

['d4',
 'd4 d5',
 'd4 d5 c4',
 'd4 d5 c4 e6',
 'd4 d5 c4 e6 Nc3',
 'd4 d5 c4 e6 Nc3 Be7',
 'd4 d5 c4 e6 Nc3 Be7 Nf3',
 'd4 d5 c4 e6 Nc3 Be7 Nf3 Nf6',
 'd4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5',
 'd4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5 h6',
 'd4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5 h6 Bf4',
 'd4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5 h6 Bf4 0-0',
 'd4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5 h6 Bf4 0-0 e3',
 'd4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5 h6 Bf4 0-0 e3 Nbd7',
 'd4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5 h6 Bf4 0-0 e3 Nbd7 g4',
 'd4 d5 c4 e6 Nc3 Be7 Nf3 Nf6 Bg5 h6 Bf4 0-0 e3 Nbd7 g4 dxc4']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用itertools从字符串列表中创建文件路径? - How to make file path from list of strings using itertools? 如何将itertools.permutations(“ 0123456789”)的结果(在python中)转换为字符串列表 - How to turn the result (in python) of itertools.permutations(“0123456789”) into list of strings itertools累积构建递归列表 - itertools accumulate to build a recursive list 如何使用 python itertools.groupby() 按第一个字符对字符串列表进行分组? - How can I use python itertools.groupby() to group a list of strings by their first character? 如何从有序邻接表构建递归字典树 - How to build a recursive dictionary tree from an ordered adjacency list 将itertools.permutations的输出从元组列表转换为字符串列表 - Converting the output of itertools.permutations from list of tuples to list of strings 如何在列表理解中应用BREAK for Itertools? - how to apply BREAK for Itertools count in List Comprehensions? Python itertools:如何将排列打印为垂直列表 - Python itertools: how to print permutations as a vertical list 将itertools.chain对象转换/解压缩为无序列表的最有效方法 - Most efficient way to convert/unpack an itertools.chain object to an unordered and ordered list Python itertools.product构建汇总并添加到新列表 - Python itertools.product build sum and add to new list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM