3.9.2 - Lists - Adding and Removing Objects

# 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] //






阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/seamanj/article/details/49899987
个人分类: maya python
想对作者说点什么? 我来说一句

PhoneBook implementation

2012年05月13日 6KB 下载

没有更多推荐了,返回首页

不良信息举报

3.9.2 - Lists - Adding and Removing Objects

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