python之列表操作(list)

代码:

# 列表操作功能汇总
print("列表操作功能汇总")
list_demo = ['first', 'second', 'thrid', 'fourth']
# 复制list_demo列表取名list
list = list_demo[:]
print("原列表为:", list)
print("-----------------------------")

print("输出列表第一个元素:", list[0])
print("输出列表最后一个元素:", list[-1])
print("从2个开始到第3个输出列表的元素:", list[1:3])
print("从2个开始到末尾输出列表的元素:", list[1:])
print("从间隔2输出列表的元素:", list[::2])
print("从倒序输出列表的元素:", list[::-1])

print("-----------------------------")

# 修改列表指定位置的值
list = list_demo[:]
list[1] = 2
print("修改后的列表:", list)

# 在列表末尾添加元素
list = list_demo[:]
list.append("hello")
print("列表中插入元素至末尾:", list)

# 扩展列表
list = list_demo[:]
list.extend(["hello", "world"])
print("扩展列表:", list)
list[len(list):] = ["hello", "world"]
print("再次扩展列表:", list)

# 列表合并
list = list_demo[:]
list2 = ["hello", "world"]
list3 = list + list2
print("列表合并:", list3)

# 列表中指定位置插入元素
list = list_demo[:]
list.insert(1, "hello")
print("列表中指定位置插入元素:", list)

print("-----------------------------")

# 删除列表中指定位置的元素
list = list_demo[:]
del list[1]
print("删除列表中指定位置的元素:", list)

# 删除列表中指定位置的元素并返回
list = list_demo[:]
popone = list.pop(1)
print("删除列表中指定位置的元素并记录:", list, "; 删掉的元素是:", popone)

# 删除列表中指定值的元素,不存在则报错
list = list_demo[:]
list.remove("first")
print("删除列表中指定值的数据:", list)

# 清空列表
list = list_demo[:]
list.clear()

print("-----------------------------")
# 列表解析:将for循环和表达式的代码合并成一行
list = [value ** 2 for value in range(1, 5)]
print("列表解析结果:", list)

print("-----------------------------")
# 检查列表中是否有指定的元素:in或not in。
list = list_demo[:]
if "first" in list:
    print("判断'first'在列表中")

print("-----------------------------")
# 判断列表中是否有值
if list:
    print("判断列表中有值。")
else:
    print("判断列表为空。")

print("-----------------------------")

nums = [3, 2, 1, 4]
# 正序排序
nums.sort()
print(nums)
# 反向排序
nums.sort(reverse=True)
print(nums)
# 反向排序
nums.reverse()
print(nums)
""" 
[1, 2, 3, 4]
[4, 3, 2, 1]
[1, 2, 3, 4]
"""

print("-----------------------------")

# 获取列表位置
nums = [3, 2, 1, 4, 5]
print(nums.index(1))
print(nums.index(4, 2, 5))  # 从位置2到位置5找第一个4的index
nums[nums.index(3)] = 0
print(nums)
""" 
2
[0, 2, 1, 4, 5]
"""

 

运行结果:

列表操作功能汇总
原列表为: ['first', 'second', 'thrid', 'fourth']
-----------------------------
输出列表第一个元素: first
输出列表最后一个元素: fourth
从2个开始到第3个输出列表的元素: ['second', 'thrid']
从2个开始到末尾输出列表的元素: ['second', 'thrid', 'fourth']
从间隔2输出列表的元素: ['first', 'thrid']
从倒序输出列表的元素: ['fourth', 'thrid', 'second', 'first']
-----------------------------
修改后的列表: ['first', 2, 'thrid', 'fourth']
列表中插入元素至末尾: ['first', 'second', 'thrid', 'fourth', 'hello']
扩展列表: ['first', 'second', 'thrid', 'fourth', 'hello', 'world']
再次扩展列表: ['first', 'second', 'thrid', 'fourth', 'hello', 'world', 'hello', 'world']
列表合并: ['first', 'second', 'thrid', 'fourth', 'hello', 'world']
列表中指定位置插入元素: ['first', 'hello', 'second', 'thrid', 'fourth']
-----------------------------
删除列表中指定位置的元素: ['first', 'thrid', 'fourth']
删除列表中指定位置的元素并记录: ['first', 'thrid', 'fourth'] ; 删掉的元素是: second
删除列表中指定值的数据: ['second', 'thrid', 'fourth']
-----------------------------
列表解析结果: [1, 4, 9, 16]
-----------------------------
判断'first'在列表中
-----------------------------
判断列表中有值。
-----------------------------
[1, 2, 3, 4]
[4, 3, 2, 1]
[1, 2, 3, 4]
-----------------------------
2
3
[0, 2, 1, 4, 5]

 

posted @ 2017-07-23 09:31  星瑞  阅读(527)  评论(0编辑  收藏  举报