<p>来自@JohanL的答案很好地说明了标准python库中最接近的等价物是什么</p>
<p>2019年11月,我改编了Matt Hagy的一篇要点,现在已经在<code>pypi</code></p>
<p><a href="https://pypi.org/project/infixpy/" rel="nofollow noreferrer">https://pypi.org/project/infixpy/</a></p>
<pre><code>from infixpy import *
a = (Seq(range(1,51))
.map(lambda x: x * 4)
.filter(lambda x: x <= 170)
.filter(lambda x: len(str(x)) == 2)
.filter( lambda x: x % 20 ==0)
.enumerate()
.map(lambda x: 'Result[%d]=%s' %(x[0],x[1]))
.mkstring(' .. '))
print(a)
# Result[0]=20 .. Result[1]=40 .. Result[2]=60 .. Result[3]=80
</code></pre>
<p><strong>其他答案中描述的其他方法</strong></p>
<ul>
<li><p>pyxtension<a href="https://stackoverflow.com/a/62585964/1056563">https://stackoverflow.com/a/62585964/1056563</a></p>
<p>从pyxtension.streams导入流</p>
</li>
<li><p>sspipe<a href="https://stackoverflow.com/a/56492324/1056563">https://stackoverflow.com/a/56492324/1056563</a></p>
<p>从SSP管道导入,px</p>
</li>
</ul>
<p><strong>旧方法</strong></p>
<p>2018年秋天,我发现了一个更具吸引力的工具包</p>
<p><a href="https://github.com/dwt/fluent" rel="nofollow noreferrer">https://github.com/dwt/fluent</a></p>
<p><a href="https://i.stack.imgur.com/mvTll.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mvTll.png" alt="enter image description here"/></a></p>
<p>在对可用的<em>第三方</em>库进行了相当彻底的审查之后,它似乎最适合于需求</p>
<p>您可以创建自己的管道函数。我把它用于争论如下所示的一些文本。粗体线是工作发生的地方。所有这些<code>@Pipe</code>东西我只需要编码一次就可以重用</p>
<p>此处的任务是关联第一个文本中的缩写:</p>
<pre><code>rawLabels="""Country: Name of country
Agr: Percentage employed in agriculture
Min: Percentage employed in mining
Man: Percentage employed in manufacturing
PS: Percentage employed in power supply industries
Con: Percentage employed in construction
SI: Percentage employed in service industries
Fin: Percentage employed in finance
SPS: Percentage employed in social and personal services
TC: Percentage employed in transport and communications"""
</code></pre>
<p>在第二个文本中使用关联的标记:</p>
<pre><code>mylabs = "Country Agriculture Mining Manufacturing Power Construction Service Finance Social Transport"
</code></pre>
<p>以下是功能操作的<em>一次性</em>编码(在后续管道中重用):</p>
<pre><code>@Pipe
def split(iterable, delim= ' '):
for s in iterable: yield s.split(delim)
@Pipe
def trim(iterable):
for s in iterable: yield s.strip()
@Pipe
def pzip(iterable,coll):
for s in zip(list(iterable),coll): yield s
@Pipe
def slice(iterable, dim):
if len(dim)==1:
for x in iterable:
yield x[dim[0]]
elif len(dim)==2:
for x in iterable:
for y in x[dim[0]]:
yield y[dim[1]]
@Pipe
def toMap(iterable):
return dict(list(iterable))
</code></pre>
<p>这里是大结局:一条管道中的所有内容:</p>
<pre><code>labels = (rawLabels.split('\n')
| trim
| split(':')
| slice([0])
| pzip(mylabs.split(' '))
| toMap )
</code></pre>
<p>结果是:</p>
<pre><code>print('labels=%s' % repr(labels))
labels={'PS': 'Power', 'Min': 'Mining', 'Country': 'Country', 'SPS': 'Social', 'TC': 'Transport', 'SI': 'Service', 'Con': 'Construction', 'Fin': 'Finance', 'Agr': 'Agriculture', 'Man': 'Manufacturing'}
</code></pre>