私の初心者では、実験してたくさんの小さなスクリプトを書き、学ぼうとしています。私はこの困惑にぶつかりました:
import re
nouns = ['bacon', 'cheese', 'eggs', 'milk']
article = []
def list_in_list(list1, list2):
for list_1_element in list1:
print list_1_element
for list_2_element in list2:
print list_2_element
with open('test_sentence.txt', 'r') as input_f:
for line in input_f:
article.append(re.findall(r"[\w']+|[.,!?;:]", line))
list_in_list(article, nouns)
test_sentence.txt の内容は次のとおりです。
I need to go shopping today and some of the things I need to buy are bacon, cheese and eggs. I also need to buy something with a comma in it, such as milk, cheese, and bacon.
私が理解していないのは、print list_1_element
実際に「list1」リスト全体を出力する理由['I', 'need', 'to', 'go'.............]
です. そして、print list_2_element
予想どおり、実際にはそのリストの各要素を新しい行に出力します。
ではなぜ違いが?