map - 将嵌套的for循环转换为映射等效

时间:2016-05-24 16:02:41

标签: python python-3.x

示例:

>>>for x in iterable1:
...    expression

地图表格将是:

>>>map(lambda x:expression,iterable1)

如何将此扩展为嵌套?

示例:

>>>for x in itr1:
...    for y in itr2:
...        expr

不使用理解。

编辑:仅使用地图。

4 个答案:

答案 0 :(得分:2)

您可以使用itertools.product构建两个嵌套序列的笛卡尔积,并将map表达式添加到2元组的列表中:

from itertools import product

map(lambda (x, y): expression, product(itr1, itr2))

一些实际值的示例:

seq = map(lambda (x, y): '%s:%s' % (x, y), product(itr1, itr2))
for item in seq:
    print item

请注意,lambda (x, y)是将序列中的每个2元组解包到表达式中使用的单独xy参数所必需的。

答案 1 :(得分:1)

请耐心等待。不是解释,但这在2天后起作用。仅使用地图和列表。这是糟糕的代码。欢迎提出缩短代码的建议。 Python 3解决方案

使用列表理解的示例:

>>> a=[x+y for x in [0,1,2] for y in [100,200,300]]
>>> a
[100,200,300,101,201,301,102,202,302]

使用示例:

>>>a=[]
>>>for x in [0,1,2]:
...    for y in [100,200,300]:
...        a.append(x+y)
...
>>>a
[100,200,300,101,201,301,102,202,302]

现在仅使用map:

的示例
>>>n=[]
>>>list(map(lambda x:n.extend(map(x,[100,200,300])),map(lambda x:lambda y:x+y,[0,1,2])))
>>>n
[100,200,300,101,201,301,102,202,302]

更小的python2.7解决方案:

>>>m=[]
>>>map(lambda x:m.extend(x),map(lambda x:map(x,[100,200,300]),map(lambda x:lambda y:x+y,[0,1,2])))
>>>m
[100,200,300,101,201,301,102,202,302]

另一种变化:我通过电子邮件发送给Mark Lutz,这是他的解决方案。这不使用闭包,并且最接近嵌套for循环功能。

>>> X = [0, 1, 2]               
>>> Y = [100, 200, 300]
>>> n = []
>>> t = list(map(lambda x: list(map(lambda y: n.append(x + y), Y)),X))
>>> n
[100,200,300,101,201,301,102,202,302]

答案 2 :(得分:0)

你不能。 lambda特别限于其返回值可以封装为单个表达式的函数:不允许使用语句。

您应该问自己的一个问题是,为什么您认为这是编写Python程序的理想方式?为了便于阅读,已明确定义了该语言,您应尽一切可能保持该可读性。

答案 3 :(得分:0)

Thnks lapin!

我有同样的问题,你给我解决方案:

很长的路:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>


<div class="container">
  <nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
    <a class="navbar-brand active" href="https://www.inplaydesign.com/index.html">d|b</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item">
          <a class="nav-link" href="https://blog.inplaydesign.com">blog</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="/gallery/gallery.html">gallery</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="https://shop.inplaydesign.com">shop</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="/faq/faq.html">f.a.q.</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="/contact/contact.html">contact</a>
        </li>
      </ul>
      <li>
        <button type="button" class="btn btn-outline-dark btn-sml" data-target="#logOut">
                        <i class="fa fa-power-off"></i> log-out</button>
      </li>
    </div>
  </nav>
</div>

