【问题标题】:Inconsistent modulus result in python [duplicate]python中的模数不一致导致[重复]
【发布时间】:2022-08-08 09:18:14
【问题描述】:

我有一个包含字符串的简单列表“段落”。目标是将这些拆分的单词连接成每行 5 个单词的行。使用模数 5(针对索引值 + 1 进行简化)作为测试何时添加换行符。

由于我无法理解的原因,它工作正常,除非它突然决定跳过 5 的倍数。我无法弄清楚它为什么不一致。神秘地跳过索引 40、45 和 55。

for word in passage:
    indx = passage.index(word)   # dump into variable \"indx\" for convenience
    if (indx+1) %5  == 0:        # act every 5th element of list
        passage[indx] = f\"{word}\\n{indx+1}\" # add line break and index number to element

print(passage)
print()

final = \" \".join(passage)
print(final)

更改列表输出:

[\'When\', \'in\', \'the\', \'Course\', \'of\\n5\', \'human\', \'events,\', \'it\', \'becomes\', \'necessary\\n10\', \'for\', \'one\', \'people\', \'to\', \'dissolve\\n15\', \'the\', \'political\', \'bands\', \'which\', \'have\\n20\', \'connected\', \'them\', \'with\', \'another,\', \'and\\n25\', \'to\', \'assume\', \'among\', \'the\', \'powers\\n30\', \'of\', \'the\', \'earth,\', \'the\', \'separate\\n35\', \'and\', \'equal\', \'station\', \'to\', \'which\', \'the\', \'Laws\', \'of\', \'Nature\', \'and\', \'of\', \"Nature\'s\", \'God\', \'entitle\', \'them,\\n50\', \'a\', \'decent\', \'respect\', \'to\', \'the\', \'opinions\', \'of\', \'mankind\', \'requires\', \'that\\n60\', \'they\', \'should\', \'declare\', \'the\', \'causes\\n65\', \'which\', \'impel\', \'them\', \'to\', \'the\', \'separation.\']

和 \"joined\" 输出为字符串:

When in the Course of
5 human events, it becomes necessary
10 for one people to dissolve
15 the political bands which have
20 connected them with another, and
25 to assume among the powers
30 of the earth, the separate
35 and equal station to which the Laws of Nature and of Nature\'s God entitle them,
50 a decent respect to the opinions of mankind requires that
60 they should declare the causes
65 which impel them to the separation.

想法?

很抱歉没有包括原始列表,ewong:

[\'When\', \'in\', \'the\', \'Course\', \'of\', \'human\', \'events,\', \'it\', \'becomes\', \'necessary\', \'for\', \'one\', \'people\', \'to\', \'dissolve\', \'the\', \'political\', \'bands\', \'which\', \'have\', \'connected\', \'them\', \'with\', \'another,\', \'and\', \'to\', \'assume\', \'among\', \'the\', \'powers\', \'of\', \'the\', \'earth,\', \'the\', \'separate\', \'and\', \'equal\', \'station\', \'to\', \'which\', \'the\', \'Laws\', \'of\', \'Nature\', \'and\', \'of\', \"Nature\'s\", \'God\', \'entitle\', \'them,\', \'a\', \'decent\', \'respect\', \'to\', \'the\', \'opinions\', \'of\', \'mankind\', \'requires\', \'that\', \'they\', \'should\', \'declare\', \'the\', \'causes\', \'which\', \'impel\', \'them\', \'to\', \'the\', \'separation.\']

我会检查枚举。 (刚开始使用 Python。对不起,如果我看起来很迟钝。)

Eduardo Reis,感谢关于重复数组元素会导致某种索引问题的建议。我会调查的。

  • 欢迎来到堆栈溢出。你的样本输入是什么?
  • print(indx, word) 会比写这个问题要快。
  • index 本质上总是错误的工具。使用enumerate
  • 使用for indx, word in enumerate(passage):。请注意,在您的情况下,如果 word 在段落中重复,您将得到错误的结果
  • 欢迎来到堆栈溢出。出现问题是因为.index 发现首先列表中给定单词的索引。它不可能给你“当前单词”的索引,因为你是调用方法;它没有循环的上下文,它只看到单词,然后在列表中查找。解决方案是使用一个循环结构循环时为您提供索引,就像在第一个链接的副本中一样。

标签: python list modulo


【解决方案1】:

您可以将段落拆分为单词(在空格上),然后将该单词列表分成 5 个块,将每个块与索引连接起来(如果 > 0):

passage = """
When in the Course of human events, it becomes necessary
for one people to dissolve the political bands which have
connected them with another, and to assume among the powers 
of the earth, the separate and equal station to which the 
Laws of Nature and of Nature's God entitle them, a decent 
respect to the opinions of mankind requires that they should 
declare the causes which impel them to the separation.
"""
words = passage.split()
chunks = [' '.join(([str(i)] if i else []) + words[i:i+5]) for i in range(0, len(words), 5)]

输出:

[
 'When in the Course of',
 '5 human events, it becomes necessary',
 '10 for one people to dissolve',
 '15 the political bands which have',
 '20 connected them with another, and',
 '25 to assume among the powers',
 '30 of the earth, the separate',
 '35 and equal station to which',
 '40 the Laws of Nature and',
 "45 of Nature's God entitle them,",
 '50 a decent respect to the',
 '55 opinions of mankind requires that',
 '60 they should declare the causes',
 '65 which impel them to the',
 '70 separation.'
]

然后可以将其与 \n 连接以获得输出:

print('\n'.join(chunks))

输出:

When in the Course of
5 human events, it becomes necessary
10 for one people to dissolve
15 the political bands which have
20 connected them with another, and
25 to assume among the powers
30 of the earth, the separate
35 and equal station to which
40 the Laws of Nature and
45 of Nature's God entitle them,
50 a decent respect to the
55 opinions of mankind requires that
60 they should declare the causes
65 which impel them to the
70 separation.

【讨论】:

    猜你喜欢
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 2016-07-07
    • 2014-06-24
    • 2013-04-28
    • 2019-07-03
    相关资源
    最近更新 更多