简体   繁体   English

如何使用python中的for循环计算列表中四个相邻元素的数量?

[英]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). 我有一个字母列表,我需要计算字母J出现为JJJJ(4 Js)的次数。 It has to be done using for loop (and not .count). 必须使用for循环(而不是.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. 而且我知道代码是不正确的,因为如果有JJJJJ(5 Js),那么我的代码会将其计为2xJJJJ(4 Js),结果将是4而不是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 . 在您的代码中,您多次检查t<len(data)但这并不能防止t+3大于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: 您无需勾选t ,可以直接选择正确的范围:

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: 整个结构可以用sum器和切片代替sum

>>> 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. 如果不想计数重叠的子字符串,则可以在每次看到'J'字符时增加一个计数器。 The counter gets resetted for any other character or when the counter reaches '4'. 计数器会因其他任何字符或达到“ 4”而复位。 In this case, the jjjj_counter gets incremented: 在这种情况下, jjjj_counter会增加:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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