python中的数字操作
我不熟悉脚本语言。我有一个excel(xls,2003)文件,它有以下编号python中的数字操作,python,excel,Python,Excel,我不熟悉脚本语言。我有一个excel(xls,2003)文件,它有以下编号 2 48 3 49 6 57 11 89 19 120 29 110 32 105 我正在努力做到以下几点 阅读excel文件:我做了 找出第2列中两个连续数字之间的正负差值 在步骤2定义的第2列中,当差值在正或负意义上最大时,在第1列中找到相应的数字 我已经完成了以下读取excel文件的脚本,但我不确定如何继续 import xlrd myexcel =
2 48
3 49
6 57
11 89
19 120
29 110
32 105
我正在努力做到以下几点
import xlrd
myexcel = xlrd.open_workbook('sample.xls')
#print "WorkSheets:", myexcel.sheet_by_names()
sheet = myexcel.sheet_by_index(0)
c = sheet.col_values(1)
print c
#data = [] #make a data store
我期待下面的打印
最大正差:11
最大负差值:29给你:
col1 = [2, 3, 6, 11, 19, 29, 32]
col2 = [48, 49, 57, 89, 120, 110, 105]
pd, nd, pi, ni = 0, 0, -1, -1
for i in range(len(col2)-1):
d = col2[i+1] - col2[i]
if d > 0 and d > pd:
pd, pi = d, i
if d < 0 and abs(d) > nd:
nd, ni = abs(d), i
print "Max positive difference :" + str(col1[pi+1])
print "Max negative difference :" + str(col1[ni+1])
更新:简短版本
col1 = [2, 3, 6, 11, 19, 29, 32]
col2 = [48, 49, 57, 89, 120, 110, 105]
m = [(x[1] - x[0]) for x in zip(col2[:1] + col2, col2 + col2[-1:])]
print "Max positive difference :" + str(col1[m.index(max(m))])
print "Max negative difference :" + str(col1[m.index(min(m))])
我想这正是你想要的
(max_pos_difference, max_pos_row_col_1_value, neg_row_col_1_value, max_neg_row_col_1_value) = get_max_row()
print "Pos val: {} Pos row: {} Neg val: {} Neg row: {}".format(max_pos_difference, max_pos_row_col_1_value, neg_row_col_1_value, max_neg_row_col_1_value)
def get_max_row():
max_pos_difference = 0
max_neg_difference = 0
max_pos_row = 0
max_neg_row = 0
for rownum in range(sheet.nrows):
if rownum <= sheet.nrows - 1:
this_row_value = sheet.cell(rownum, 1).value
next_row_value = sheet.cell(rownum+1, 1).value
difference = next_row_value - this_row_value
if difference > max_pos_difference and difference >= 0:
max_pos_difference = difference
max_pos_row = rownum
if difference < max_neg_difference and difference < 0:
max_neg_difference = difference
max_neg_row = rownum
return (max_pos_difference, sheet.cell(max_pos_row, 0).value, max_neg_difference, sheet.cell(max_neg_row, 0).value
(最大位置差、最大位置行列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列
打印“Pos val:{}Pos row:{}Neg val:{}Neg row:{}”。格式(max_Pos_差值、max_Pos_row_colu 1_值、Neg_row_colu 1_值、max_Neg_row_colu 1_值)
def get_max_row():
最大位置差=0
最大负差值=0
最大位置行=0
最大负行=0
对于范围内的行数(sheet.nrows):
如果rownum max_pos_差值和差值>=0:
最大位置差异=差异
最大位置行=行数
如果差值<最大负差值且差值<0:
最大负差值=差值
最大负行数=行数
返回值(最大位置差,工作表单元格(最大位置行,0)。值,最大负位置差,工作表单元格(最大负位置行,0)。值
您可以在不过度使用局部变量、循环和条件的情况下尝试此操作:
#! /usr/bin/python
col1 = [2, 3, 6, 11, 19, 29, 32]
col2 = [48, 49, 57, 89, 120, 110, 105]
difs = [ (a, c - b) for a, b, c in zip (col1 [1:], col2, col2 [1:] ) ]
print (max (difs, key = lambda x: x [1] ) [0] )
print (max (difs, key = lambda x: -x [1] ) [0] )
请提供示例输出。因此,您想要
6,11
?因为89-57是最大的。输出应该是这样的:最大负差=2919不是最大差,它应该是11。对吗?ATOZTOA,感谢您的快速帮助。我真的很感激。我对编码非常陌生。只是想知道如何以您使用的格式放置数据。我是使用xlrd.Hi Wadester从excel文件中提取数据,似乎出现了一些问题。我得到了无效的语法(&&replacement&&by'and',它传递了语法错误,但编译器不喜欢最后一行。它给出了以下错误缩进错误:未缩进与任何外部缩进级别不匹配最后一行max_rom shoul中有一个打字错误d是max_row。它仍然给出识别错误。在返回之前还有一个额外的空间。我已经编辑过了。我没有在解释器中实际尝试过,只是穿上了,所以很抱歉,但现在应该可以了。我想知道如何将excel中的数据格式化为您使用的方式。阅读您的代码,我想是用co之类的方式l1=sheet.col_值(1)
和col2=sheet.col_值(2)
。但我不知道API;这只是一个有根据的猜测。
#! /usr/bin/python
col1 = [2, 3, 6, 11, 19, 29, 32]
col2 = [48, 49, 57, 89, 120, 110, 105]
difs = [ (a, c - b) for a, b, c in zip (col1 [1:], col2, col2 [1:] ) ]
print (max (difs, key = lambda x: x [1] ) [0] )
print (max (difs, key = lambda x: -x [1] ) [0] )