关闭

3.9.2 - Lists - Adding and Removing Objects

220人阅读 评论(0) 收藏 举报
分类:

# Python List Examples

a = [] #Empty List

b = [0, 1, 2, 3, 4] #List with 5 items

c = [3.14, "abc", [1, 2, 3, ["a", "b", "c"]]]  #List with a nested list

d = [5, 6, 7, 8]

a.append("a")
a.append("b")
a.append("c")

a = ["a", "b", "c"]
a.extend(b)
len(a)

a = [] #Empty List
a.append("Python")

a = [] #Empty List
a.extend("Python")

b.insert(0, "a") # before the index position
b.insert(3, "b")

b.remove("b")  #delete by the object

#an error will occur if you want to delete an inexistent element


del d[0]  #delete by the index


结果

a = [] #Empty List
b = [0, 1, 2, 3, 4] #List with 5 items
c = [3.14, "abc", [1, 2, 3, ["a", "b", "c"]]]  #List with a nested list
d = [5, 6, 7, 8]
a.append("a")
a.append("b")
a.append("c")
a = ["a", "b", "c"]
a.extend(b)
a
// Result: ['a', 'b', 'c', 0, 1, 2, 3, 4] //
len(a)
// Result: 8 //
a = [] #Empty List
a.append("Python")
a
// Result: ['Python'] //
a = [] #Empty List
a.extend("Python")
a
// Result: ['P', 'y', 't', 'h', 'o', 'n'] //
b.insert(0, "a") # before the index position
b
// Result: ['a', 0, 1, 2, 3, 4] //
b.insert(3, "b")
b
// Result: ['a', 0, 1, 'b', 2, 3, 4] //
b.remove("b")  #delete by the object
b
// Result: ['a', 0, 1, 2, 3, 4] //
d
// Result: [5, 6, 7, 8] //
del d[0]  #delete by the index
d
// Result: [6, 7, 8] //






0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:418394次
    • 积分:10148
    • 等级:
    • 排名:第1801名
    • 原创:594篇
    • 转载:91篇
    • 译文:0篇
    • 评论:96条
    最新评论