이 아무튼에게 귀하의 질문에 직접적으로 대답하지는 않겠지 만, 여기서 우리가 마주 쳤던 몇 가지 문제를 이해하는 데 도움이 될 수 있습니다. 대부분 목록에 추가하는 방법을 볼 수 있으며 문자열, 목록 및 정수 (실제로는 할 수없는 길이)를 가져 오는 것의 차이점을 알 수 있습니다. 당신은에 코드를 얻을 수있을 것입니다, 당신이 우리의 대답을 통해 볼 때,
def step_forward():
raw_input('(Press ENTER to continue...)')
print('\n.\n.\n.')
def experiment():
""" Run a whole lot experiments to explore the idea of lists and
variables"""
# create an empty list, test length
word_list = []
print('the length of word_list is: {}'.format(len(word_list)))
# expect output to be zero
step_forward()
# add some words to the list
print('\nAdding some words...')
word_list.append('Hello')
word_list.append('Experimentation')
word_list.append('Interesting')
word_list.append('ending')
# test length of word_list again
print('\ttesting length again...')
print('\tthe length of word_list is: {}'.format(len(word_list)))
step_forward()
# print the length of each word in the list
print('\nget the length of each word...')
for each_word in word_list:
print('\t{word} has a length of: {length}'.format(word=each_word, length=len(each_word)))
# output:
# Hello has a length of: 5
# Experimentation has a length of: 15
# Interesting has a length of: 11
# ending has a length of: 6
step_forward()
# set up a couple of counters
short_word = 0
long_word = 0
# test the length of the counters:
print('\nTrying to get the length of our counter variables...')
try:
len(short_word)
len(long_word)
except TypeError:
print('\tERROR: You can not get the length of an int')
# you see, you can't get the length of an int
# but, you can get the length of a word, or string!
step_forward()
# we will make some new tests, and some assumptions:
# short_word: a word is short, if it has less than 9 letters
# long_word: a word is long, if it has 9 or more letters
# this test will count how many short and long words there are
print('\nHow many short and long words are there?...')
for each_word in word_list:
if len(each_word) < 9:
short_word += 1
else:
long_word += 1
print('\tThere are {short} short words and {long} long words.'.format(short=short_word, long=long_word))
step_forward()
# BUT... what if we want to know which are the SHORT words and which are the LONG words?
short_word = 0
long_word = 0
for each_word in word_list:
if len(each_word) < 9:
short_word += 1
print('\t{word} is a SHORT word'.format(word=each_word))
else:
long_word += 1
print('\t{word} is a LONG word'.format(word=each_word))
step_forward()
# and lastly, if you need to use the short of long words again, you can
# create new sublists
print('\nMaking two new lists...')
shorts = []
longs = []
short_word = 0
long_word = 0
for each_word in word_list:
if len(each_word) < 9:
short_word += 1
shorts.append(each_word)
else:
long_word += 1
longs.append(each_word)
print('short list: {}'.format(shorts))
print('long list: {}'.format(longs))
# now, the counters short_words and long_words should equal the length of the new lists
if short_word == len(shorts) and long_word == len(longs):
print('Hurray, its works!')
else:
print('Oh no!')
experiment()
을 바라 건데, 위의 미니 실험을 검사 :
아래의 코드를 실행 시도하고 무슨 일이 일어나고 있는지 검사 당신이 필요로하는 것을하십시오 :)
흠 ... 이전에 정의 된/발견 된 문장과 비교할 것입니다. 하지만 어떻게하면 다시 불러올 수 있습니까?이 루프에서 사용할 수 있고 길이가 같은 상태로 만들 수 있습니까? 내 문장의 길이는 숫자의 길이와 일치해야한다는 것을 알고 있지만 어떻게 쓰는지 확신 할 수 없습니다. – user2172079
괜찮 았나. 귀하의 코드가 올바르게 이해 된 경우 : count_pali() 함수는 회문이 발견 될 때마다 증분하여 총 회문 수를 계산합니다. 그리고 ... 또한이 문장 중 얼마나 많은 글자가 3 글자 이상인지를 세고 싶습니다. 그래서 회문 테스트 후 if 문을 직접 사용해 볼 수 있습니까? 의사 코드에서는 다음과 같이 보일 것입니다 : # 위의 코드는 ... 단어 인 경우 회문 : 마지막 코멘트에 더 추가 count_pali + 1 –
: 여기 '# 당신 앞의 코드를 ... 단어 인 경우 회문 : count_pali + 1 단어 이상 3 글자 이상 인 경우 long_word +1 ' 더 가깝습니까? –