Skip to main content
deleted 39 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

How make array search more efficient in python? Array-search algorithm for identical sequences and character distances

Here is Python code written to perform the following operations:

  1. Find each occurrence of a three-letter sequence in a character array (e.g. a sequence such as ('a','b','c')), including overlapping sequences of up to 2 shared characters.
  2. Count the characters between the start of each sequence and the start of all identical sequences following it.
  3. Every time the same number of characters results from #2, increment a counter for that specific number of characters (regardless of which character sequence caused it).
  4. Return a dictionary containing all accumulated counters for character distances.

The code is shown below.

 
counts = {}
# repeating groups of three identical chars in a row
for i in range(len(array)-2):
    for j in range(i+1,len(array)-2):
        if ((array[i] == array[j]) & (array[i+1] == array[j+1]) & (array[i+2] == array[j+2])):
            if counts.has_key(j-i) == False:
                counts[j-i] = 1
            else:
                counts[j-i] += 1

This code was originally written in another programming language, but I would like to apply any optimizations or improvements available in Python. Thank you for your time.

How make array search more efficient in python?

Here is Python code written to perform the following operations:

  1. Find each occurrence of a three-letter sequence in a character array (e.g. a sequence such as ('a','b','c')), including overlapping sequences of up to 2 shared characters.
  2. Count the characters between the start of each sequence and the start of all identical sequences following it.
  3. Every time the same number of characters results from #2, increment a counter for that specific number of characters (regardless of which character sequence caused it).
  4. Return a dictionary containing all accumulated counters for character distances.

The code is shown below.

counts = {}
# repeating groups of three identical chars in a row
for i in range(len(array)-2):
    for j in range(i+1,len(array)-2):
        if ((array[i] == array[j]) & (array[i+1] == array[j+1]) & (array[i+2] == array[j+2])):
            if counts.has_key(j-i) == False:
                counts[j-i] = 1
            else:
                counts[j-i] += 1

This code was originally written in another programming language, but I would like to apply any optimizations or improvements available in Python. Thank you for your time.

Array-search algorithm for identical sequences and character distances

Here is Python code written to perform the following operations:

  1. Find each occurrence of a three-letter sequence in a character array (e.g. a sequence such as ('a','b','c')), including overlapping sequences of up to 2 shared characters.
  2. Count the characters between the start of each sequence and the start of all identical sequences following it.
  3. Every time the same number of characters results from #2, increment a counter for that specific number of characters (regardless of which character sequence caused it).
  4. Return a dictionary containing all accumulated counters for character distances.
 
counts = {}
# repeating groups of three identical chars in a row
for i in range(len(array)-2):
    for j in range(i+1,len(array)-2):
        if ((array[i] == array[j]) & (array[i+1] == array[j+1]) & (array[i+2] == array[j+2])):
            if counts.has_key(j-i) == False:
                counts[j-i] = 1
            else:
                counts[j-i] += 1

This code was originally written in another programming language, but I would like to apply any optimizations or improvements available in Python.

The non-code text of the answer misstated the problem. I fixed the lack of clarity.
Source Link

i have theHere is Python code in python, which parses an array (containing characters), searching for repeating groups of characters (in this case repeating groups of the same 3 chars) and countingwritten to perform the occurencesfollowing operations:

  1. Find each occurrence of a three-letter sequence in a character array (e.g. a sequence such as ('a','b','c')), including overlapping sequences of up to 2 shared characters.
  2. Count the characters between the start of each sequence and the start of all identical sequences following it.
  3. Every time the same number of characters results from #2, increment a counter for that specific number of characters (regardless of which character sequence caused it).
  4. Return a dictionary containing all accumulated counters for character distances.

The code is shown below.

counts = {}
# repeating groups of three identical chars in a row
for i in range(len(array)-2):
    for j in range(i+1,len(array)-2):
        if ((array[i] == array[j]) & (array[i+1] == array[j+1]) & (array[i+2] == array[j+2])):
            if counts.has_key(j-i) == False:
                counts[j-i] = 1
            else:
                counts[j-i] += 1

But i think, that theThis code should be improved in python some way (originally was originally written in another programming language). if is there some way, please help mebut I would like to improve thisapply any optimizations or improvements available in Python. thanksThank you for your time.

i have the code in python, which parses an array (containing characters), searching for repeating groups of characters (in this case repeating groups of the same 3 chars) and counting the occurences:

counts = {}
# repeating groups of three identical chars in a row
for i in range(len(array)-2):
    for j in range(i+1,len(array)-2):
        if ((array[i] == array[j]) & (array[i+1] == array[j+1]) & (array[i+2] == array[j+2])):
            if counts.has_key(j-i) == False:
                counts[j-i] = 1
            else:
                counts[j-i] += 1

But i think, that the code should be improved in python some way (originally was written in another language). if is there some way, please help me to improve this. thanks

Here is Python code written to perform the following operations:

  1. Find each occurrence of a three-letter sequence in a character array (e.g. a sequence such as ('a','b','c')), including overlapping sequences of up to 2 shared characters.
  2. Count the characters between the start of each sequence and the start of all identical sequences following it.
  3. Every time the same number of characters results from #2, increment a counter for that specific number of characters (regardless of which character sequence caused it).
  4. Return a dictionary containing all accumulated counters for character distances.

The code is shown below.

counts = {}
# repeating groups of three identical chars in a row
for i in range(len(array)-2):
    for j in range(i+1,len(array)-2):
        if ((array[i] == array[j]) & (array[i+1] == array[j+1]) & (array[i+2] == array[j+2])):
            if counts.has_key(j-i) == False:
                counts[j-i] = 1
            else:
                counts[j-i] += 1

This code was originally written in another programming language, but I would like to apply any optimizations or improvements available in Python. Thank you for your time.

Source Link
Peter
  • 33
  • 2

How make array search more efficient in python?

i have the code in python, which parses an array (containing characters), searching for repeating groups of characters (in this case repeating groups of the same 3 chars) and counting the occurences:

counts = {}
# repeating groups of three identical chars in a row
for i in range(len(array)-2):
    for j in range(i+1,len(array)-2):
        if ((array[i] == array[j]) & (array[i+1] == array[j+1]) & (array[i+2] == array[j+2])):
            if counts.has_key(j-i) == False:
                counts[j-i] = 1
            else:
                counts[j-i] += 1

But i think, that the code should be improved in python some way (originally was written in another language). if is there some way, please help me to improve this. thanks