模拟超市购物
- shopping_list = ['milk', 'yoghurt', 'egg', 'butter', 'bread', 'bananas']
- cart = []
- while shopping_list != []:
- article = shopping_list.pop()
- cart.append(article)
- print(article, shopping_list, cart)
- print("shopping_list: ", shopping_list)
- print("cart: ", cart)
- s = "Toronto is the largest City in Canada"
- t = "Python courses in Toronto by Bodenseo"
- # print(list(zip(s,t)))
- ss = "".join(["".join(x) for x in zip(s,t)])
- print(ss) #TPoyrtohnotno ciosu rtshees lianr gTeosrto nCtiot yb yi nB oCdaennasdeao
- print(ss[::2]) #Toronto is the largest City in Canada
- print(ss[1::2]) #Python courses in Toronto by Bodenseo
- x = ["a","b","c"]
- y = [x] * 4
- print(y)
- # [['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c']]
- y[0][0] = "p"
- print(y)
- # [['p', 'b', 'c'], ['p', 'b', 'c'], ['p', 'b', 'c'], ['p', 'b', 'c']]
Python列表方法
列表方法 描述
append() 它将新元素添加到列表的末尾extend() 它通过添加另一个列表、字符串、元组中的元素来扩展列表
insert() 它以所需的索引注入一个新元素,lst.insert(index, object)
remove() 它从列表中删除所需的元素,lst.remove(x)
pop() 它从给定位置删除并返回一个项目,默认最后一项。
clear() 它删除列表的所有元素。
index() 它返回最先匹配的元素的索引。lst.index("v")\lst.index("v",3)\lst.index("v",2,3)
count() 它返回总数。作为参数传递的元素。
sort() 它以递增的方式对列表的元素进行排序。
reverse() 它会反转列表中元素的顺序。
copy() 它执行列表的浅拷贝并返回。
Python列表内置函数
功能 描述
all() 如果列表包含具有True值的元素或为空,则返回True。any() 如果任何成员具有True值,则它也返回True。
enumerate() 它返回一个具有所有列表元素的索引和值的元组。
len() 返回值是列表的大小。
list() 它转换所有可迭代的对象并作为列表返回。
max() 具有最大值的成员
min() 最小值的成员
sorted() 它返回列表的排序副本。
sum() 返回值是列表中所有元素的总和。
# 空白列表
L1 = []
# 整数列表
L2 = [10, 20, 30]
# 异构数据类型列表
L3 = [1, "Hello", 3.4]
创建一个空列表
List()构造函数
Python包含一个内置的list()方法,又名构造函数,它接受序列或元组作为参数,然后转换为Python列表
>>> theList = list([1, 2, [1.1, 2.2]])
创建一个空列表
>>> my_list = [] >>> my_list = list()
List()构造函数
Python包含一个内置的list()方法,又名构造函数,它接受序列或元组作为参数,然后转换为Python列表
>>> theList = list([1, 2, [1.1, 2.2]])
>>> theList
[1, 2, [1.1, 2.2]]
>>> len(theList)
3
List Comprehension
列表推导
>>> theList = [iter for iter in range(5)]
>>> print(theList)
[0, 1, 2, 3, 4]
>>> listofCountries = ["India","America","England","Germany","Brazil","Vietnam"]
>>> firstLetters = [ country[0] for country in listofCountries ]
>>> print(firstLetters)
['I', 'A', 'E', 'G', 'B', 'V']
>>> print ([x+y for x in 'get' for y in 'set'])
['gs', 'ge', 'gt', 'es', 'ee', 'et', 'ts', 'te', 'tt']
>>> print ([x+y for x in 'get' for y in 'set' if x != 't' and y != 'e' ])
['gs', 'gt', 'es', 'et']
>>> months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'] >>> oddMonths = [iter for index, iter in enumerate(months) if (index%2 == 0)]
>>> oddMonths
['jan', 'mar', 'may', 'jul', 'sep', 'nov']
>>> init_list = [0]*3
>>> print(init_list)
[0, 0, 0]
>>> print(two_dim_list)
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> two_dim_list[0][2] = 1
>>> print(two_dim_list)
[[0, 0, 1], [0, 0, 1], [0, 0, 1]]
>>> two_dim_list = [[0]*3 for i in range(3)]
>>> print(two_dim_list)
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> two_dim_list[0][2] = 1
>>> print(two_dim_list)
[[0, 0, 1], [0, 0, 0], [0, 0, 0]]
>>> L1 = ['a', 'b']
>>> L2 = [1, 2]
>>> L3 = ['Learn', 'Python']
>>> L1 + L2 + L3
['a', 'b', 1, 2, 'Learn', 'Python']
列表扩展
>>> L2 = [1, 2]
>>> L3 = ['Learn', 'Python']
>>> L1 + L2 + L3
['a', 'b', 1, 2, 'Learn', 'Python']
>>> L1 = ['a', 'b']
>>> L2 = ['c', 'd']
>>> L1.extend(L2)
>>> print(L1)
['a', 'b', 'c', 'd']
>>> L1 = ['x', 'y']
>>> L1.append(['a', 'b'])
>>> L1
['x', 'y', ['a', 'b']]
列表切片
复制列表
>>> id(theList)
55530056
>>> id(theList[::])
55463496
for index, element in enumerate(theList):
print(index, element)
for index in range(len(theList)):
print(index)
element = it.next() # fetch first value
element = it.next() # fetch second value
newList = sorted(theList, reverse=True)
print("Original list:", theList, "Memory addr:", id(theList))
print("Copy of the list:", newList, "Memory addr:", id(newList))
# Original list: ['a', 'e', 'i', 'o', 'u'] Memory addr: 120082568
# Copy of the list: ['u', 'o', 'i', 'e', 'a'] Memory addr: 120086216
列表更新
list[0]=1
列表删除元素
len(list) 返回列表长度
list.sort():按升序或降序排列整数,浮点数或字符串的列表
列表更新
list[0]=1
列表删除元素
有3种Python方法:list.remove(),list.pop()和del运算符
list.remove("v")
list.pop(0)
del list[0]
list.remove("v")
list.pop(0)
del list[0]
#列表添加
a = []
for x in range(0,10):
a.append(x)
print(a)
# List Comprehension
#列表推导
#列表推导
print([x for x in a])
list_of_squares_2 = [int**2 for int in range(1, 10)]
numbers = [2, 5, 7, 9] print(len(numbers)) >>> 4
max(list)返回列表中的最大值
numbers = [2, 5, 7, 9] print(max(numbers)) >>> 9
min(list):返回列表中的最小值
numbers = [2, 5, 7, 9] print(min(numbers)) >>> 2
list(tuple):将一个元组对象转换为一个列表
animals = ('cat', 'dog', 'fish', 'cow') print(list(animals)) >>> ['cat', 'dog', 'fish', 'cow']
list.append(element):将元素追加到列表中
numbers = [2, 5, 7, 9] numbers.append(15) print(numbers) >>> [2, 5, 7, 9, 15]
list.pop(index):从列表中删除指定索引处的元素,默认最后一项
numbers = [2, 5, 7, 9, 15] numbers.pop(2) print(numbers) >>> [2, 5, 9, 15]
list.remove(element):从列表中删除元素
values = [2, 5, 7, 9] values.remove(2) print(values) >>> [5, 7, 9]list.reverse():反转列表的对象
values = [2, 5, 7, 10] values.reverse() print(values) >>> [10, 7, 5, 2]list.index(element):获取列表中元素的索引值
animals = ['cat', 'dog', 'fish', 'cow', 'goat'] fish_index = animals.index('fish') print(fish_index) >>> 2sum(list):获取列表中所有值的总和,如果值都是数字(整数或小数)
values = [2, 5, 10] sum_of_values = sum(values) print(sum_of_values) >>> 17
values = [1, 7, 9, 3, 5] # To sort the values in ascending order: values.sort() print(values) >>> [1, 3, 5, 7, 9]
values = [2, 10, 7, 14, 50] # To sort the values in descending order: values.sort(reverse = True) print(values) >>> [50, 14, 10, 7, 2]
# to sort the list by length of the elements strings = ['cat', 'mammal', 'goat', 'is'] strings.sort() print(strings) strings.sort(key = len) print(strings)
评论
发表评论