本文介绍了建立基于时间的箱子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好,我是python和这个小组的新手,我正在尝试构建一些 垃圾箱,并且想知道是否有人能帮助我。我是否因为如何开始而感到有点失落。 我有一些文本文件与其他2个字段一起存档 格式如下>> 1231 23 56 1232 25 79 1234 26 88 1235 22 34 1237 31 85 1239 35 94 这在12小时内持续。期。我希望能够在一行中将附加字段的低值和高值放置 分为5分钟。所以它看起来像这样>> 1235 22 88 1240 31 94 我希望有道理。我应该使用像numarray这样的模块来支付这个,或者是否可以只使用原生函数?任何想法 都会对我有所帮助。 谢谢你 - 马库斯 Hello, I''m new to python and this group and am trying to build somebins and was wondering if any of you could kindly help me out. I''m abit lost on how to begin. I have some text files that have a time filed along with 2 other fieldsformatted like this >> 1231 23 561232 25 791234 26 881235 22 341237 31 851239 35 94 This goes on throughout a 12hr. period. I''d like to be able to placethe low and high values of the additional fields in a single linedivided into 5min intervals. So it would look something like this >> 1235 22 881240 31 94 I hope that makes sense. Should I be using a module like numarray forthis, or is it possible to just use the native functions? Any ideaswould help me very much. Thank you - Marcus 推荐答案 这样的事情可以做到: def splitter(可迭代): """采用基于行的迭代器,产量每行的值列表 编辑此代码以进行更复杂的基于行的解析(如果需要)""" for iterable: yield [line(item)for line.split()] def groupkey(data): """ Groups times以5分钟的分辨率。注意这个版本不起作用 与示例完全相同 - 所以必要时修复"" time = data [0] 返回时间/ 100 * 100 +(时间%100)/ 5 * 5 def grouper(可迭代): """对时间进行分组和汇总" ,groupby中的数据(可迭代,组密钥): data_x = zip(* data)#transform来自的数据cols到行 打印时间,min(data_x [1]),max(data_x [2]) #练习它: source =""" 1231 23 56 1232 25 79 1234 26 88 1235 22 34 1237 31 85 1239 35 94 """ This sort of thing would do it: from itertools import groupby def splitter(iterable):"""Takes a line-based iterator, yields a list of values per lineedit this for more sophisticated line-based parsing if required"""for line in iterable:yield [int(item) for item in line.split()] def groupkey(data):"""Groups times by 5 min resolution. Note this version doesn''t workexactly like the example - so fix if necessary"""time = data[0]return time / 100 * 100 + (time % 100) / 5 * 5 def grouper(iterable):"""Groups and summarizes the lines"""for time, data in groupby(iterable, groupkey):data_x = zip(*data) #transform the data from cols to rowsprint time, min(data_x[1]), max(data_x[2]) # Exercise it: source = """1231 23 561232 25 791234 26 881235 22 341237 31 851239 35 94""" 请按时间注意此组每个5分钟结束,而不是像你的例子那样开始的。如果需要更改,请修改groupkey HTH Michael Note this groups by the time at the end of each 5 mins, rather than thebeginning as in your example. If this needs changing, fix groupkey HTH Michael 你是(非常)新的计算机编程吗?这个学校是否是b $ b家庭作业?问的原因是练习不需要数据结构比一维整数数组更复杂 (如果有人怀疑时间总是在提升订单),和 * NO *数据结构,如果一个人信任。它可以很容易地完成,而不需要任何额外的模块或库,几乎可以用任何计算机语言来发布。所以,这不是一个真正的Python问题。也许你应该看看一些基本的计算机编程学习。 Python *是一个非常棒的语言 - 查看Python网站。 无论如何,这是一种方法 - 只有输入和输出 安排是特定于Python的。而且你不需要它*。*。*(尚) :-) HTH, John =========================== C:\ junk>类型mcd.py #看,马,没有进口! lines =""" \ 1231 23 56 1232 25 79 1234 26 88 1235 22 34 1237 31 85 1239 35 94 "" DUMMY = 9999 bintm = DUMMY for line in lines.split(''\ n''):#在实践中,打开(''input_file'',''r''): 如果不是行:继续 ilist = [int(fld)for fld in line.strip()。split()] print" ilist:",ilist klock, lo,hi = ilist newbintm =((klock + 4)// 5 * 5)%2400 print" bintm =%d,klock =%d, newbintm =%d" %(bintm,klock, newbintm) if newbintm!= bintm: if bintm!= DUMMY: print" ==>> %04d%02d%02d" %(bintm,binlo,binhi) bintm,binlo,binhi = newbintm,lo,hi else: binlo = min(binlo ,lo) binhi = max(binhi,hi) print" end of file ..." if bintm!= DUMMY: print" ==>> %4d%2d%2d %(bintm,binlo,binhi) C:\ junk> python mcd.py ilist:[1231,23,56] bintm = 9999,klock = 1231,newbintm = 1235 ilist:[1232,25,79] bintm = 1235,klock = 1232,newbintm = 1235 ilist:[1234,26,88] bintm = 1235,klock = 1234,newbintm = 1235 ilist:[1235 ,22,34] bintm = 1235,klock = 1235,newbintm = 1235 ilist:[1237,31,85] bintm = 1235,klock = 1237,newbintm = 1240 ==>> 1235 22 88 ilist:[1239,35,94] bintm = 1240,klock = 1239,newbintm = 1240 文件结束... ==>> 1240 31 94 C:\ junk> ===================== =========== Are you (extremely) new to computer programming? Is this schoolhomework? The reason for asking is that the exercise requires no datastructure more complicated than a one-dimensional array of integers(if one doubts that the times will always be in ascending order), and*NO* data structures if one is trusting. It can be done easily withoutany extra modules or libraries in just about any computer languageever invented. So, it''s not really a Python question. Perhaps youshould be looking at some basic computer programming learning. Python*is* a really great language for that -- check out the Python website. Anyway here''s one way of doing it -- only the input and outputarrangements are Python-specific. And you don''t need iter*.*.* (yet):-) HTH,John===========================C:\junk>type mcd.py# Look, Ma, no imports!lines = """\1231 23 561232 25 791234 26 881235 22 341237 31 851239 35 94"""DUMMY = 9999bintm = DUMMYfor line in lines.split(''\n''): # in practice, open(''input_file'', ''r''):if not line: continueilist = [int(fld) for fld in line.strip().split()]print "ilist:", ilistklock, lo, hi = ilistnewbintm = ((klock + 4) // 5 * 5) % 2400print "bintm = %d, klock = %d, newbintm = %d" % (bintm, klock,newbintm)if newbintm != bintm:if bintm != DUMMY:print "==>> %04d %02d %02d" % (bintm, binlo, binhi)bintm, binlo, binhi = newbintm, lo, hielse:binlo = min(binlo, lo)binhi = max(bin hi)print "end of file ..."if bintm != DUMMY:print "==>> %4d %2d %2d" % (bintm, binlo, binhi) C:\junk>python mcd.pyilist: [1231, 23, 56]bintm = 9999, klock = 1231, newbintm = 1235ilist: [1232, 25, 79]bintm = 1235, klock = 1232, newbintm = 1235ilist: [1234, 26, 88]bintm = 1235, klock = 1234, newbintm = 1235ilist: [1235, 22, 34]bintm = 1235, klock = 1235, newbintm = 1235ilist: [1237, 31, 85]bintm = 1235, klock = 1237, newbintm = 1240==>> 1235 22 88ilist: [1239, 35, 94]bintm = 1240, klock = 1239, newbintm = 1240end of file ...==>> 1240 31 94 C:\junk>================================ 大声笑,是的,我对编程比较陌生......而且对python来说很新。 我有使用循环的经验,如果thens和布尔操作, 但是我还没有使用列表或数组'那么...所以这是我的 first forray。这不是功课,我很长时间没有上学。我一直想要扩展我的编程能力,我选择了python作为 意味着实现这个目标......到目前为止我真的很喜欢它:-) 谢谢你们的代码。我最终和约翰一起工作了,因为 这对我来说有点容易。我非常感谢 代码...它教会了我很多关于python如何转换 字符串'到整数的事情,反之亦然。我不希望得到它, 但是看了一下后,我做了,并且能够修改它以便 我可以使用我自己的文件。是的! 我唯一的问题是能够在 bin中总计一个字段。使用sum(hi)只返回最后一个值...我不确定 如何在脚本贯穿每一行时累加值。 Any指针? 再次感谢您的帮助。 Marcus Lol, yes, I am relatively new to programming... and very new to python.I have experience working with loops, if thens, and boolean operations,but I haven''t worked with lists or array''s as of yet... so this is myfirst forray. This isn''t homework, I''m long out of school. I''ve beenwanting to extend my programming abilities and I chose python as themeans to acheiving that goal... so far I really like it :-) Thank you both for the code. I ended up working with John''s becauseit''s a bit easier for me to get through. I very much appreciate thecode... it taught me quite a few things about how python convertsstring''s to integers and vice versa. I didn''t expect to get thorugh it,but after looking at it a bit, I did, and was able to modify it so thatI could work with my own files. Yeah! The only question I have is in regards to being able to sum a field ina bin. Using sum(hi) returns only the last value... I''m uncertain howto cumulatively add up the values as the script runs through each line.Any pointers? Thank you again for all your help.Marcus 这篇关于建立基于时间的箱子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 18:44