Python 中 List 的操作要比你想象中要灵活得多
# -*- coding: utf-8 -*-
'''
===========================================================
# Python List 常用操作
# author: xiawp
# date: 2018/12/14
===========================================================
'''
# 定义List
list = ["java", "C", "C++", "C#", "php", "python"]
print (list)
print ("-" * 100)
# 下标取元素
print ("index 1 = ",list[1])
# 负数下标取元素
print ("index -1 = ", list[-1])
print ("index -3 = ", list[-3])
# 多下标一并取出为子集合
print ("index 2 and index 6 :", list[2:6])
# 当下标不存在时抛异常
#print ("index 18 = ", list[18]) 直接报错 IndexError:list index out of range
#print ("index 18 and index 4 ", list[18,4]) 直接报错 IndexError:list index out of range
#print ("index 4 and index 18 ", list[4,18]) 直接报错 IndexError:list index out of range
# List 追加
list.append(" Javascript ")
print (list)
# List插入
list.insert(4,"HTML")
print (list)
# List继承
list.extend(["CSS","JQuery"])
print (list)
# 根据元素取下标
print ("php 的所在下标是 ",list.index("php"))
# 当入参元素不存在时抛异常
#print ("Linux 的所在下标是 ",list.index("Linux")) 直接报错 ValueError:'Linux' is not in list
# 根据元素删除List中的元素
list.remove("C++")
#当所删元素不存在时抛异常
#list.remove("Linux") 直接报错 ValueError:list.remove(x): x not in list
print (list)
# 弹出最后一个元素
print (list.pop())
print (list)
# List参与"加"运算
list = list + ["Redis", "Oracle", "MySQL"] #与extend相似
# 等价于:list = list.extend([...])
# 等价于:list += [...]
print (list)
# List 参与 “乘” 运算
list *= 2
print (list)
# List 没 “除” 和 “减”运算
#list /= 2
#list -= ["Redis", "Oracle", "MySQL"]
# ‘ " ’ 和 ‘ ' ’ 赋值字符没区别
list = ['java', 'C', 'C#', 'HTML', 'php', 'python', ' Javascript ', 'CSS', 'JQuery']
print (list)
# ** 使用join链接list成为字符串 **
param = {"身高":180,"体重":65,"性别":"男","姓名":"张小明"}
print (param)
list2 = ["%s:%s" % (k,v) for k , v in param.items()]
print (list2)
#joinValue = " 》".join(["%s:%s" % (k,v) for k , v in param.items()])
joinValue = " 》".join(list2)
print (joinValue)
# ** List 分割字符串 **
student = joinValue
print(student.split(" 》"))
print(student.split(" 》", 2))
# ** List 的映射解析 **
list3 = [1,5,9,3,5,7,0]
#print ([elem*10 for elem in list3])
print ([elem<<2 for elem in list3])
# ** dictionary中的解析 **
print(param.keys())
list4 = param.keys()
print (list4)
print(param.values())
list5 = param.values()
print (list5)
# items 表示返回list内部的元数组集合
print (param.items())
# 返回List 类型数据
print ([k for k, v in param.items()])
list6 = [k for (k, v) in param.items()] #用(k,v)这样写更直观
list7 = [v for k, v in param.items()]
print (list7)
#print (["%s : %s" % k,v for k, v in param.items()]) 直接编译不通过:SyntaxError: invalid syntax
print (["%s : %s" % (k,v) for k, v in param.items()])
# ** List 过滤 **
print([elem for elem in list if elem == 'java'])
#print ([elem for elem in list7 if elem > 5]) 混合数据类型的时候直接比较可能会报错
print ([elem for elem in list if len(elem) > 5])
#print ([elem for elem in list7 if len(elem) > 5]) 直接报错
print ([elem for elem in list7 if type(elem) == type(180)])
print ([e for e in list7 if isinstance(e,str)])
print ([e for e in list7 if isinstance(e,int) and e >100])
# List.count 函数表示统计元素在该List集合中出现的次数
print ([e for e in list7 if list7.count(e)==1])
print ("-" * 100)
print ("list:",list)
print ("list2:",list2)
print ("list3:",list3)
print ("list4:",list4)
print ("list5:",list5)
print ("list6:",list6)
print ("list7:",list7)
print ("param:",param)
print ("joinValue",joinValue)
print ("student",student)
'''
===========================================================
---------- EditPlus Python37 ----------
['java', 'C', 'C++', 'C#', 'php', 'python']
----------------------------------------------------------------------------------------------------
index 1 = C
index -1 = python
index -3 = C#
index 2 and index 6 : ['C++', 'C#', 'php', 'python']
['java', 'C', 'C++', 'C#', 'php', 'python', ' Javascript ']
['java', 'C', 'C++', 'C#', 'HTML', 'php', 'python', ' Javascript ']
['java', 'C', 'C++', 'C#', 'HTML', 'php', 'python', ' Javascript ', 'CSS', 'JQuery']
php 的所在下标是 5
['java', 'C', 'C#', 'HTML', 'php', 'python', ' Javascript ', 'CSS', 'JQuery']
JQuery
['java', 'C', 'C#', 'HTML', 'php', 'python', ' Javascript ', 'CSS']
['java', 'C', 'C#', 'HTML', 'php', 'python', ' Javascript ', 'CSS', 'Redis', 'Oracle', 'MySQL']
['java', 'C', 'C#', 'HTML', 'php', 'python', ' Javascript ', 'CSS', 'Redis', 'Oracle', 'MySQL', 'java', 'C', 'C#', 'HTML', 'php', 'python', ' Javascript ', 'CSS', 'Redis', 'Oracle', 'MySQL']
['java', 'C', 'C#', 'HTML', 'php', 'python', ' Javascript ', 'CSS', 'JQuery']
{'身高': 180, '体重': 65, '性别': '男', '姓名': '张小明'}
['身高:180', '体重:65', '性别:男', '姓名:张小明']
身高:180 》体重:65 》性别:男 》姓名:张小明
['身高:180', '体重:65', '性别:男', '姓名:张小明']
['身高:180', '体重:65', '性别:男 》姓名:张小明']
[4, 20, 36, 12, 20, 28, 0]
dict_keys(['身高', '体重', '性别', '姓名'])
dict_keys(['身高', '体重', '性别', '姓名'])
dict_values([180, 65, '男', '张小明'])
dict_values([180, 65, '男', '张小明'])
dict_items([('身高', 180), ('体重', 65), ('性别', '男'), ('姓名', '张小明')])
['身高', '体重', '性别', '姓名']
[180, 65, '男', '张小明']
['身高 : 180', '体重 : 65', '性别 : 男', '姓名 : 张小明']
['java']
['python', ' Javascript ', 'JQuery']
[180, 65]
['男', '张小明']
[180]
[180, 65, '男', '张小明']
----------------------------------------------------------------------------------------------------
list: ['java', 'C', 'C#', 'HTML', 'php', 'python', ' Javascript ', 'CSS', 'JQuery']
list2: ['身高:180', '体重:65', '性别:男', '姓名:张小明']
list3: [1, 5, 9, 3, 5, 7, 0]
list4: dict_keys(['身高', '体重', '性别', '姓名'])
list5: dict_values([180, 65, '男', '张小明'])
list6: ['身高', '体重', '性别', '姓名']
list7: [180, 65, '男', '张小明']
param: {'身高': 180, '体重': 65, '性别': '男', '姓名': '张小明'}
joinValue 身高:180 》体重:65 》性别:男 》姓名:张小明
student 身高:180 》体重:65 》性别:男 》姓名:张小明
输出完成 (耗时 0 秒) - 正常终止
===========================================================
'''