파이썬 순차탐색으로 조건에맞는 위치를 찾아내려면???

cobra의 이미지

lines=['1', '홀', '2', '짝', '3', '짝', '4', '짝', '5', '홀', '6', '홀'
,'7', '짝', '8', '짝', '9', '짝', '10', '짝', '11', '홀', '12', '홀',
'13', '홀', '14', '짝', '15', '홀', '16', '홀', '17', '짝', '18', '홀',
'19','홀', '20', '홀']

start = int(input("처음 수 입력: ")) #예 3 입력
last = int(input("마지막 수 입력: ")) #예 7 입력

#입력한 크기만큼 리스트에 담기
lst=[]
for i in range(start,last+1):
lst.append(str(i)+'\n') # ['3','4','5','6','7']
lst.reverse()

# 두개의 리스트간 동일한 요소찾기
# lines리스트와 입력한 lst['3','4','5','6','7'] 리스트 비교
list_group=list(set(lst).intersection(lines))
list_group.sort()
print("동일한 요소",list_group)

#lines에 동일한요소 인덱스찾기
#동일한 요소의 index를 찾아 저장 index_num=[4,6,8,10,12]

index_num=[]
for i in list_group:
for d in lines:
if d == i:
index_num.append(lines.index(d))

index_num.sort()
print(index_num)

# 홀,짝 인덱스 찾기 # 홀,짝 의 인덱스찾아 저장 [5,7,9,11,13]
index_a_b=[]
for i in index_num:
index_a_b.append(i+1)

print(index_a_b)

# 홀,짝 개수 분리
a_count = 0
b_count = 0
for i in index_a_b:
if lines[i] == "짝":
a_count+=1
else:
b_count+=1

print("조회한 짝 갯수: ", a_count) # 짝 == 3
print("조회한 홀 갯수: ", b_count) # 홀 == 2

#구간길이
total_count=a_count+b_count
print("조회한 홀,짝 합계: ", total_count) #홀,짝 합계 == 5

f.close()
사용자가입력한 수의 범위에 홀짝의 개수를 세어서 리스트에 비교후 리스트안에 동일한 길이에 동일한 개수가 있다면 모든위치를알려주려고하는데 어떻게 코드를 짜야할까요??

예)사용자가 입력해서 찾은 짝==3, 홀 ==2개 을 lines[0]부터 부터 길이가 10(5*2)인 인덱스안에 짝==3, 홀 ==2개가 존재한다면 해당위치를 출력해주고 없다면 다시 lines[1]부터 순차적으로 찾아 동일한 갯수를 갖는 위치를 전부 찾는코드를 만들려고합니다

파이썬3의 이미지

cobra wrote:
리스트안에 동일한 길이에 동일한 개수가 있다면

이해하기 참 어려운 말이네요...
황병희의 이미지

(bionic)soyeomul@localhost:~/111$ python3 3.py
처음 수 입력: 3
마지막 수 입력: 7
1 5 (2, 3, 4)
2 6 (2, 3, 4)
3 7 (2, 3, 4)
4 8 (2, 3, 4)
5 9 (2, 3, 4)
8 12 (2, 3, 4)
(bionic)soyeomul@localhost:~/111$ python3 3.py
처음 수 입력: 1
마지막 수 입력: 6
1 6 (3, 3, 5)
8 13 (3, 3, 5)
9 14 (3, 3, 5)
(bionic)soyeomul@localhost:~/111$ python3 3.py
처음 수 입력: 4
마지막 수 입력: 9
2 7 (2, 4, 5)
3 8 (2, 4, 5)
4 9 (2, 4, 5)
5 10 (2, 4, 5)
6 11 (2, 4, 5)
7 12 (2, 4, 5)
(bionic)soyeomul@localhost:~/111$ 

# -*- coding: utf-8 -*-
 
lines = [
    "1", "홀",
    "2", "짝",
    "3", "짝",
    "4", "짝",
    "5", "홀",
    "6", "홀",
    "7", "짝",
    "8", "짝",
    "9", "짝",
    "10", "짝",
    "11", "홀",
    "12", "홀",
    "13", "홀",
    "14", "짝",
    "15", "홀",
    "16", "홀",
    "17", "짝",
    "18", "홀",
    "19", "홀",
    "20", "홀",
]
 
start = int(input("처음 수 입력: ")) # 예 3 입력
last = int(input("마지막 수 입력: ")) # 예 7 입력
 
def f(x, y):
    idx_start = lines.index(str(x))
    idx_last = lines.index(str(y))
 
    lines_input = lines[idx_start:idx_last+2]
 
    홀갯수 = lines_input.count("홀")
    짝갯수 = lines_input.count("짝")
    범위값 = y - x
 
    return 홀갯수, 짝갯수, 범위값
 
DELTA = f(start, last)[2]
 
for i, j in zip (range(1, 21-DELTA), range(1+DELTA, 21)):
    if f(i, j) == f(start, last):
        print(i, j, f(i, j))
 
# EOF

[우분투 18.04 파여폭스 나비에서 작성했어요]

--
^고맙습니다 감사합니다_^))//

cobra의 이미지

헐~~~ 제대로 이해하셨어요...
코드가 이렇게 간결 할수있네요 그저 놀랍습니다...
이제 문법배운 초보라 아직 전부를 이해할수는 없지만 좀 더 공부하겠습니다
다시한번 감탄하면서 배움에 정말 감사드립니다

cobra의 이미지

죄송하지만 메일로 lines리스트 확장에 대해서 질문좀 해도 될까요???

파이썬3의 이미지

cobra wrote:
lines리스트 확장에 대해서 질문

comp.lang.python 에 수많은 파이썬 해커들이 대기하고 있어요~

[크롬북에서 적었어요~]

cobra의 이미지

따끈한 정보 감사합니다^^