简体   繁体   English

Python:将文件中的单词合并到一行

[英]Python: Merge words from files to one line

I have a list with words out of different textfiles. 我有一个列表,列出了不同文本文件中的单词。 When I sort this list out and clean them of duplicates I still see multiple words which are the soms but out of different text files, eg ['you', 'chainletter.txt'] and ['you', 'grail.txt'] My question is , how could I combine these to give the output ['you', 'chainletter.txt', 'grail.txt'] 当我整理出该列表并清除重复项时,我仍然会看到多个单词,它们是soms但来自不同的文本文件,例如['you','chainletter.txt']和['you','grail.txt' ]我的问题是,如何将这些结合起来以提供输出['you','chainletter.txt','grail.txt']

I'm currently using the following code: 'pearl' module: 我目前正在使用以下代码:“珍珠”模块:

def make_table(pairs):
    import sort
    import dup
    sort = sort.merge_pairs(pairs)
    nodup = dup.remove_dups(sort)
    print(nodup)
    return nodup

And i want my 'search engine' to display words like described above 我希望我的“搜索引擎”显示上述单词

I'm not quite sure what you're trying to achieve overall, but the two problems you mention can be solved as follows: 我不确定您要总体上实现什么目标,但是您提到的两个问题可以通过以下方式解决:

  1. You can use set() to remove duplicates from a list (and list() to turn it back into a list if you like). 您可以使用set()从列表中删除重复项如果需要,可以使用list()将其转回到列表中)。

  2. To add the elements of a list into another list, use .extend() 要将列表中的元素添加到另一个列表中,请使用.extend()

This question about merging lists may also be of interest. 关于合并列表的这个问题可能也很有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM