python实现将range()函数生成的数字存储在一个列表中


Posted in Python onApril 02, 2020

说明

同学的代码中遇到一个数学公式牵扯到将生成指定的数字存储的一个列表中,那个熊孩子忽然懵逼的不会啦,,,给了博主一个表现的机会,,,哈哈哈好嘛,虽然很简单但还是记录一下吧,,,嘿嘿

一 代码

# coding=utf-8
"""
@author: jiajiknag
程序功能:
"""
# 方法一
lifts = []
for n in range(1,13):
 # lift = 1 +6 * np.sin(np.pi * n/12)
 lift = 1 + n/12
 lifts.append(lift)
print(lifts)

# 方法二
print("------------------------------------")
squares = [1 +i/12 for i in range(1,5)]
print(squares)

二 结果

python实现将range()函数生成的数字存储在一个列表中

好嘛,,,有没有很神奇的节奏!

补充知识:Python 通过range初始化list set 等

啥也不说了,还是直接看代码吧!

"""
01:range()函数调查
02:通过help()函数调查range()函数功能
03:Python中的转义字符
04:使用start、step、stop的方式尝试初始化list、tuple、set等
05:使用len()获取list、set、tuple的长度
"""

help(range)
tempRange = range(1,100,2)
print("type(tempRange): " + str(type(tempRange)))
print("tempRange: " + str(tempRange))

tempStr = ""
for i in range(5): # 注意 输出0到4,包括0和4但不包括5.
 tempStr += (" " + str(i) + " ")
print("for i in range(5) " + tempStr) #for i in range(5) 0 1 2 3 4 

tempStr = ""
for i in range(20,0,-2):
 tempStr += (" " + str(i) + " ")
# 注意看输出不包括0
print("for i in range(20,0,-2) " + tempStr)
"""
for i in range(20,0,-2) 20 18 16 14 12 10 8 6 4 2 
"""


tempStr = ""
for i in [1,2,3]:
 tempStr += (" " + str(i) + " ")
# for i in [1,2,3] 1 2 3 
print("for i in [1,2,3] " + tempStr)


tempStr = ""
for i in "Hello world!":
 tempStr += (" " + str(i) + " ")
# for i in "Hello world!" H e l l o  w o r l d ! 
print("for i in \"Hello world!\" " + tempStr)

print(list(range(5,10))) # 默认步长1,输出:[5, 6, 7, 8, 9]不包括10
print(list(range(0,10,2))) #输出:[0, 2, 4, 6, 8]
print(list(range(10,0,2))) #输出:[]
print(list(range(10,0,-2))) #输出:[10, 8, 6, 4, 2]

# 尝试使用start、step、stop的方式尝试初始化list、tuple、set等
# print(list(1,9,1)) # TypeError: list() takes at most 1 argument (3 given)
# print(set(1,9,1)) # TypeError: set expected at most 1 arguments, got 3
# print(tuple(1,9,1)) # TypeError: tuple() takes at most 1 argument (3 given)

tempList = list(range(0,10,1));
print("list(range(0,10,1)): " + str(tempList))

tempSet = set(range(0,10,1))
print("list(set(0,10,1)): " + str(tempSet))

tempTuple = tuple(range(0,10,1))
print("list(tuple(0,10,1)): " + str(tempTuple))

tempDic = {"num":1}
print("len(list) :" + str(len(tempList))) # len(list) :10
print("len(set) :" + str(len(tempSet))) # len(set) :10
print("len(tuple) :" + str(len(tempTuple))) # len(tuple) :10
print("len(dic) :" + str(len(tempDic))) # len(dic) :1

# list.append [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'b']
tempList.append('b')
print("list.append " + str(tempList))

# set.add {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a'}
tempSet.add('a')
print("set.add " + str(tempSet))

