问题
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.]
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.
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
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
I would appreciate any thoughts or concerns.
回答1:
Just a sketch:
>>> L = ['128','130','140','145','','','','283','379','','','','','','175','183','187','','','','']
Subgroups:
>>> L1 = [L[i : i+7] for i in range(0, len(L), 7)]
>>> L1
[['128', '130', '140', '145', '', '', ''],
['283', '379', '', '', '', '', ''],
['175', '183', '187', '', '', '', '']]
Elements in a subgroup:
>>> L2 = [sum(x.isdigit() for x in SL) for SL in L1]
>>> L2
[4, 2, 3]
Maximum:
>>> max(L2)
4
回答2:
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
Usage example:
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])
来源:https://stackoverflow.com/questions/34012713/counting-sets-of-numbers-in-a-long-list