
正文
python中pr函数 python pr
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
pr = '%d %s %d = ' % (nums[0], op, nums[1]) 在python里是什么意思?
这是用%来作格式化输出,"
"内的%d、%s将被对应的nums[0]、op的值自动替换掉。
比如:nums=[1,2]
,op="+"
pr实际就等于:"1+2",
相关问答
Q1: python中pow的用法
python中python中pr函数的pow函数的功能是计算x的y次幂。
math模块pow()方法的语法:
import math
math.pow( x, y )
内置的pow()方法:
pow(x, y[, z])
函数是计算x的y次方python中pr函数,如果z在存在,则再对结果进行取模,其结果等效于pow(x,y) %z。
注意:pow()通过内置的方法直接调用,内置方法会把参数作为整型,而math模块则会把参数转换为float。
参数:x --数值表达式。y --数值表达式。z --数值表达式。
返回值:返回xy(x的y次方)的值。
以下展示python中pr函数了使用pow()方法的实例:
一、在命令行中的使用
1、pow(x,y):这个是表示x的y次幂。
pow(2,4)
16
2、pow(x,y,z):这个是表示x的y次幂后除以z的余数。
pow(2,4,5)
1
二、在IDE中的使用
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import math #导入math模块
print "math.pow(100, 2) : ", math.pow(100, 2)
#使用内置,查看输出结果区别
print "pow(100, 2) : ", pow(100, 2)
print "math.pow(100, -2) : ", math.pow(100, -2)
print "math.pow(2, 4) : ", math.pow(2, 4)
print "math.pow(3, 0) : ", math.pow(3, 0)
Q2: Python程序,定义一个 prime() 函数求整数 n 以内(不包括n)的所有素数(1不是素数)
定义一个 prime() 函数求整数 n 以内(不包括n)的所有素数(1不是素数),br并返回一个按照升序排列的素数列表。使用递归来实现一个二分查找算法br函数bi_search(),该函数实现检索任意一个整数在 prime() 函数生成的素数列br表中位置(索引)的功能,并返回该位置的索引值,若该数不存在则返回 -1。brbr输入格式:br第一行为正整数 nbr接下来若干行为待查找的数字,每行输入一个数字br输出格式:br每行输出相应的待查找数字的索引值br输入样例:br10br2br4br6br7br输出样例:br0br-1br-1br3br
Q3: (python编程)使用函数输出斐波那契数列:[-1, -1, -2, -3, -5, -8, -13, -21, -34, -55](添加到列表)?
你看看你递归代码的复杂度 是O(2^n) 而第二个的复杂度是O(n) 运行效率当然不同COUNTER = 0
def fibn(n):
global COUNTER
COUNTER += 1
if n == 0:
return 1
elif n == 1:
return 1
else:
return fibn(n-1) + fibn(n-2)
statistics = []
for i in range(35):
COUNTER = 0
fibn(i + 1)
statistics.append(((i + 1), COUNTER))
print statistics[(1, 1), (2, 3), (3, 5), (4, 9), (5, 15), (6, 25), (7, 41), (8, 67), (9, 109), (10, 177), (11, 287), (12, 465), (13, 753), (14, 1219), (15, 1973), (16, 3193), (17, 5167), (18, 8361), (19, 13529), (20, 21891), (21, 35421), (22, 57313), (23, 92735), (24, 150049), (25, 242785), (26, 392835), (27, 635621), (28, 1028457), (29, 1664079), (30, 2692537), (31, 4356617), (32, 7049155), (33, 11405773), (34, 18454929), (35, 29860703)]做了一个简单的proflieimport cProfile
import pstats
def fibn(n):
if n == 0:
return 1
elif n == 1:
return 1
else:
return fibn(n-1) + fibn(n-2)
print ' i, calls, time'
for i in range(50):
pr = cProfile.Profile()
pr.enable()
fibn(i)
pr.disable()
stats = pstats.Stats(pr)
stats.strip_dirs()
st = stats.stats[('test1.py', 3, 'fibn')]
print '%3d, %10d, %8f' % (i, st[1], st[3])
i, calls, time 0, 1, 0.000000 1, 1, 0.000001 2, 3, 0.000003 3, 5, 0.000005 4, 9, 0.000008 5, 15, 0.000012 6, 25, 0.000020 7, 41, 0.000033 8, 67, 0.000165 9, 109, 0.000088 10, 177, 0.000141 11, 287, 0.000228 12, 465, 0.000450 13, 753, 0.000601 14, 1219, 0.001016 15, 1973, 0.003561 16, 3193, 0.002593 17, 5167, 0.004372 18, 8361, 0.007097 19, 13529, 0.011073 20, 21891, 0.018552 21, 35421, 0.032467 22, 57313, 0.051762 23, 92735, 0.095383 24, 150049, 0.133490 25, 242785, 0.212390 26, 392835, 0.352861 27, 635621, 0.578204 28, 1028457, 0.987839 29, 1664079, 1.506812 30, 2692537, 2.682802 31, 4356617, 3.998936 32, 7049155, 8.089419 33, 11405773, 13.058235 34, 18454929, 23.930004 35, 29860703, 36.503880目测fibn(50)要算出来需要两周
Q4: python排序问题,给出一个txt文件,怎样按分数高低进行排序?
把你每条数据的格式发出来看下。
不然不好弄。
其实就是通过正则表达式等方式python中pr函数,把分数提取出来python中pr函数,然后排序就行python中pr函数了。
给你两个函数,你自己组合吧
#构建一个列表,filename是你txt文件的路径,构建的列表,按照分数进行排序并返回,参数reverse为False则为升序,为True则为降序
def build_list(filename):
import re
reslist = []
fp = open(filename)
textlist = fp.readlines()
fp.close()
pattern = re.compile("(\\d*)\\.\\s*(\\d*\\.\\d*)\\s*([\\s\\S]*(?=\\(\\d*\\)))\\((\\d*)\\)\\s*([\\w,]*)")
for text in textlist:
pr = pattern.search(text)
if pr:
reslist.append((pr.group(1),pr.group(2),pr.group(3),pr.group(4),pr.group(5)))
reslist.sort(key = lambda x:float(x[1]))
return reslist
#根据输入的year参数,返回year年度的电影资料的列表,year为字符串,movielist为使用上面的build_list函数生成的列表
def list_movie_by_year(movielist,year):
return [x for x in movielist if x[3] == year]
如,你数据的路径为d:\data.txt
则
sorted_list = build_list('d:/data.txt')
想找1993年的数据,
则调用函数
list_movie_by_year(sorted_list,'1993')
Q5: pr()函数在python叫什么?
Python 有办法将任意值转为字符串:将它传入repr()或str()函数。 函数str()用于将值转化为适于人阅读的形式,而repr()转化为供解释器读取的形式。
关于python中pr函数和python pr的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








