简体   繁体   English

令牌的所有唯一排列

[英]all unique permutations of the tokens

Output all unique permutations of the tokens from part 1. And the total number of permutations. Output 第 1 部分中标记的所有唯一排列。以及排列总数。

Example:例子:

  • This is a test这是一个测试
  • This is test a这是测试a
  • This a is test这是测试
  • This test is a这个测试是一个
  • This test a is这个测试a是
  • Test is a This测试是一个 This

do you know how to do it in python?你知道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()

You can use the function permutations from the package itertools (if package is allowed).您可以使用 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 from code above: 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