Python Delete List Contents

Source: Internet
Author: User
It's a little embarrassing today

a=[' Xxxx_game.sql ', ' xxxx_game_sp.sql ', ' xxxx_gamelog_sp.sql ', ' xxxx_gamelog.sql ']for i in a:  if ' gamelog ' in I:    a.remove (i) Print a[' Xxxx_game.sql ', ' xxxx_game_sp.sql ', ' xxxx_gamelog.sql ']

The process of the calendar is obviously miss off the ' Xxxx_gamelog.sql ' this project, you can try it yourself, why will not be deleted, what is the reason?

We'll verify it again.

For I in a:  if ' gamelog ' in I:    print I, Xxxx_gamelog_sp.sql xxxx_gamelog.sql

Seeing the result, if we do not remove it, there is no problem. can be completely calendar.

So we probably know that when the remove operation on the list, the method of using the calendar is not possible. How to solve it?

a1=a[::]      #这里我们镜像一个列表a1, but do not use a1=a, why, we can test a1=a[::] a1==a True; A1 is a False; A1=a a1==a true; Home can be tested, this is a feature of the list. For i in A1:  if ' gamelog ' in I:    a.remove (i) print a [' Xxxx_game.sql ', ' xxxx_game_sp.sql ']

One more example.

[{' Num ': ' 001 ', ' Name ': ' Zhang San ', ' workingtime ': ' Monday ', ' money ': ' 100 '}
{' Num ': ' 002 ', ' Name ': ' John Doe ', ' workingtime ': ' Tuesday ', ' money ': ' 200 '}]
Because there is ' Zhang San ', so delete {' Num ': ' 001 ', ' Name ': ' Zhang San ', ' workingtime ': ' Monday ', ' money ': ' 100 '} entire line, how to operate

The idea is to find the index of the element to delete in the list, and then call the pop, index as the parameter. The pop returns the element that was deleted. The rest of the queue is the remaining after the index element is deleted.

LName = [{' num ': ' 001 ', ' name ': ' Zhang San ', ' workingtime ': ' Monday ', ' money ': ' + '} {' num ': ' 002 ', ' name ': ' John Doe ', ' Workingti ' Me ': ' Tuesday ', ' money ': ' $ '}]for x in range (len (lname)):  #列表遍历      if l[x][' name '] = = U ' Zhang San ':        lname.pop (x) 
   #用 pop.    break         #操作完成 and break out

All right, let's get here today.

  • 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.