组合两个嵌套列表并删除重复项

时间:2017-10-15 05:29:41

标签: python list set

我想首先删除重复项来组合两个嵌套列表。

list1 = [[1,2], [1,3], [3,5], [4,1], [9,6]]
list2 = [[1,2], [1,3], [3,5], [6,6], [0,2], [1,7], [7,7]]
results = [[1,2], [1,3], [3,5], [4,1], [9,6], [6,6], [0,2], [1,7], [7,7]]

我的代码:

not_in_list1 = set(list2) - set(list1)
results = list(list1) + list(not_in_list1)

错误:

TypeError: unhashable type: 'list'

是否因为set操作无法在嵌套列表中使用?

由于

3 个答案:

答案 0 :(得分:2)

  

是否因为嵌套列表中不能使用set操作?

是的,这是因为列表是可变的,因此列表可以在创建后更改,这意味着集合中使用的哈希值可以更改。

但是元组是不可变的,所以你可以在集合中使用它们:

<div class="container-fluid" >
  <!-- style="height: 90%; width: 60%; float:left;" height="100%" width="49%" align="left" -->
  <div class="youtube-video" id="video">
    <!-- for live video -->
    <iframe src="https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2F1277805348996425%2Fvideos%2F<?php echo $liveID; ?>%2F&show_text=0&width=476" width="476" height="476" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true" allowFullScreen="true"></iframe>
    <!-- <iframe class="embed-responsive-item" src="https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2F<?php echo $liveID; ?>%2Fvideos%2F1277978488979111%2F&show_text=1&width=560" width="560" height="475" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true" allowFullScreen="true"></iframe> -->
    <!-- <iframe src="https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2F1277805348996425%2Fvideos%2F1278782988898661%2F&show_text=1&width=560" width="560" height="475" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true" allowFullScreen="true"></iframe> -->
    <!-- for embbedded facebook video (test purposes) -->
    <!-- <iframe src="https://www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2FDota2BestYolo%2Fvideos%2F<?php echo $liveID; ?>%2F&show_text=1&width=560" width="560" height="451" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true" allowFullScreen="true"></iframe> -->

    <!-- youtube embed video -->
   <!-- <iframe class="embed-responsive-item" src="https://www.youtube.com/embed/live_stream?channel=UCJaiEVEFaen5QC28rJp0fEw"></iframe> -->
  </div>

  <div class="chat row" >
    <div id="messages" class="chat-area"></div>
      <?php
            if (loggedin()) { ?>
                <table>
                  <tr>
                    <td>
                      <textarea style="padding: 10px;" rows="3" cols="50" class="entry row" placeholder="Type your message here..." name="msg" id="txtBox"></textarea>
                    </td>
                  </tr>
                </table>
                <?php } else { ?>
                  <table>
                  <tr>
                    <td style="width: 400px;">
                      <textarea style="" id="message" rows="3" cols="50" class="entry row" placeholder="Type your message here..." name="msg"></textarea>

                        <!-- Modal -->
                        <div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                          <div class="modal-dialog" role="document">
                            <div class="modal-content">
                              <div class="modal-header">
                                <h5 class="modal-title" id="exampleModalLabel">Please Login</h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                  <span aria-hidden="true">&times;</span>
                                </button>
                              </div>
                              <div class="modal-body">
                                <p>You must first login before you can join the conversation.</p>
                              </div>
                              <div class="modal-footer">
                                <input type="button"  class="btn btn-primary" value="Login" onclick="location.href='login.html'"/>
                                <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
                              </div>
                            </div>
                          </div>
                        </div>
                    </td>
                </tr>
                </table>
             <?php } ?>
 </div> 

</div><!-- end of container -->

将它们转换为元组:

 list1 = [[1,2], [1,3], [3,5], [4,1], [9,6]]
 list2 = [[1,2], [1,3], [3,5], [6,6], [0,2], [1,7], [7,7]]

tuple1 = [tuple(l) for l in list1] tuple2 = [tuple(l) for l in list2] not_in_tuples = set(tuple2) - set(tuple1) 的结果:

not_in_tuples

并在 {(0, 2), (1, 7), (6, 6), (7, 7)} 中将它们组合回您想要的内容:

results

产生:

results = list1 + list(map(list, not_in_tuples))

编辑

如果有兴趣在将两个列表添加到一起后保留两个列表的顺序:

[[1, 2], [1, 3], [3, 5], [4, 1], [9, 6], [0, 2], [1, 7], [7, 7], [6, 6]]

产生:

list1 = [[1,2], [1,3], [3,5], [4,1], [9,6]]
list2 = [[1,2], [1,3], [3,5], [6,6], [0,2], [1,7], [7,7]]

intersection = set(map(tuple, list1)).intersection(set(map(tuple, list2)))

result = list1 + [list(t) for t in map(tuple, list2) if t not in intersection]

答案 1 :(得分:1)

您需要将子列表[mutable]转换为元组[immutable]并获取集合

async componentWillMount() {
  let products = await this.props
  .searchLocations(
    this.props.navigation.state.params.product_name, 
    this.props.navigation.state.params.searchRadius
  )
  .then(
    () => 
        Promise.all(this.props.locations.map(async(location) =>
          ({[location.id]: await this.searchProducts(
            this.props.navigation.state.params.product_name, 
            location.storeid
          )}))
        )
  );
  console.log(products);
}

输出:

set([tuple(i) for i in list1+list2])

答案 2 :(得分:1)

另一种方法是:

>>> list1 = [[1,2], [1,3], [3,5], [4,1], [9,6]]
>>> list2 = [[1,2], [1,3], [3,5], [6,6], [0,2], [1,7], [7,7]] 
>>> k = list1+list2 #We combine both the lists
>>> z = [] #Declare an empty list
>>>for i in k: #Loop through every element of the combined list
       if i in z: #If the element is already in the final list
           pass #Do nothing
       else: #If the element in the combined list is not not there in the final list
           z.append(i) #Append that element to the final list
>>>print z
>>>[[1, 2], [1, 3], [3, 5], [4, 1], [9, 6], [6, 6], [0, 2], [1, 7], [7, 7]]