Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 如何匹配列表中列表中的相同字符并分别导出结果_List_For Loop_While Loop - Fatal编程技术网

List 如何匹配列表中列表中的相同字符并分别导出结果

List 如何匹配列表中列表中的相同字符并分别导出结果,list,for-loop,while-loop,List,For Loop,While Loop,伙计们!我得到一个列表(final_word_list),我想匹配名为“text_under_directory”的列表下所有子列表中的相同字符,然后分别导出结果,如图所示 我的预期产出是: ['four', 'six', 'two'] ['eight', 'four', 'six', 'ten', 'two'] ['eight', 'four', 'six', 'ten', 'twelve', 'two'] ['four', 'six', 'two'] ['eight', 'six', 't

伙计们!我得到一个列表(final_word_list),我想匹配名为“text_under_directory”的列表下所有子列表中的相同字符,然后分别导出结果,如图所示

我的预期产出是:

['four', 'six', 'two']
['eight', 'four', 'six', 'ten', 'two']
['eight', 'four', 'six', 'ten', 'twelve', 'two']
['four', 'six', 'two']
['eight', 'six', 'ten']
['eight' 'ten', 'twelve'] 

好的,我自己找到答案。我只是把它放在下面

from nltk.tokenize import word_tokenize

final_word_list = ['zero', 'two', 'four', 'six', 'eight', 'ten', 'twelve', 'fourteen', 'sixteen']

texts_under_directory = [['one', 'two', 'three', 'four', 'five', 'six'], ['five', 'six', 'seven', 'eight', 'nine', 'ten'], ['eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen']]

# texts_under_directory[0] = ['one', 'two', 'three', 'four', 'five', 'six']
# texts_under_directory[1] = ['five', 'six', 'seven', 'eight', 'nine', 'ten']
# texts_under_directory[2] = ['eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen']

n = 3
final_result = [[] for _ in range(n)]

i = 0
while i < len(texts_under_directory):
    for b in texts_under_directory[i]:
        for a in final_word_list:
            if a == b:
                for x in word_tokenize(b):
                    final_result[i].append(x)

    print(sorted(set(final_result[i])))

    i += 1
from nltk.tokenize import word_tokenize

final_word_list = ['zero', 'two', 'four', 'six', 'eight', 'ten', 'twelve', 'fourteen', 'sixteen']

texts_under_directory = [['one', 'two', 'three', 'four', 'five', 'six'], ['five', 'six', 'seven', 'eight', 'nine', 'ten'], ['eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen']]

# texts_under_directory[0] = ['one', 'two', 'three', 'four', 'five', 'six']
# texts_under_directory[1] = ['five', 'six', 'seven', 'eight', 'nine', 'ten']
# texts_under_directory[2] = ['eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen']

n = 3
final_result = [[] for _ in range(n)]

i = 0
while i < len(texts_under_directory):
    for b in texts_under_directory[i]:
        for a in final_word_list:
            if a == b:
                for x in word_tokenize(b):
                    final_result[i].append(x)

    print(sorted(set(final_result[i])))

    i += 1
['four', 'six', 'two']
['eight', 'six', 'ten']
['eight', 'ten', 'twelve']