Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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_String_Sequences - Fatal编程技术网

Python 神秘的蟒蛇行为

Python 神秘的蟒蛇行为,python,string,sequences,Python,String,Sequences,我正在尝试编写一个快速脚本来输出序列。 顺序如下所示: 1,11,21,1211,111221,312211... 从1开始,第二项是11,第三项是21的ect 我在下面写了一个脚本,它按照这个顺序输出术语。 当输入字符串为1,即11时,它输出正确的下一项。但之后的所有术语也输出为11。。。。。如果我手动将输入设置为11,则所有术语都计算正确 这毫无意义,因为对于“1”的初始输入,mytotal_sequence[0]变量将是“1”,从而给出“11”的正确输出。 下一个术语将有total_seq

我正在尝试编写一个快速脚本来输出序列。 顺序如下所示: 1,11,21,1211,111221,312211... 从1开始,第二项是11,第三项是21的ect

我在下面写了一个脚本,它按照这个顺序输出术语。 当输入字符串为1,即11时,它输出正确的下一项。但之后的所有术语也输出为11。。。。。如果我手动将输入设置为11,则所有术语都计算正确

这毫无意义,因为对于
“1”
的初始输入,my
total_sequence[0]
变量将是
“1”
,从而给出
“11”
的正确输出。 下一个术语将有
total_sequence[1]
作为其输入,即
“11”
,即最后的迭代输出。 但这会将
“11”
作为其输出。 如果我直接将
“11”
作为总序列[1]项而不是引用
总序列[1]
(它=
“11”
),则会给出正确的输出

那么基本上为什么
input_string=“11”
output[1]
不同,其中
output=[“1”,“11”]


您正在循环体中使用
input\u string
,但这一点从未改变,它始终是第一个字符串,在您的示例
“1”

可能会使用以下命令更改内部循环的第一行:

input_string = total_sequence[i]
for current_char in input_string:
应该足够了,但我没有测试它。

这被称为“看然后说”序列;可以使用
itertools更简单地实现它。groupby

from itertools import groupby

def look_and_say(n):
    """
    Generate the first `n` terms of the sequence
      '1', '11', '21', '1211', ...
    """
    if n < 1:
        return

    # first term
    term = "1"
    yield term

    # second through `n`th terms
    for term_num in range(1, n):
        chars = []
        for ch,reps in groupby(term):
            count = len(list(reps))
            chars.append(str(count))
            chars.append(ch)
        term = "".join(chars)
        yield term
给予


有趣的奖励:证明序列中不会包含高于3的数字。

这似乎是Python挑战中的一个问题。很可能是。在您发表评论之前,我从未听说过python挑战。我从朋友们的一本拼图书中获得了这个序列,其中的拼图是估计这个序列第n项中的字符数。这是一个经典的拼图系列,你可以在Python挑战中找到它,也可以在很多关于拼图的书中找到它。我现在肯定觉得自己像个白痴了。谢谢你罗德里戈敏锐的眼睛。
from itertools import groupby

def look_and_say(n):
    """
    Generate the first `n` terms of the sequence
      '1', '11', '21', '1211', ...
    """
    if n < 1:
        return

    # first term
    term = "1"
    yield term

    # second through `n`th terms
    for term_num in range(1, n):
        chars = []
        for ch,reps in groupby(term):
            count = len(list(reps))
            chars.append(str(count))
            chars.append(ch)
        term = "".join(chars)
        yield term
N = 5
print(list(look_and_say(N)))
['1', '11', '21', '1211', '111221']