0

私の初心者では、実験してたくさんの小さなスクリプトを書き、学ぼうとしています。私はこの困惑にぶつかりました:

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予想どおり、実際にはそのリストの各要素を新しい行に出力します。

ではなぜ違いが?

4

2 に答える 2

2

article.append([some list]) リストを 1 つの項目として追加します

article.extend([some list]) は、いくつかのリストの各項目を記事リストに追加します

于 2013-09-19T17:50:09.427 に答える