计算长列表中的数字集 [英] counting sets of numbers in a long list

查看:151
本文介绍了计算长列表中的数字集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我是python编程的新手,因此在编写python程序时遇到了困难.我正在尝试计算一个长长的列表中一组由三个数字和制表符组成的七个对象(?).然后,我需要查找列表中最大的数字集(以三的倍数表示).数字由制表符分隔,数字集以7表示.例如:

As I am new to python programming, I am having difficulty writing a python program. I am trying to count a set of seven objects(?) of three numbered digits and tabs within a long list. Then I need to find which set of numbers (in multiples of three's) have the maximum number along the list. The numbers are separated by a tab and the set of numbers are in seven's. For example:

['128','130','140','145','','','','283','379','','','','','','175','183','187','','','',''etc.]

列表中的第一组数字和制表符是128、130、140、145,制表符,制表符,制表符.列表中的第二组数字和制表符是283、379,制表符,制表符,制表符,制表符,制表符.最后,列表中的第三组数字是175、183、187,制表符,制表符,制表符,制表符.

The first set of numbers and tabs in the list are 128, 130, 140, 145, tab, tab, tab. The second set of numbers and tabs in the list are 283, 379, tab, tab, tab, tab, tab. Finally, the third set of numbers in the list are 175, 183, 187, tab, tab, tab, tab.

我想计算7组数字和制表符中的3个数字,然后让最大输出数字显示3个数字.例如:

I would like to count the three number digits in the seven sets of numbers and tabs, and then have a maximum output number of which set shows the most three digit numbers. For example:

['128','130','140','145','','','','283','379','','','','','','175','183','187','','','','']
this first set = 4                this second set = 2        this third set = 3 

在此示例中,最终输出数字应为4,因为第一组七个对象显示了最多3位数字.这是我目前拥有的东西.

In this example the final output number should be 4, because the first set of seven object revealed the most 3 digit numbers. Here is my what I currently have.

#!/usr/bin/env python

allele = '128   130 140 145             283 379                     175 183 187                 
elementlist=allele.split('\t')
string= str(elementlist)
type = string.replace('\t','0')

print type

我将不胜感激.

推荐答案

如果您需要的只是最长的段,则可能只想保留最长段的起点和长度的引用,因为这样您就可以d避免复制内存中不需要的许多元素.这对于非常大的数据结构非常有用.在这种情况下,您可能需要使用以下内容:

If all you need is the longest segment, you might want to keep just a reference to the starting point and length of the longest segment, because that way you'd avoid copying a lot of elements that you don't need in memory. This is quite useful for very large data structures. In that case, you might want to use something like this:

def longest_segment(target_list, empty_element):
 longest_start = longest_len = 0
 current_start = current_len = 0     
 i=0          
 for element in target_list:
   if element == empty_element:
      current_start = -1
      current_len   = 0
   else:
      if(current_start == -1):
        current_start = i
      current_len = current_len + 1            
   if( current_len > longest_len ):
      longest_start = current_start
      longest_len   = current_len           
   i = i + 1                       
 return longest_start,longest_len     

用法示例:

L = ['128','130','140','145','','','','283','379',
 '','','','','','175','183','187','','','','']

#The function supports a generic empty element so you could use other separators, like tab
start, size = longest_segment(L,'') 

print ("The longest segment starts at:\t" ,start)
print ("The longest segment has length:\t",size )

#Up to this moment,  there is no need to copy or keep more elements in memory.  
print ("The longest segment is:\t", L[start:start + size])

这篇关于计算长列表中的数字集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