从打印输出中除去括号和撇号

时间:2020-05-16 13:38:16

标签: printing

我想从打印输出中删除['和']。我的代码看起来像这样。

text = input("Text: ")
words = text.split()


bigrams = {}
for i in range(len(words) - 1):
  current_word = words[i]
  next_word = words[i+1]
  bigrams[current_word] = []
  bigrams[current_word].append(next_word)


for k, v in bigrams.items():
    print(k,v)

当前输出...

Text: This is a sentence for the bigrams
This ['is']
is ['a']
a ['sentence']
sentence ['for']
for ['the']
the ['bigrams']

1 个答案:

答案 0 :(得分:0)

您当前正在使用python中的默认行为来打印列表。如果您想要更漂亮的东西,可以进行for循环:

paintComponent

尽管我相信在您当前的实现中,您的列表将始终只包含1个元素,因为您在for k, v in bigrams.items(): out = f"{k}:" for elem in v: out += f" {elem}," out = out[:-1] # remove last ',' print(out) 中反复对其进行了初始化,然后追加了一个元素。如果打算这样做,则可以使用以下命令进行打印:

bigrams[current_word] = []