어제 내 응용 프로그램의 하위 섹션이 작동했는데 왜 작동하지 않는지 알 수 없습니다. 여기 문자열을 조건부로 다시 포맷하는 중 오류가 발생했습니다.
는해야 할 작업은 다음과 같습니다입력 : 10th ave 501
출력 : 501 10th ave
이 코드 블록이 patterns
라는 목록에서 대상 단어 중 하나를 검색하기로하고, 경우입니다 그 뒤에 숫자가 len <= 4
인 경우 문자열 앞쪽으로 옮깁니다. patterns
에는 ave, street, road, place
과 같은 단어가 포함되어 있습니다.
address = address.split(' ')
for pattern in patterns:
try:
if address[0].isdigit():
continue
location = address.index(pattern) + 1
number_location = address[location]
if 'th' in address[location + 1] or 'floor' in address[location + 1] or '#' in address[location]:
continue
except (ValueError, IndexError):
continue
if number_location.isdigit() and len(number_location) <= 4:
address = [number_location] + address[:location] + address[location+1:]
break
address = ' '.join(address)
print address
는 현재 출력이 그냥 입력 된 것과 동일한 것입니다 :
여기 내 코드입니다. 즉 10th ave 501
은 10th ave 501
을 반환합니다. 나는 그것이 흘끗 보일 정도로 비교적 명백한 무엇인가 느낀다.
원하는 경우, 당신은 단지 첫 번째 문자를 확인하실 수 있습니다 인 경우
반환 사실 :
코드를 쉽게 이해할 수있는 한 가지 팁 : 한 가지 유형의 데이터 만 넣기 단일 변수 이름. 예를 들어'address'를 문자열로, 또한리스트 (address = address.split ('')')로 사용할 수 있습니다. – dsh
501의 길이는 4보다 작거나 같지 않은가요? – Harrison
@ 하리슨 이것이 숙제가 아니고 거리를 정상화하는 문제를 해결하고 싶다면 어쩌면이 [패키지] (https://github.com/openvenues/pypostal)가 도움이 될 수도 있습니다 – BPL