从左到右在python3中的列表上应用操作

jav*_*dba 7 python functional-programming list operator-precedence

有没有办法在列表中实现非懒惰的从左到右调用操作python

例如 scala

 val a = ((1 to 50)
  .map(_ * 4)
  .filter( _ <= 170)
  .filter(_.toString.length == 2)
  .filter (_ % 20 == 0)
  .zipWithIndex
  .map{ case(x,n) => s"Result[$n]=$x"}
  .mkString("  .. "))

  a: String = Result[0]=20  .. Result[1]=40  .. Result[2]=60  .. Result[3]=80
Run Code Online (Sandbox Code Playgroud)

虽然我意识到许多人不喜欢上面的语法,但我喜欢从左到右移动并随时添加任意操作的能力.

for当有三个或更多操作时,python 理解是不容易阅读的.结果似乎是我们需要将所有内容分解成块.

[f(a) for a in g(b) for b in h(c) for ..]
Run Code Online (Sandbox Code Playgroud)

有没有机会提到这种方法?

注意:我尝试了一些库,包括toolz.functoolz.python3懒惰的评估很复杂:每个级别返回一个map对象.此外,它不能显示它可以在输入上操作list.

jav*_*dba 6

来自@JohanL 的答案在查看标准 python 库中最接近的等价物方面做得很好。

我最终在 2019 年 11 月改编了 Matt Hagy 的一个要点,现在在 pypi

https://pypi.org/project/infixpy/

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
Run Code Online (Sandbox Code Playgroud)

其他答案中描述的其他方法

较旧的方法

我在 2018 年秋季发现了一个更有吸引力的工具包

https://github.com/dwt/fluent

在此处输入图片说明

在对可用的第三方库进行相当彻底的审查后,似乎Pipe https://github.com/JulienPalard/Pipe最适合需求。

您可以创建自己的管道函数。我把它用于处理下面显示的一些文本。粗体线是工作发生的地方。所有这些@Pipe东西我只需要编码一次,然后就可以重复使用。

这里的任务是关联第一个文本中的缩写:

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"""
Run Code Online (Sandbox Code Playgroud)

使用第二个文本中的关联标签:

mylabs = "Country Agriculture Mining Manufacturing Power Construction Service Finance Social Transport"
Run Code Online (Sandbox Code Playgroud)

这是功能操作的一次性编码(在后续管道中重用):

@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))
Run Code Online (Sandbox Code Playgroud)

这是大结局 :全部在一个管道中:

labels = (rawLabels.split('\n') 
     | trim 
     | split(':')
     | slice([0])
     | pzip(mylabs.split(' '))
     | toMap )
Run Code Online (Sandbox Code Playgroud)

结果:

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'}
Run Code Online (Sandbox Code Playgroud)