有条件地使用较短列表

时间:2017-12-20 22:37:26

标签: python

我是python的新手,当他们遇到一个包含较短列表值的条件时,尝试简单地替换列表的值。

例如:

list1 = [1,0,1,0,1,0]
list2 = [1,2,3]

我想要的输出是:

list1 = [1,1,1,2,1,3]

我可以使用带计数器的循环:

counter = 0
for i, j in enumerate(list1):
    if j == 1:
        list1[i] = list2[counter]
        counter += 1

但是对于这么简单的事情来说这似乎效率低下,所以我猜测可能有一种方法可以通过列表理解来实现这一点,例如:

[list2[i] if j == 0 else j for i,j in enumerate(list1)]

(虽然由于列表长度不同而失败)。

有没有其他简洁的方法在base python中执行此操作,可能使用mapfilter

4 个答案:

答案 0 :(得分:7)

您可以使用由短名单制作的迭代器,并在理解中只需调用next

<!-- Clients -->
    <section class="section-wrap clients bg-light" id="clients">
        <div class="container"> 

                  <div class="row heading">

                        <div class="col-md-6 col-md-offset-3">
                            <h2 class="text-center bottom-line"><b>Clients</b></h2>

                      </div>
                  </div>
                    <div class="row" >
                         <div class="col-xs-6 col-sm-3" style="padding-bottom:20px;">
                            <center>
                                <a href="http://www.at60inches.com/" onMouseOver="MM_swapImage('@60','','img/C-60-over.png',1)" onMouseOut="MM_swapImgRestore()"><img src="img/C-60-up.png" width="150px" alt="@60" id="@60"></a>
                            </center>
                         </div>
                         <div class="col-xs-6 col-sm-3" style="padding-bottom:20px;">
                            <center>
                                <a href="http://andrewalkerhair.com/" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Andre Walker','','img/C-ANDRE-over.png',1)"><img src="img/C-ANDRE-UP.png" alt="Andre Walker Hair" width="150px" id="Andre Walker"></a>
                            </center>
                         </div>
                        <div class="col-xs-6 col-sm-3" style="padding-bottom:20px;">
                            <center>
                                <a href="https://www.domainecarneros.com/" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Domaine Carneros','','img/C-domaie-over.png',1)"><img src="img/C-domaine-up.png" alt="Domaine Carneros by Taittinger"  width="150px" id="Domaine Carneros"></a>
                            </center>
                        </div>
                        <div class="col-xs-6 col-sm-3" style="padding-bottom:20px;">
                            <center>
                                <a href="http://www.tomford.com/" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Tom Ford','','img/C-ford-over.png',1)"><img src="img/C-ford-UP.png" alt="Tom Ford" width="150px" id="Tom Ford"></a>
                            </center>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-xs-6 col-sm-3" style="padding-bottom:20px;">
                            <center>
                                <a href="http://goodwillnynj.org/about-us" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Goodwill','','img/C-Goodwill-over.png',1)"><img src="img/C-Goodwill-up.png" alt="Goodwill NYNJ" width="150px" id="Goodwill"></a>
                            </center>
                        </div>
                        <div class="col-xs-6 col-sm-3" style="padding-bottom:20px;">
                            <center>
                                <a href="http://simplygum.com/" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Simply Gum','','img/C-gum-over.png',1)"><img src="img/C-gum-UP.png" alt="Simply Gum" width="150px" id="Simply Gum"></a>
                            </center>
                        </div>
                        <div class="col-xs-6 col-sm-3" style="padding-bottom:20px;">
                            <center> 
                                <a href="http://www.hearst.com/about" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Hearst','','img/C-hearst-over.png',1)"><img src="img/C-hearst-up.png" alt="Hearst" width="150" id="Hearst"></a>
                            </center> 
                        </div>
                        <div class="col-xs-6 col-sm-3" style="padding-bottom:20px;">
                            <center>
                                <a href="http://bobmackie.com/pages/bobs-bio" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Bob Mackie','','img/C-mackie-over.png',1)"><img src="img/C-mackie-up.png" alt="Bob Mackie" width="150" id="Bob Mackie"></a> 
                            </center>
                        </div>
                    </div>
                    <div class="row">
                         <div class="col-xs-6 col-sm-3" style="padding-bottom:20px;">
                             <center>
                                <a href="http://erstwhilejewelry.com/" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Erstwell','','img/C-erstwell-over.png',1)"><img src="img/C-erstwell-up.png" alt="Erstwell Jewelry" width="200" id="Erstwell"></a>
                            </center>
                         </div>
                         <div class="col-xs-6 col-sm-3" style="padding-bottom:20px;">
                            <center>
                                <a href="http://www.taittinger.fr/" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Taittinger','','img/C-Taittinger-over.png',1)"><img src="img/C-Taittinger-UP.png" alt="Taittinger" width="150" id="Taittinger"></a>
                            </center>
                         </div>
                        <div class="col-xs-6 col-sm-3" style="padding-bottom:20px;">
                            <center>
                                <a href="https://www.youtube.com/watch?v=PRzh7hhsk84" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('WIE Network','','img/C-wie-over.png',1)"><img src="img/C-wie-up.png" alt="WIE Network" width="150" id="WIE Network"></a>
                            </center>
                        </div>
                        <div class="col-xs-6 col-sm-3" style="padding-bottom:20px;">
                            <center>
                                <a href="http://candicekumai.com/about" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Candice','','img/C-candice-over.png',1)"><img src="img/C-candice-up.png" alt="Candice Kumai" width="150" id="Candice"></a>
                            </center>
                        </div>
                  </div>
                 <div class="row">
                    <div class="col-xs-6 col-sm-3" style="padding-bottom:20px;">
                        <center>
                          <a href="https://www.delivery.com/info/about" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('com','','img/C-Delivery-over.png',1)">
                          <img src="img/C-Delivery-up.png" alt="Delivery.com" width="150" id="Delivery.com"></a>
                        </center>
                    </div>
                    <div class="col-xs-6 col-sm-3" style="padding-bottom:20px;">
                        <center>
                          <a href="http://kettlebellkickboxing.com/" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Kettlebell Kickboxing','','img/C-kettle-over.png',1)"><img src="img/C-kettle-UP.png" alt="Kettlebell Kickboxing" width="150" id="Kettlebell Kickboxing"></a>
                        </center>
                    </div>
                    <div class="col-xs-6 col-sm-3" style="padding-bottom:20px;">
                        <center>
                          <a href="http://www.nelsonvercher.com/" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Nelson','','img/C-nelson-over.png',1)"><img src="img/C-nelson-up.png" alt="Nelson Verner" width="150" id="Nelson"></a>
                        </center>
                    </div>
                        <div class="col-xs-6 col-sm-3" style="padding-bottom:20px;">
                        <center>
                            <a href="https://www.mentedcosmetics.com/" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Mented','','img/C-Mented-over.png',1)"><img src="img/C-Mented-up.png" alt="Mented" width="150" id="Mented"></a>   
                        </center>
                    </div>

                </div>
                <div class="row">
                        <div class="col-xs-6 col-sm-3">
                            <center>
                                <a href="http://shortyawards.com/socialgood" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Shorty Social Good Awards','','img/C-ShortyGood-over.png',1)"><img src="img/C-ShortyGood-up.png" alt="Shorty Social Good Awards" width="150" id="Shorty Social Good"></a>
                            </center>
                        </div>
                        <div class="col-xs-6 col-sm-3">
                            <center>
                                <a href="https://www.youtube.com/watch?time_continue=1&v=lIKzgXJxBU8" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Shorty Awards','','img/C-shorty-over.png',1)"><img src="img/C-shorty-up.png" alt="Shorty Awards" width="150" id="Shorty Awards"></a>
                            </center>
                        </div>
                        <div class="col-xs-6 col-sm-3">
                            <center>
                                <a href="http://waxclub.nyc/" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Wax Club','','img/C-WaxClub-over.png',1)"><img src="img/C-WaxClub-up.png" alt="Wax Club" width="150" id="Wax Club"></a>
                            </center>
                        </div>
                        <div class="col-xs-6 col-sm-3"></div>
                </div>
        </div>

