Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 打断段落的句子并动态地重新排列和重新构建框架_Python - Fatal编程技术网

Python 打断段落的句子并动态地重新排列和重新构建框架

Python 打断段落的句子并动态地重新排列和重新构建框架,python,Python,考虑一段 p = "Both apples and oranges are fruits but apples are usually sweet and oranges are usually citrus.Apple have a pH arround 3 and orange between 3-4. Oranges are an excellent source of Vitamin C. Apples have a higher foliage (55mcg) as compared

考虑一段

p = "Both apples and oranges are fruits but apples are usually sweet and oranges are usually citrus.Apple have a pH arround 3 and orange between 3-4. Oranges are an excellent source of Vitamin C. Apples have a higher foliage (55mcg) as compared to oranges (23mcg).
我想把段落重新组织成这样

re_order = "Apple have a pH arround 3 and orange between 3-4.Both apples and oranges are fruits but apples are usually sweet and oranges are usually citrus.Apples have a higher foliage (55mcg) as compared to oranges (23mcg).Oranges are an excellent source of Vitamin C."

如何使用python将p转换为重新排序段落?

使用python3中的随机库:

import random

def reorder(phrase):
    phrase = phrase.split('.')
    random.shuffle(phrase)

    return str.join('.', phrase) + '.'
您正在使用random.shuffle方法。这是随机混合的列表。 这里有一个例子:

# Import module
import random as rd

# Your string sentence
p = "Both apples and oranges are fruits but apples are usually sweet and oranges are usually citrus.Apple have a pH arround 3 and orange between 3-4. Oranges are an excellent source of Vitamin C. Apples have a higher foliage(55mcg) as compared to oranges(23mcg)."

# Slice p in list of sentence (every '.')
p_list = p.split(".")
print(p_list)
# ['Both apples and oranges are fruits but apples are usually sweet and oranges are
# usually citrus', 'Apple have a pH arround 3 and orange between 3-4', ' Oranges are an 
# excellent source of Vitamin C', ' Apples have a higher foliage(55mcg) as compared
# to oranges(23mcg)', '']

# Change the order
rd.shuffle(p_list)
print(p_list)
# [' Oranges are an excellent source of Vitamin C', ' Apples have a higher foliage(55mcg)
# as compared to oranges(23mcg)', '', 'Both apples and oranges are fruits but apples are 
# usually sweet and oranges are usually citrus', 'Apple have a pH arround 3 and orange 
# between 3-4']

# Rebuild the list as one string
p = '. '.join(p_list)
print(p)
# Oranges are an excellent source of Vitamin C.  Apples have a higher foliage(55mcg) as 
# compared to oranges(23mcg). . Both apples and oranges are fruits but apples are usually 
# sweet and oranges are usually citrus. Apple have a pH arround 3 and orange
# between 3-4


您想如何重新订购?通过字母数字排序?为什么‘苹果的叶子比橘子的高55微克?’是第三个吗?我只是想随机地重新排列句子。没有条件……有可能吗?没有。每次运行程序时,顺序都会改变。有时,由于顺序是随机混合的,并且您只处理3个元素,因此可能没有顺序。