python, del[] 用法, 笨方法學python

Enbu_Yuan發表於2017-03-07
#coding=utf-8
city = ['GZ', 'SZ', 'HY', 'ZH', 'XX']


def del_function():
    print "This is a function to remove an element from list\n."
    print "Before"

    for x in city:
        print "This is before list: %s\n" % x

    del city[1:3] #刪除多個元素。
    del city[0] #刪除1個元素
    #del city #刪除整個list
    print "I just using del to remove an element from list: city."
    print "AFTER"

    for y in city:
        print "This is after list:%s\n" % y

del_function()

1)del用於list列表操作,刪除一個或者連續幾個元素

相關文章