请注意,如果填充元素不足,您可以为next提供默认值:

答案 1 :(得分:3)

尝试类似:

<form>
    <fields name="params">
        <fieldset name="basic" label="Color settings">
            <field 
                    name = "eventcolor" 
                    type = "color"
                    default = "frontpage" 
                    validate = "color"
                    class="input-colorpicker"
                    value="#000" 
                    size="10"
                    label = "Kleur"
                    required = "true"
                    description = "Kies een event kleur"
                    

            </field>
        </fieldset>
          
    </fields>
</form>

请注意,这会从[x if x else list2.pop(0) for x in list1] 删除项目。

答案 2 :(得分:1)

你可以试试这个:

list1 = [1, 0, 1, 0, 1, 0]
list2 = [1, 2, 3]

it2 = iter(list2)

[x if x != 0 else next(it2) for x in list1]
# [1, 1, 1, 2, 1, 3]

输出:

list1 = [1,0,1,0,1,0]
list2 = [1,2,3]
new_list = [list2[list1[:i].count(a)] if a != 1 else a for i, a in enumerate(list1)]

答案 3 :(得分:1)

如果您想使用map(),可以试试这个:

from collections import deque

list1 = [1,0,1,0,1,0]
list2 = [1,2,3]

queue = deque(list2)

result = list(map(lambda x : x if x else queue.popleft(), list1))

print(result)

哪个输出:

[1, 1, 1, 2, 1, 3]

注意:我使用了堆栈/队列数据结构collections.deque来允许从前面O(1) popleft(),而不是使用pop(0) },这是O(n)。如果您不想使用此库,则可以事先撤消list2,然后拨打pop(),这也是O(1)