以上这篇python实现将range()函数生成的数字存储在一个列表中就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
pymssql ntext字段调用问题解决方法
Dec 17 Python
Python 爬虫学习笔记之单线程爬虫
Sep 21 Python
Python中将字典转换为列表的方法
Sep 21 Python
python中set()函数简介及实例解析
Jan 09 Python
python中join()方法介绍
Oct 11 Python
pandas每次多Sheet写入文件的方法
Dec 10 Python
Python人脸识别第三方库face_recognition接口说明文档
May 03 Python
python开启debug模式的方法
Jun 27 Python
图文详解Django使用Pycharm连接MySQL数据库
Aug 09 Python
win10下python3.8的PIL库安装过程
Jun 08 Python
如何通过安装HomeBrew来安装Python3
Dec 23 Python
python 将Excel转Word的示例
Mar 02 Python
Python 给下载文件显示进度条和下载时间的实现
Apr 02 #Python
python求前n个阶乘的和实例
Apr 02 #Python
python实现将字符串中的数字提取出来然后求和
Apr 02 #Python
python对指定字符串逆序的6种方法(小结)
Apr 02 #Python
在python中实现求输出1-3+5-7+9-......101的和
Apr 02 #Python
django中url映射规则和服务端响应顺序的实现
Apr 02 #Python
Python实现从N个数中找到最大的K个数
Apr 02 #Python
You might like
discuz安全提问算法
2007/06/06 PHP
让PHP支持页面回退的两种方法
2008/01/10 PHP
php实现图片上传并进行替换操作
2016/03/15 PHP
php正确输出json数据的实例讲解
2018/08/21 PHP
js一组验证函数
2008/12/20 Javascript
JQuery 引发两次$(document.ready)事件
2010/01/15 Javascript
javascript操作ul中li的方法
2015/05/14 Javascript
实例讲解jquery中mouseleave和mouseout的区别
2016/02/17 Javascript
简单解析JavaScript中的__proto__属性
2016/05/10 Javascript
jQuery获取radio选中项的值实例
2016/06/18 Javascript
基于jQuery实现滚动切换效果
2016/12/02 Javascript
详解Angular2表单-模板驱动的表单(Template-Driven Forms)
2017/08/04 Javascript
微信小程序实现无限滚动列表
2020/05/29 Javascript
[01:14]辉夜杯战队访谈宣传片—NEWBEE.Y
2015/12/26 DOTA
[01:07:34]DOTA2-DPC中国联赛定级赛 RNG vs Aster BO3第二场 1月9日
2021/03/11 DOTA
[08:38]DOTA2-DPC中国联赛 正赛 VG vs Elephant 选手采访
2021/03/11 DOTA
用Python编写生成树状结构的文件目录的脚本的教程
2015/05/04 Python
python将ansible配置转为json格式实例代码
2017/05/15 Python
python代码 FTP备份交换机配置脚本实例解析
2019/08/01 Python
使用python实现离散时间傅里叶变换的方法
2019/09/02 Python
Python3 tkinter 实现文件读取及保存功能
2019/09/12 Python
python实现身份证实名认证的方法实例
2019/11/08 Python
python怎么调用自己的函数
2020/07/01 Python
使用CSS媒体查询(Media Queries)和JavaScript判断浏览器设备类型的方法
2014/04/03 HTML / CSS
野兽派官方旗舰店:THE BEAST 野兽派
2016/08/05 全球购物
UNIX文件系统常用命令
2012/05/25 面试题
机械工程师的岗位职责
2013/11/17 职场文书
高中军训感言1000字
2014/03/01 职场文书
双方协议书
2014/04/22 职场文书
北京故宫导游词
2015/01/31 职场文书
前台岗位职责范本
2015/04/16 职场文书
2015年初三班主任工作总结
2015/05/21 职场文书
2019最新婚庆对联集锦!
2019/07/10 职场文书
日本读研:怎样写好一篇日本研究计划书?
2019/07/15 职场文书
goland设置颜色和字体的操作
2021/05/05 Golang
Win10开机修复磁盘错误怎么跳过?Win10关闭开机磁盘检查的方法
2022/09/23 数码科技