list1 = ['Google', 'Runoob', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"];
list1 = ['Google', 'Runoob', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print ("list1[0]: ", list1[0]) print ("list2[1:5]: ", list2[1:5])
list = ['Google', 'Runoob', 1997, 2000] print ("第三个元素为 : ", list[2]) list[2] = 2001 print ("更新后的第三个元素为 : ", list[2])
list = ['Google', 'Runoob', 1997, 2000] print ("原始列表 : ", list) del list[2] print ("删除第三个元素 : ", list)
list1 = ['Google', 'Runoob', 'Taobao'] print (len(list1)) list2=list(range(5)) # 创建一个 0-4 的列表 print (len(list2))
list1, list2 = ['Google', 'Runoob', 'Taobao'], [456, 700, 200] print ("list1 最大元素值 : ", max(list1)) print ("list2 最大元素值 : ", max(list2))
list1, list2 = ['Google', 'Runoob', 'Taobao'], [456, 700, 200] print ("list1 最小元素值 : ", min(list1)) print ("list2 最小元素值 : ", min(list2))
aTuple = (123, 'Google', 'Runoob', 'Taobao') list1 = list(aTuple) print ("列表元素 : ", list1) str="Hello World" list2=list(str) print ("列表元素 : ", list2)
list1 = ['Google', 'Runoob', 'Taobao'] list1.append('Baidu') print ("更新后的列表 : ", list1)
aList = [123, 'Google', 'Runoob', 'Taobao', 123]; print ("123 元素个数 : ", aList.count(123)) print ("Runoob 元素个数 : ", aList.count('Runoob'))
list1 = ['Google', 'Runoob', 'Taobao'] list2=list(range(5)) # 创建 0-4 的列表 list1.extend(list2) # 扩展列表 print ("扩展后的列表:", list1)
# 语言列表 language = ['French', 'English', 'German'] # 元组 language_tuple = ('Spanish', 'Portuguese') # 集合 language_set = {'Chinese', 'Japanese'} # 添加元组元素到列表末尾 language.extend(language_tuple) print('新列表: ', language) # 添加集合元素到列表末尾 language.extend(language_set) print('新列表: ', language)
list1 = ['Google', 'Runoob', 'Taobao'] print ('Runoob 索引值为', list1.index('Runoob')) print ('Taobao 索引值为', list1.index('Taobao'))
list1 = ['Google', 'Runoob', 'Taobao'] list1.insert(1, 'Baidu') print ('列表插入元素后为 : ', list1)
list1 = ['Google', 'Runoob', 'Taobao'] list1.pop() print ("列表现在为 : ", list1) list1.pop(1) print ("列表现在为 : ", list1)
list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] list1.remove('Taobao') print ("列表现在为 : ", list1) list1.remove('Baidu') print ("列表现在为 : ", list1)
list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] list1.reverse() print ("列表反转后: ", list1)
aList = ['Google', 'Runoob', 'Taobao', 'Facebook'] aList.sort() print ( "List : ", aList)
# 列表 vowels = ['e', 'a', 'u', 'o', 'i'] # 降序 vowels.sort(reverse=True) # 输出结果 print ( '降序输出:', vowels )
# 获取列表的第二个元素 def takeSecond(elem): return elem[1] # 列表 random = [(2, 2), (3, 4), (4, 1), (1, 3)] # 指定第二个元素排序 random.sort(key=takeSecond) # 输出类别 print ('排序列表:', random)
list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] list1.clear() print ("列表清空后 : ", list1)
list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] list2 = list1.copy() print ("list2 列表: ", list2)