<div class="container carousel-container">
  <div class="container container1" id="container2">
    <div class="row">
      <div class="col-lg-8 col-md-8 col-sm-12">
        <div class="carousel slide carousel-fade" id="carousel-0">
          <!-- Carousel items -->
          <div class="carousel-inner">

            <div class="active carousel-item" data-slide-number="0">
              <img class="img-thumbnail" height="450" width="720" height="450" width="720" src="http://via.placeholder.com/720x450">
            </div>
          </div>
        </div>
      </div>
      <div class="col align-self-center carousel-text"></div>
      <div style="display: none;">
        <div class="slide-content" data-slide="0">
          <h2>Skygate (Night)</h2>
          <a href="../downloads/Facebook/skygatenight1fbt.jpg" download>
            <button class="btn btn-outline-dark btn-sm" type="button" style="margin-left: -1px;">Facebook Cover</button>
          </a>
          <div class="btn-group">
            <div class="btn-group btn-group-sm">
              <button type="button" class="btn btn-outline-dark btn-sm dropdown-toggle" data-toggle="dropdown">Desktop</button>
              <div class="dropdown-menu" role="menu">
                <a class="dropdown-item" href="../downloads/Desktop/1024x768" download>1024 x 768 (4:3)</a>
                <a class="dropdown-item" href="../downloads/Desktop/1600x1200" download>1600 x 1200 (4:3)</a>
                <a class="dropdown-item" href="../downloads/Desktop/1280x1024" download>1280 x 1024 (5:4)</a>
                <a class="dropdown-item" href="../downloads/Desktop/1366x768" download>1366 x 768 (16:9)</a>
                <a class="dropdown-item" href="../downloads/Desktop/1920x1080" download>1920 x 1080 (16:9)</a>
                <a class="dropdown-item" href="../downloads/Desktop/1680x1050" download>1680 x 1050 (16:10)</a>
                <a class="dropdown-item" href="../downloads/Desktop/1920x1200" download>1920 x 1200 (16:10)</a>
                <a class="dropdown-item" href="../downloads/Desktop/2560x1600" download>2560 x 1600 (16:10)</a>
                <a class="dropdown-item" href="../downloads/Desktop/2880x1800" download>2880 x 1800 (16:10)</a>
                <a class="dropdown-item" href="../downloads/Desktop/3840x2160" download>3840 x 2160 (UHD 16:9)</a>
                <a class="dropdown-item" href="../downloads/Desktop/4096x2304" download>4096 x 2304 (4K 16:9)</a>
                <a class="dropdown-item" href="../downloads/Desktop/5120x2880" download>5120 x 2880 (5K 16:9)</a>
              </div>
            </div>
            <div class="btn-group btn-group-sm">
              <button type="button" class="btn btn-outline-dark btn-sm dropdown-toggle" data-toggle="dropdown">Mobile</button>
              <div class="dropdown-menu" role="menu">
                <a class="dropdown-item" href="../downloads/Mobile/iPhone5" download>640 x 1136 (iphone 5)</a>
                <a class="dropdown-item" href="../downloads/Mobile/iPhone6" download>750 x 1344 (iphone 6/7)</a>
                <a class="dropdown-item" href="../downloads/Mobile/iPhone6+" download>1920 x 1920 (iphone 6+/7+)</a>
                <a class="dropdown-item" href="../downloads/Mobile/HTC" download>1080 x 1920 (S4, HTC One)</a>
                <a class="dropdown-item" href="../downloads/Mobile/iPad" download>1024 x 1024 (iPad 1&2/Mini)</a>
                <a class="dropdown-item" href="../downloads/Mobile/Surface3" download>2160 x 1440 (Surface 3)</a>
                <a class="dropdown-item" href="../downloads/Mobile/Retina" download>2048 x 2048 (Retina iPad/Mini)</a>
                <a class="dropdown-item" href="../downloads/Mobile/GalaxyS6" download>2560 x 2560 (Galaxy S6)</a>
              </div>
            </div>
            <div class="btn-group btn-group-sm">
              <button type="button" class="btn btn-outline-dark btn-sm dropdown-toggle" data-toggle="dropdown">Dual</button>
              <div class="dropdown-menu" role="menu">
                <a class="dropdown-item" href="../downloads/Dual/2560x1024" download>2560 x 1024</a>
                <a class="dropdown-item" href="../downloads/Dual/3840x1080" download>3840 x 1080</a>
                <a class="dropdown-item" href="../downloads/Dual/3360x1050" download>3360 x 1050</a>
                <a class="dropdown-item" href="../downloads/Dual/3840x1200" download>3840 x 1200</a>
                <a class="dropdown-item" href="../downloads/Dual/5120x1600" download>5120 x 1600</a>
                <a class="dropdown-item" href="../downloads/Dual/8192x2304" download>8192 x 2304</a>
                <a class="dropdown-item" href="../downloads/Dual/7680x2160" download>7680 x 2160</a>
                <a class="dropdown-item" href="../downloads/Dual/10240x2880" download>10240 x 2880</a>
              </div>
            </div>
            <div class="btn-group btn-group-sm">
              <button type="button" class="btn btn-outline-dark btn-sm dropdown-toggle" data-toggle="dropdown">Triple</button>
              <div class="dropdown-menu" role="menu">
                <a class="dropdown-item" href="../downloads/Triple/3840x1024" download>3840 x 1024</a>
                <a class="dropdown-item" href="../downloads/Triple/5760x1080" download>5760 x 1080</a>
                <a class="dropdown-item" href="../downloads/Triple/5040x1050" download>5040 x 1050</a>
                <a class="dropdown-item" href="../downloads/Triple/5760x1200" download>5760 x 1200</a>
                <a class="dropdown-item" href="../downloads/Triple/7680x1600" download>7680 x 1600</a>
                <a class="dropdown-item" href="../downloads/Triple/12280x2304" download>11520 x 2160</a>
                <a class="dropdown-item" href="../downloads/Triple/12280x2304" download>12288 x 2304</a>
                <a class="dropdown-item" href="../downloads/Triple/15360x2880" download>15360 x 2880</a>
              </div>
            </div>
            <div class="btn-group btn-group-sm">
              <button type="button" class="btn btn-outline-dark btn-sm dropdown-toggle" data-toggle="dropdown">
                                    <i class="fa fa-apple" aria-hidden="true"></i>
                                </button>
              <div class="dropdown-menu" role="menu">
                <h6 class="dropdown-header">Dual Screen</h6>
                <a class="dropdown-item" href="../downloads/macOS/dual/2560x1024" download>2560 x 1024</a>
                <a class="dropdown-item" href="../downloads/macOS/dual/3840x1080" download>3840 x 1080</a>
                <a class="dropdown-item" href="../downloads/macOS/dual/3360x1050" download>3360 x 1050</a>
                <a class="dropdown-item" href="../downloads/macOS/dual/3840x1200" download>3840 x 1200</a>
                <a class="dropdown-item" href="../downloads/macOS/dual/5120x1600" download>5120 x 1600</a>
                <a class="dropdown-item" href="../downloads/macOS/dual/8192x2304" download>8192 x 2304</a>
                <a class="dropdown-item" href="../downloads/macOS/dual/7680x2160" download>7680 x 2160</a>
                <a class="dropdown-item" href="../downloads/macOS/dual/10240x2880" download>10240 x 2880</a>
                <h6 class="dropdown-header">Triple Screen</h6>
                <a class="dropdown-item" href="../downloads/macOS/triple/3840x1024" download>3840 x 1024</a>
                <a class="dropdown-item" href="../downloads/macOS/triple/5760x1080" download>5760 x 1080</a>
                <a class="dropdown-item" href="../downloads/macOS/triple/5040x1050" download>5040 x 1050</a>
                <a class="dropdown-item" href="../downloads/macOS/triple/5760x1200" download>5760 x 1200</a>
                <a class="dropdown-item" href="../downloads/macOS/triple/7680x1600" download>7680 x 1600</a>
                <a class="dropdown-item" href="../downloads/macOS/triple/12280x2304" download>11520 x 2160</a>
                <a class="dropdown-item" href="../downloads/macOS/triple/12280x2304" download>12288 x 2304</a>
                <a class="dropdown-item" href="../downloads/macOS/triple/15360x2880" download>15360 x 2880</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="container animated fadeIn" id="thumbnail-view" data-masonry='{ "itemSelector": ".grid-item", "columnWidth": 150 }'>
    <div class="grid">
      <div class="grid-item">
        <a class="hexLink carousel-selector" data-slide="0">
          <img width="150" height="97.5" class="img-thumbnail" src="http://via.placeholder.com/150x97">
        </a>
      </div>
    </div>
  </div>
</div>

简短方法和解决方案

SUITE = 'Heart Diamonds Spades Clubs'.split()
RANKS = '2 3 4 5 6 7 8 9 10 J Q K A'.split()

for x in RANKS:
    for y in SUITE:
        deck.append(x+" " +y)
print(deck)
print(len(deck))