簡體   English   中英

令牌的所有唯一排列

[英]all unique permutations of the tokens

Output 第 1 部分中標記的所有唯一排列。以及排列總數。

例子:

  • 這是一個測試
  • 這是測試a
  • 這是測試
  • 這個測試是一個
  • 這個測試a是
  • 測試是一個 This

你知道python怎么做嗎?

import itertools
import itertools
from nltk.tokenize import word_tokenize
import nltk
nltk.download('punkt')
def tokenize_word():
  sentence = str(input("please right a comment: "))
  punctuation = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
 
# Removing punctuations in string
# Using loop + punctuation string
  for element in sentence:
    if element in punctuation:
        sentence = sentence.replace(element, "")
  
  tokenize_sentence= word_tokenize(sentence)
  alphabetics_order_sentence = sorted(tokenize_sentence)
  for i in range(1, len(alphabetics_order_sentence) + 1):
    permutations= itertools.permutations(alphabetics_order_sentence)
    print(permutations)

tokenize_word()

您可以使用 package itertools中的 function permutations (如果允許 package)。

from itertools import permutations

st = "This is a test"
result = list(permutations(st.split()))

for i in result:
    print(" ".join(i))
    
print()
print("The number of permutations is {}".format(len(result)))

Output 來自上面的代碼:

This is a test
This is test a
This a is test
This a test is
This test is a
This test a is
is This a test
is This test a
is a This test
is a test This
is test This a
is test a This
a This is test
a This test is
a is This test
a is test This
a test This is
a test is This
test This is a
test This a is
test is This a
test is a This
test a This is
test a is This

The number of permutations is 24

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM