Tuesday, 15 June 2010

python - Large list generation optimization -


I need a python function that will take a list of strings:

  SAC = ['A [0]', 'B [2: 5]', 'A [4]']  

and a new list of "extended" elements of preserve back Order, such as:

  extended = ['a [0]', 'b [2]', 'b [3]', 'b [4]', 'b [5] ] To achieve my goal I have written this simple task:  
  def expand_seq (seq): # ['' [4] Item for item for Xango in item [i] ['% S [% s]'% [item in item Em.split ('['] [0], i) xrange (IT (item.split ('['] [- 1] [: - 1] .split (':') [0]), integer .split ('['] [- 1] [: - 1]. Split (':') [- 1]) + 1)]  

When dealing with a sequence If it produces less than 500 items then it works well, but it is very slow when generating large lists (more than 1 million) for example:

  # Let's generate one million items! Seq = ['A [1: 5000000]', 'B [1: 5000000]'] T1 = time.clock () seq = expansion_sec (ceek) t2 = time.cock () print round (t2-t 1, 3) # Result: 9.541 seconds  

I am looking for ways to improve this function and if I have any suggestions given to deal with big lists, So I would love to hear them!

Speed ​​of 35% of the following:

  import re r = Recompile (r \ "(\ w +) \ [(\ d +) (? :: (\ d +))? \" "DEF extension_sec (CEC): results = [] item for seq: m = R.match (item) name, start, end = m.group (1), int (m.group (2)), m.group (3) rng = xrange (start, end (end)) if otherwise end (  

With this code:

Ul>

  • We compile a regular expression for use in the function.
  • Drugs are inserted directly.

  • No comments:

    Post a Comment