python 列表的建立

2022-01-16 13:40:47 字數 4732 閱讀 6131

python 字典

資料型別的分類

可變(不可雜湊)的:list dict set

不可變(可雜湊)的:str bool int tuple

1. 列表的建立

方法一:

l1 = [1,2,"哈哈哈"]

方法二:

li = list()
方法三:列表推導式

2. 增

insert

extend

li = ["520","python",123]

print(li)

li.insert(1,"哈哈哈") #按索引插入,把「哈哈哈」插入到索引為1的位置

print(li)

li.extend("abc123") #extend是迭代增加

print(li)

none

li = ["蘋果","香蕉","菠蘿"]

li.extend(["番茄","青菜"])

print(li)

print(li)

['蘋果', '香蕉', '菠蘿', '番茄', '青菜']

['蘋果', '香蕉', '菠蘿', '番茄', '青菜', ['中國', '上海']]

3.刪
#pop()

li = ["蘋果","香蕉","菠蘿","火龍果","西瓜","柿子"]

print(li.pop()) #預設刪除最後乙個元素

print(li.pop(2)) #按索引刪

print(li)

柿子

菠蘿['蘋果', '香蕉', '火龍果', '西瓜']

#remove()

li = ["蘋果","香蕉","蘋果","西瓜"]

li.remove("蘋果")

print(li)

['香蕉', '蘋果', '西瓜']
#clear

li = ["蘋果","香蕉","蘋果","西瓜"]

li.clear()

print(li)

#del

li = ["蘋果","香蕉","菠蘿","蘋果","西瓜","水蜜桃","橘子"]

del li[3] #按索引刪

print(li)

del li[::3] #按切片刪

print(li)

del li #把這個li刪了。print(li)會報錯

['蘋果', '香蕉', '菠蘿', '西瓜', '水蜜桃', '橘子']

['香蕉', '菠蘿', '水蜜桃', '橘子']

4.改
#直接改

li = ["蘋果","香蕉","水蜜桃","橘子"]

li[-2] = "hhhh"

print(li)

['蘋果', '香蕉', 'hhhh', '橘子']
#切片改

li = ["蘋果","香蕉","菠蘿","蘋果","西瓜","水蜜桃","橘子"]

li[2:] = "hh"

print(li)

['蘋果', '香蕉', 'h', 'h']
如果要變成 li = ["蘋果","香蕉","hh"] 怎麼辦呢?

#方法一:

li = ["蘋果","香蕉","菠蘿","蘋果","西瓜","水蜜桃","橘子"]

del li[2:]

print(li)

#方法二

li = ["蘋果","香蕉","菠蘿","蘋果","西瓜","水蜜桃","橘子"]

li[2:]=['hh']

5.查
li = ["蘋果","香蕉","菠蘿","蘋果","西瓜","水蜜桃","橘子"]

print(li[-3])

print(li[::2])

西瓜

['蘋果', '菠蘿', '西瓜', '橘子']

資料型別的分類

可變(不可雜湊)的:list dict set

不可變(可雜湊)的:str bool int tuple

1. 列表的建立

方法一:

l1 = [1,2,"哈哈哈"]

方法二:

li = list()
方法三:列表推導式

2. 增

insert

extend

li = ["520","python",123]

print(li)

li.insert(1,"哈哈哈") #按索引插入,把「哈哈哈」插入到索引為1的位置

print(li)

li.extend("abc123") #extend是迭代增加

print(li)

none

li = ["蘋果","香蕉","菠蘿"]

li.extend(["番茄","青菜"])

print(li)

print(li)

['蘋果', '香蕉', '菠蘿', '番茄', '青菜']

['蘋果', '香蕉', '菠蘿', '番茄', '青菜', ['中國', '上海']]

3.刪
#pop()

li = ["蘋果","香蕉","菠蘿","火龍果","西瓜","柿子"]

print(li.pop()) #預設刪除最後乙個元素

print(li.pop(2)) #按索引刪

print(li)

柿子

菠蘿['蘋果', '香蕉', '火龍果', '西瓜']

#remove()

li = ["蘋果","香蕉","蘋果","西瓜"]

li.remove("蘋果")

print(li)

['香蕉', '蘋果', '西瓜']
#clear

li = ["蘋果","香蕉","蘋果","西瓜"]

li.clear()

print(li)

#del

li = ["蘋果","香蕉","菠蘿","蘋果","西瓜","水蜜桃","橘子"]

del li[3] #按索引刪

print(li)

del li[::3] #按切片刪

print(li)

del li #把這個li刪了。print(li)會報錯

['蘋果', '香蕉', '菠蘿', '西瓜', '水蜜桃', '橘子']

['香蕉', '菠蘿', '水蜜桃', '橘子']

4.改
#直接改

li = ["蘋果","香蕉","水蜜桃","橘子"]

li[-2] = "hhhh"

print(li)

['蘋果', '香蕉', 'hhhh', '橘子']
#切片改

li = ["蘋果","香蕉","菠蘿","蘋果","西瓜","水蜜桃","橘子"]

li[2:] = "hh"

print(li)

['蘋果', '香蕉', 'h', 'h']
如果要變成 li = ["蘋果","香蕉","hh"] 怎麼辦呢?

#方法一:

li = ["蘋果","香蕉","菠蘿","蘋果","西瓜","水蜜桃","橘子"]

del li[2:]

print(li)

#方法二

li = ["蘋果","香蕉","菠蘿","蘋果","西瓜","水蜜桃","橘子"]

li[2:]=['hh']

5.查
li = ["蘋果","香蕉","菠蘿","蘋果","西瓜","水蜜桃","橘子"]

print(li[-3])

print(li[::2])

西瓜

['蘋果', '菠蘿', '西瓜', '橘子']

python列表建立操作 python列表操作

建立列表 sample list a 1,a b python 列表操作 sample list a b 0,1,3 得到列表中的某乙個值 value start sample list 0 end value sample list 1 刪除列表的第乙個值 del sample list 0 在列...

python列表建立操作 python列表操作

列表是最常用的python資料型別,它可以作為乙個方括號內的逗號分隔值出現。列表的資料項不需要具有相同的型別。如 list a b 2,5,1 1 新建列表 stus 建立空列表 stus1 list 建立空列表 print stus print stus1 stus 范冰冰 維達 soon 上述 ...

Python建立列表

list1 none 5使用這種方式建立乙個普通的一維列表是沒問題的 可以檢測一下 list1 none 5list1 3 1print list1 none none none,1 none 但是,當用這種方式建立乙個多維列表時,就會出現以下問題 list2 none 5 5 list2 2 3 ...