Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.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_Arrays_Range - Fatal编程技术网

Python 重新连接一个单词数组,使得有特定数量的行

Python 重新连接一个单词数组,使得有特定数量的行,python,arrays,range,Python,Arrays,Range,我的目标是做一些类似的事情,通常可能会做一些类似的东西,但不是包装文本与指定的最大长度,我希望相反,指定多少行 我已经将字符串拆分为,现在我想用一个空格重新连接字符串数组,这样我就有了指定数量的行 这是我到目前为止所拥有的num是我需要的行数,words是一个单词数组wpc是一个(不正确的)假设,即“每个块有多少个单词”: 这会产生不正确的结果: [ "The quick brown fox", "jumps over the lazy", "dog" ] 它有3行,而不是所需的4

我的目标是做一些类似的事情,通常可能会做一些类似的东西,但不是包装文本与指定的最大长度,我希望相反,指定多少行

我已经将字符串拆分为,现在我想用一个空格重新连接字符串数组,这样我就有了指定数量的行

这是我到目前为止所拥有的
num
是我需要的行数,
words
是一个单词数组
wpc
是一个(不正确的)假设,即“每个块有多少个单词”:

这会产生不正确的结果:

[
  "The quick brown fox",
  "jumps over the lazy",
  "dog"
]
它有3行,而不是所需的4行

单词列表和所需行数都是动态的,我哪里出错了

正确的输出有点可疑。。一种可能性是

[
  "The quick brown",
  "fox jumps",
  "over the",
  "lazy dog"
]
当然,“三个字”行可能在任何地方。只要总是有
num
行,那么奇数行的位置(第一行、最后一行、随机)就不重要了

此外,如果您添加了更多的单词,则最好将这些单词平均分布(同样,我也不太在意它们是如何分布的):


你翻转了
wpc
num
的用法,我认为
ceil
的用法在这里是错误的

这里有一个解决方案:

import math

num = 4
words = [
  "The",
  "quick",
  "brown",
  "fox",
  "jumps",
  "over",
  "the",
  "lazy",
  "dog"
]
wpc = math.floor(len(words)/num)
chunks = [' '.join(words[wpc*i:wpc*(i+1)]) for i in range(num)]

chunks[-1] = " ".join([chunks[-1]] + words[wpc * num:])
结果是:

['The quick', 'brown fox', 'jumps over', 'the lazy dog']
均匀分布的解决方案: Thqt稍微复杂一点。基本上,我知道每个词块的最小单词数是多少(本例中为2个),并在一些词块之间随机分布额外的单词

num = 4
words = [
  "The",
  "quick",
  "brown",
  "fox",
  "jumps",
  "over",
  "the",
  "lazy",
  "dog", 
    "bla"
]

base_words_per_chunk = math.floor(len(words)/num)
extra_words = len(words) - num * base_words_per_chunk
words_per_chunk = [base_words_per_chunk] * num

larger_chunks = random.sample(range(num), extra_words)
for inx in larger_chunks:
    words_per_chunk[inx] = words_per_chunk[inx] + 1

start_point = 0
chunks = []
for inx in itertools.accumulate(words_per_chunk):
    chunk = words[start_point:inx]
    chunks.append(chunk)
    start_point = inx

你翻转了
wpc
num
的用法,我认为
ceil
的用法在这里是错误的

这里有一个解决方案:

import math

num = 4
words = [
  "The",
  "quick",
  "brown",
  "fox",
  "jumps",
  "over",
  "the",
  "lazy",
  "dog"
]
wpc = math.floor(len(words)/num)
chunks = [' '.join(words[wpc*i:wpc*(i+1)]) for i in range(num)]

chunks[-1] = " ".join([chunks[-1]] + words[wpc * num:])
结果是:

['The quick', 'brown fox', 'jumps over', 'the lazy dog']
均匀分布的解决方案: Thqt稍微复杂一点。基本上,我知道每个词块的最小单词数是多少(本例中为2个),并在一些词块之间随机分布额外的单词

num = 4
words = [
  "The",
  "quick",
  "brown",
  "fox",
  "jumps",
  "over",
  "the",
  "lazy",
  "dog", 
    "bla"
]

base_words_per_chunk = math.floor(len(words)/num)
extra_words = len(words) - num * base_words_per_chunk
words_per_chunk = [base_words_per_chunk] * num

larger_chunks = random.sample(range(num), extra_words)
for inx in larger_chunks:
    words_per_chunk[inx] = words_per_chunk[inx] + 1

start_point = 0
chunks = []
for inx in itertools.accumulate(words_per_chunk):
    chunk = words[start_point:inx]
    chunks.append(chunk)
    start_point = inx

如果有第10个单词,输出的长度应该是3/3/2/2还是4/2/2/2(忽略顺序)?@西尔克沃姆最好是3/3/2/2如果有第10个单词,输出的长度应该是3/3/2/2还是4/2/2(忽略顺序)?@西尔克沃姆最好是3/3/2/2这肯定比我的尝试要近得多-但现在它只是在最后一行标记了额外的单词,而不是重新分配。如果你在数组中再加几个词,你就会明白我的意思了。再次感谢你的回答。你想让它们均匀分布吗?是的,那太棒了!非常感谢你!这当然比我的尝试要近得多,但现在它只是在最后一行标记额外的单词,而不是重新分发。如果你在数组中再加几个词,你就会明白我的意思了。再次感谢你的回答。你想让它们均匀分布吗?是的,那太棒了!非常感谢你!