如何以列表形式返回函数的输出

时间:2018-10-15 12:15:15

标签: python list

.jupyter

以下是我从下面的代码获得的输出。我需要以列表['Sharayu','neeraj'.....]

的形式输出
static
└── custom
    └── custom.css

4 个答案:

答案 0 :(得分:0)

这样做:

path = '/home/python/resumes/*.txt'
files = glob.glob(path)
data_list = []
for n in files:
    with io.open(n,'r') as f:
        data=f.read()
        data_list.append(fetch_name(data))
    print(data_list)

答案 1 :(得分:0)

使用list()可以做到如下:

namelist=list()    
for n in files:
    with io.open(n,'r') as f:
        data=f.read()
        namelist.append(fetch_name(data))
print(namelist)

答案 2 :(得分:0)

如果您希望函数返回一个列表,则可以初始化一个列表并将名称附加到列表中:

def fetch_name(resume_text):
    names = []
    tokenized_sentences = nltk.sent_tokenize(resume_text)
    for sentence in tokenized_sentences:
        for chunk in nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(sentence), tagset='universal')):
            if hasattr(chunk, 'label'):  # and chunk.label() == 'PERSON':
                chunk = chunk[0]
            (name, tag) = chunk
            if tag == 'NOUN':
                # print(name)
                # z.extend(name)
                names.append(name)
    return names

答案 3 :(得分:0)

您可以使用append method将条目添加到列表中。

resumes = []
for path in glob.glob('/home/python/resumes/*.txt'):
    with open(path, 'r', encoding='utf-8') as f:
        resumes.append(fetch_name(f.read()))
print(resumes)