Python list Go heavy

Source: Internet
Author: User
Tags python list

The title has faulty wordings, in fact it is this:

Suppose there are two lists: L1 = [1,2,3,4]; L2 = [1,2,5,6] then removes the L2 element contained in the L1

Direct this certainly is not possible:

def removeexists (L1, L2): For  E1 in L1:    if E1 in L2:      l1.remove (E1)

No matter what language can not do so, but a little different, it does not error and can go down, but the results are not expected.

As a result, since 1 in L1 has been removed, the iterator index points to the second element, but now the second element is 3, so the result of L1 is [2,3,4], and if L1 was originally [1,2,4,5], the result is [2,4], which is quite interesting ...

The solution does not matter, stick it a bit:

def removeexists (L1, L2):   = l1[:]  for in  l1t:    if in L2:      L1.remove (E1)

Python list Go heavy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.