简体   繁体   中英

How to count the number of four adjecent elements in a list using for loops in python?

I have a list of letters and I need to count how many times the letter J appears as JJJJ (4 Js). It has to be done using for loop (and not .count). I wrote some code but I get an error that the index is out of range. And I know that the code is not correct because if there is JJJJJ (5 Js) then my code would count that as 2xJJJJ (4 Js) and the result would be 4 instead of 3.

I would really appreciate any help or advice. Thank you!

data = ["A","B","A","A","A","B","J","J","J","J","B","B","B","J","J","J","J","J","B","A","A","A","J","J","J","J",]
for t in range(len(data)):
    if t<len(data) and data[t] == "J":
        if t<len(data) and data[t+1] == "J":
            if t<len(data) and data[t+2] == "J":
                if data[t+3] == "J":
                        countTTT_1 += 1
print("JJJJ appears" ,countTTT_1)

This is what you can do:

data = 'ANDJRJEJJJJARGJJJJCSEGJJJJJESFF'

def count_j(data: str, maxj: int) -> int:
    number_of_j = 0
    number_of_repetitions = 0

    for ch in data:
            if ch == 'J':
                n, number_of_j = divmod(number_of_j, maxj)

                number_of_repetitions += n
                number_of_j += 1

    n, _ = divmod(number_of_j, maxj)

    number_of_repetitions += n

    return number_of_repetitions

print(count_j(data, 4))

Problem

In your code, you check many times if t<len(data) but it doesn't prevent t+3 from being larger than len(data) - 1 .

With a minimal amount of modification, you could write:

data = ["A","B","A","A","A","B","J","J","J","J","B","B","B","J","J","J","J","J","B","A","A","A","J","J","J","J",]
countTTT_1 = 0
for t in range(len(data)):
    if t<len(data) - 1 and data[t] == "J":
        if t<len(data) - 2 and data[t+1] == "J":
            if t<len(data) - 3 and data[t+2] == "J":
                if data[t+3] == "J":
                        countTTT_1 += 1
print("JJJJ appears" ,countTTT_1)
# ('JJJJ appears', 4)

You don't need to check t , you can choose the correct range directly:

data = ["A","B","A","A","A","B","J","J","J","J","B","B","B","J","J","J","J","J","B","A","A","A","J","J","J","J",]
countTTT_1 = 0
for t in range(len(data) - 3):
    if data[t] == "J":
        if data[t+1] == "J":
            if data[t+2] == "J":
                if data[t+3] == "J":
                        countTTT_1 += 1
print("JJJJ appears" ,countTTT_1)
# ('JJJJ appears', 4)

This whole structure could be replaced by sum with a generator comprehension and a slice:

>>> s = 'ABAAABJJJJBBBJJJJJBAAAJJJJ'
>>> sum(1 for i in range(len(s) - 3) if s[i:i + 4] == 'JJJJ')
4

Note that it counts overlapping substrings, though, just like your original example.

Solution

If you don't want to count overlapping substrings, you could increment a counter every time you see a 'J' character. The counter gets resetted for any other character or when the counter reaches '4'. In this case, the jjjj_counter gets incremented:

text = 'ABAAABJJJJBBBJJJJJBAAAJJJJ'

def count_jjjj(text):
  jjjj_counter = 0
  last_js = 0
  for char in text:
    if char == 'J':
      last_js += 1
      if last_js == 4:
        jjjj_counter += 1
        last_js = 0
    else:
      last_js = 0
  return jjjj_counter

print(count_jjjj(text))
# 3
print(count_jjjj('JJJ'))
# 0
print(count_jjjj('JJJJ'))
# 1
print(count_jjjj('JJJJJ'))
# 1
print(count_jjjj('JJ JJ'))
# 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM