Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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
Python 3.x 使用文件名作为函数的参数并从另一个列表中删除一个列表_Python 3.x - Fatal编程技术网

Python 3.x 使用文件名作为函数的参数并从另一个列表中删除一个列表

Python 3.x 使用文件名作为函数的参数并从另一个列表中删除一个列表,python-3.x,Python 3.x,我已经写了一段代码,但可能函数部分不正确。我应该对文件名创建一个函数,然后返回到它 其次,必须是另一个函数,用于从text1\u单词中删除text2\u单词的元素 我有代码,但没有从text1\u words中删除text2\u words列表的功能,我的文件名函数不正确,因为我无法返回该函数 预期结果: Insert name of the file:text.txt Number of words in the text: 96 Number of unique words: 42 H

我已经写了一段代码,但可能函数部分不正确。我应该对文件名创建一个函数,然后返回到它

其次,必须是另一个函数,用于从
text1\u单词
中删除
text2\u单词
的元素

我有代码,但没有从
text1\u words
中删除
text2\u words
列表的功能,我的文件名函数不正确,因为我无法返回该函数

预期结果:

Insert name of the file:text.txt

Number of words in the text: 96

Number of unique words: 42

Hitta

din

nyhet

Stoff

nyheter

runt
.
.
.
import re

def common_words(filename, text2="vanligaord.txt"):

    text1=0
    infile_1= open("text.txt", "r")
    filename=text1
    text1=input('Insert name of the file:')

    text1=infile_1.read()



    for punctuation in '!,?,.,-':
        text1=text1.replace(punctuation, " ")
        text1_words=text1.split()


    print('Number of words in the text:', len(text1_words))

    infile_2= open("vanligaord.txt", "r",encoding='latin_1')
    text2= infile_2.read()
    text2_words=text2.split('\n')

    unique_words=[]
    for word in text1_words:
        if word not in text2_words:
            unique_words.append(word)


    print('Number of unique words:', len(unique_words))

 print ('\n'.join(unique_words))
代码:

Insert name of the file:text.txt

Number of words in the text: 96

Number of unique words: 42

Hitta

din

nyhet

Stoff

nyheter

runt
.
.
.
import re

def common_words(filename, text2="vanligaord.txt"):

    text1=0
    infile_1= open("text.txt", "r")
    filename=text1
    text1=input('Insert name of the file:')

    text1=infile_1.read()



    for punctuation in '!,?,.,-':
        text1=text1.replace(punctuation, " ")
        text1_words=text1.split()


    print('Number of words in the text:', len(text1_words))

    infile_2= open("vanligaord.txt", "r",encoding='latin_1')
    text2= infile_2.read()
    text2_words=text2.split('\n')

    unique_words=[]
    for word in text1_words:
        if word not in text2_words:
            unique_words.append(word)


    print('Number of unique words:', len(unique_words))

 print ('\n'.join(unique_words))
预期输出屏幕截图:

这有点不清楚。您能显示准确的输入文件和预期的输出吗?谢谢,是的,当然。插入文件名:text.txt\n文本中的字数:96\n唯一字数:42\n Hitta din nyhet Stoff nyheter runt omkring Kanske nyligen hänt Komunen skolan kopplat hållbarhet berätta Du söka aktuella Forskiningsrapporter nyheter Nationallt koppla bor spinna vidare Eller varför skapa egen nyhetenkätundersökning viktigWell,我有两个文本文件:第一个名为'text.txt',第二个是单词列表名为'vanligord.txt',不,对不起。请将此作为问题本身的文本。链接到图像是一件非常痛苦的事情——我必须重新输入所有内容,这浪费了空间,对那些有屏幕阅读器的人来说也不友好。