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

所属分类: 脚本专栏 / python 阅读数: 2091
收藏 0 赞 0 分享

说明

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

一 代码

# 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初始化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()函数生成的数字存储在一个列表中就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

pandas的qcut()方法详解

这篇文章主要介绍了pandas的qcut()方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

从列表或字典创建Pandas的DataFrame对象的方法

这篇文章主要介绍了从列表或字典创建Pandas的DataFrame对象的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas.DataFrame的pivot()和unstack()实现行转列

这篇文章主要介绍了pandas.DataFrame的pivot()和unstack()实现行转列,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

pandas中的series数据类型详解

这篇文章主要介绍了pandas中的series数据类型详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas 时间格式转换的实现

这篇文章主要介绍了pandas 时间格式转换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python中时间、日期、时间戳的转换的实现方法

这篇文章主要介绍了python中时间、日期、时间戳的转换的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas进行时间数据的转换和计算时间差并提取年月日

这篇文章主要介绍了pandas进行时间数据的转换和计算时间差并提取年月日,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法

这篇文章主要介绍了详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python和c语言的主要区别总结

在本篇文章里小编给各位整理了关于python和c语言的主要区别的相关知识帖内容,有需要的朋友们学习阅读下。
收藏 0 赞 0 分享

选择Python写网络爬虫的优势和理由

在本篇文章里小编给各位整理了一篇关于选择Python写网络爬虫的优势和理由以及相关代码实例,有兴趣的朋友们阅读下吧。
收藏 0 赞 0 分享
查看更多