python程式設計基礎知識 列表(一)

2022-03-17 07:50:55 字數 4244 閱讀 8713

b=['trek','cannondale','redline','specialized']

print(b)

['trek', 'cannondale', 'redline', 'specialized']
b=['trek','cannondale','redline','specialized']

print(b[0])

trek
b=['trek','cannondale','redline','specialized']

print(b[1])

print(b[-1])

cannondale

specialized

b=['trek','cannondale','redline','specialized']

messge = "my name is "+b[0]+'.'

print(messge)

my name is trek.
b=['trek','cannondale','redline','specialized']

print(b)

b[0]='dau'

print(b)

['trek', 'cannondale', 'redline', 'specialized']

['dau', 'cannondale', 'redline', 'specialized']

1.在列表末尾處新增元素
b=['trek','cannondale','redline','specialized']

print(b)

print(b)

['trek', 'cannondale', 'redline', 'specialized']

['trek', 'cannondale', 'redline', 'specialized', 'cat']

下來呢,我們建立乙個空列表,再在其中新增元素『honda』,『woo』,『hod』

a=

print(a) #輸出最終的結果

['honda', 'woo', 'hod']
2.在列表中插入元素
b=['trek','cannondale','redline','specialized']

b.insert(1,'pig')##在索引1出新增空間,並將值『pig』儲存到這個地方

print(b)

['trek', 'pig', 'cannondale', 'redline', 'specialized']
1.使用del語句刪除元素
b=['trek','cannondale','redline','specialized']

print(b)

del b[0]##刪除了b中第乙個元素『trek』

print(b)

['trek', 'cannondale', 'redline', 'specialized']

['cannondale', 'redline', 'specialized']

2. 使用pop方法刪除元素
b=['trek','cannondale','redline','specialized']

print(b)

popped_b=b.pop()

print(b)

print(popped_b)

['trek', 'cannondale', 'redline', 'specialized']

['trek', 'cannondale', 'redline']

specialized

b=['trek','cannondale','redline','specialized']

led_b = b.pop()

print("the last was " + led_b + '.')

the last was specialized.
3. 彈出列表中任何位置處的元素
b=['trek','cannondale','redline','specialized']

first_b = b.pop(1)

print("i owned was a " + first_b + '.')

i owned was a cannondale.
4. 根據值刪除元素
b=['trek','cannondale','redline','specialized']

print(b)

b.remove('redline')

print(b)

['trek', 'cannondale', 'redline', 'specialized']

['trek', 'cannondale', 'specialized']

b=['trek','cannondale','redline','specialized']

print(b)

tee = 'redline'

b.remove(tee)

print(b)

print("\nthe " + tee + " is too big to me. ")

['trek', 'cannondale', 'redline', 'specialized']

['trek', 'cannondale', 'specialized']

the redline is too big to me.

b=['trek','cannondale','redline','specialized']

b.sort()

print(b)

['cannondale', 'redline', 'specialized', 'trek']
b=['trek','cannondale','redline','specialized']

b.sort(reverse = true)

print(b)

['trek', 'specialized', 'redline', 'cannondale']
cars = ['bmw','audi','toyota','jeep']

print("here is the original list: ")

print(cars)

print("\nhere is the sorted list:")

print(sorted(cars))

print("\nhere is the original list again:")

print(cars)

here is the original list: 

['bmw', 'audi', 'toyota', 'jeep']

here is the sorted list:

['jeep', 'audi', 'bmw', 'toyota']

here is the original list again:

['bmw', 'audi', 'toyota', 'jeep']

cars = ['bmw','audi','toyota','jeep']

print(cars)

cars.reverse()

print(cars)

['bmw', 'audi', 'toyota', 'jeep']

['jeep', 'toyota', 'audi', 'bmw']

cars = ['bmw','audi','toyota','jeep']

len(cars)

未完待續!

python基礎知識 列表

1.新增操作 生成乙個新的列表 extend 接受引數並將該引數的每個元素都新增到原有的列表中,原地修改列表而不是新建列表 insert 插入任意物件到列表中,可以控制插入位置。2.修改 修改列表本身只需要直接賦值操作就行。3.刪除操作 del 我們通過索引刪除指定位置的元素。remove 移除列表...

python 基礎知識 列表

列表就相當於乙個容器,用來存放物件,變數等內容,例如下面幾個列表 list1 list2 list list3 hello 17 true 3.14 list4 hello 17 true print list4 下面講 關於列表的一些操作 print list print list insert ...

Python 列表 基礎知識

方法一 方法二 no repeat l fromkeys l keys 操作一 判斷值是否在 不在列表中in not in操作符 list1 1,2,3,4 a 1 a in list1 true a 5 a in list1 false a not in list1 true操作二 統計指定值在列...