ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python基础之列表推导式

2019-10-20 16:01:18  阅读:182  来源: 互联网

标签:index 推导 python List 列表 range print


#列表推导式 ---> 返回的是列表  for语句 效率更高
# 1*1 2*2 3*3 4*4 5*5 6*6 7*7 8*8 9*9
# import time
# to = time.clock() #时间戳
# Li = []
#
# for i in range(1,10):
# Li.append(i)
# # print(str(i) + "*" + str(i),end=" ")
# print("for循环消耗的时间是:{a}".format(a = time.clock() -to)) # 8.55257868800022e-06
# print("\n")

#列表推导式
# List = [str(index) + "*" + str(index) for index in range(1,10)]
# print(List)
# print("列表推导式消耗的时间是:{a}".format(a = time.clock() -to)) # 6.613994185386836e-05

#for 求1-100以内的偶数
# List = []
# for i in range(1,101):
# if i % 2 == 0:
# List.append(i)
# print("1-100以内的偶数:",List)

#列表推导式
# List = [i for i in range(1,101) if i % 2 == 0]
# print("1-100以内的偶数:",List)

#for + 字典
# List = []
# d = {"course":"Python","price":"66"}
# for index,value in d.items():
# # print(index,"-->",value)
# List.append(index + "-->" + value)
# print(List)


#列表生成式 全排列
# List = [m + "--->" + n for m in ["Python","Selenium","Jenkins","Appium"] for n in ["11","33","55","77"]]
# print(List)

#把所有的字母转换成大写 --for循环
# newList = []
# List = ["python","java","selenium"]
# for index in List:
# newList.append(index.upper()) #srt.upper() 用于字符串小写转成大写
# print(newList)

#列表推导式
# List = ["python","java","selenium"]
# res = [index.upper() for index in List ]
# print(res)


# os模块 目录相关内置库

import os
# print(os.listdir())
# res = [dirname for dirname in os.listdir("..")]
# print(res)

# res = [dirname for dirname in os.listdir(".") if dirname.endswith("*函数.py")]
# print(res)


#列表推导式:取出名字长度大于3的人员
# nameList = ["body","jim","jerry","tom","python"]
# newList = [index for index in nameList if len(index)>3]
# print(newList)

#列表推导式: M = [[1,2,3],[4,5,6],[7,8,9]],求3,6,9组成的列表
#方法一:
# M = [[1,2,3],[4,5,6],[7,8,9]]
# List = []
# for index in M:
# List.append(index[2])
# print(List)

#方法二:
# M = [[1,2,3],[4,5,6],[7,8,9]]
# List = [index[2] for index in M ]
# print(List)

#列表推导式: M = [[1,2,3],[4,5,6],[7,8,9]],求1,5,9组成的列表
# M = [[1,2,3],[4,5,6],[7,8,9]]
# List = [M[index][index] for index in range(len(M))]
# print(List)

#列表推导式:求(x, y),其中x是0-5之间的偶数,y是0-5之间的奇数组成的元祖列表
# List = [(x,y) for x in range(6) if x%2==0 for y in range(6) if y%2!=0]
# print(List)

#列表推导式:生成间隔5分钟的时间列表序列
List = ["%02d:%02d"%(h,m) for h in range(25) for m in range(1,61,5)]
print(List)

标签:index,推导,python,List,列表,range,print
来源: https://www.cnblogs.com/Teachertao/p/11707857.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有