将列表保存为单个或单独的文件

时间:2017-04-06 08:55:59

标签: python string list

我的任务是:

  

开发一个程序,识别句子中的单个单词,将它们存储在一个列表中,并将原始句子中的每个单词替换为该单词在列表中的位置。

     

例如,句子   问问你的国家可以做什么,因为你问你可以为你的国家做些什么   包含单词ASK,NOT,WHAT,YOUR,COUNTRY,CAN,DO,FOR,YOU

     

可以使用序列1,2,3,4,5,6,7,8,9,1,3,9,6,7,8从这个单词中这些单词的位置重新创建句子, 4,5

我已经完成了任务的第一部分,到目前为止我已经有了这段代码:

my_list = ['ASK', 'NOT', 'WHAT', 'YOUR', 'COUNTRY', 'CAN', 'DO', 'FOR', 
'YOU', 'ASK', 'WHAT', 'YOU', 'CAN', 'DO', 'FOR', 'YOUR', 'COUNTRY']
with open("task2.txt", 'w') as f:
f.write("\n".join(map(str, my_list)))

这被认为有效吗?我怎样才能提高效率?

然而,我的问题是剩下的任务:

  

将单词列表和这些单词在句子中的位置保存为单独的文件或单个文件。

我不知道这意味着什么。我做了一些研究,发现它涉及阅读和写入文件,但我不知道下一步该做什么。

然而我想出了这个:

{{1}}

但我不知道它的作用或它与任务的关系???我需要能够解释我能做的代码,因为我不了解它。

有人可以修改我的代码吗?谢谢

3 个答案:

答案 0 :(得分:2)

由于您已标记此R,因此这是R中的解决方案。

sentence <- "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"

find.words <- strsplit(sentence, " ")[[1]] # split string into 
words <- unique(find.words) # find unique words
# find positions of words in the unique vector
words.pos <- sapply(find.words, FUN = function(x, words) which(x == words), words = words)

sprintf("The sentence can be recreated from the positions of these words in the list %s using the sequence %s", 
        paste(words, collapse = " "), paste(words.pos, collapse = " "))

[1] "The sentence can be recreated from the positions of these words in the list ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU using the sequence 1 2 3 4 5 6 7 8 9 1 3 9 6 7 8 4 5"

以下是如何将其写入文件的一种方法。

# find position by word
pos.by.word <- sapply(words, FUN = function(x, fw) which(x == fw), fw = find.words)
# paste names together
concat.words.freq <- mapply(FUN = function(x, y) {
  paste(x, paste(y, collapse = " "))
}, names(pos.by.word), pos.by.word)

write.table(as.data.frame(concat.words.freq), file = "out.txt",
            row.names = FALSE, col.names = FALSE, quote = FALSE)

out.txt看起来像这样:

ASK 1 10
NOT 2
WHAT 3 11
YOUR 4 16
COUNTRY 5 17
CAN 6 13
DO 7 14
FOR 8 15
YOU 9 12

答案 1 :(得分:0)

我给你一个可能的解决方案:

from collections import defaultdict

words = defaultdict(list)
sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
s = sentence.split()
for pos, word in enumerate(s):
    words[word].append(pos + 1)

输出:

In [20]: words
Out[20]: 
defaultdict(list,
        {'ASK': [1, 10],
         'CAN': [6, 13],
         'COUNTRY': [5, 17],
         'DO': [7, 14],
         'FOR': [8, 15],
         'NOT': [2],
         'WHAT': [3, 11],
         'YOU': [9, 12],
         'YOUR': [4, 16]})

答案 2 :(得分:-1)

试试这个

s = sentence.split()
positions = [s.index(x)+1 for x in s]

map = {}
for w in s:
  map[w] = True

words = []
for w in map:
  words.append(w)

print(words)