python基础列表list

# -*- coding: utf-8 -*-

#创建一个列表

number_list = [1, 3, 5, 7, 9]

string_list = ["abc", "bbc", "python"]

mixed_list = ['python', 'java', 3, 12]


#访问列表中的值

second_num = number_list[1]

third_string = string_list[2]

fourth_mix = mixed_list[3]
//格式化输出
print("second_num: {0} third_string: {1} fourth_mix: {2}".format(second_num, third_string, fourth_mix))

#更新列表
print("number_list before: " + str(number_list))

number_list[1] = 30

print("number_list after: " + str(number_list))

#删除列表元素
print("mixed_list before delete: " + str(mixed_list))

del mixed_list[2]

print("mixed_list after delete: " + str(mixed_list))

#Python脚本语言

print(len([1,2,3])) #长度
print([1,2,3] + [4,5,6]) #组合
print(['Hello'] * 4) #重复
print(3 in [1,2,3]) #某元素是否在列表中

#列表的截取
abcd_list =['a', 'b', 'c', 'd'] 
print(abcd_list[1])
print(abcd_list[-2])
print(abcd_list[1:])

# 列表操作包含以下函数:
# 1、cmp(list1, list2):比较两个列表的元素 
# 2、len(list):列表元素个数 
# 3、max(list):返回列表元素最大值 
# 4、min(list):返回列表元素最小值 
# 5、list(seq):将元组转换为列表 
# 列表操作包含以下方法:
# 1、list.append(obj):在列表末尾添加新的对象
# 2、list.count(obj):统计某个元素在列表中出现的次数
# 3、list.extend(seq):在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
# 4、list.index(obj):从列表中找出某个值第一个匹配项的索引位置
# 5、list.insert(index, obj):将对象插入列表
# 6、list.pop(obj=list[-1]):移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
# 7、list.remove(obj):移除列表中某个值的第一个匹配项
# 8、list.reverse():反向列表中元素
# 9、list.sort([func]):对原列表进行排序

  输出结果

second_num: 3 third_string: python fourth_mix: 12
number_list before: [1, 3, 5, 7, 9]
number_list after: [1, 30, 5, 7, 9]
mixed_list before delete: ['python', 'java', 3, 12]
mixed_list after delete: ['python', 'java', 12]
3
[1, 2, 3, 4, 5, 6]
['Hello', 'Hello', 'Hello', 'Hello']
True
b
c
['b', 'c', 'd']

Process finished with exit code 0

 

posted @ 2017-12-22 16:29  雷大侠!  阅读(232)  评论(0编辑  收藏  举报