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

Python列表和列表

Python列表和列表,python,list,python-3.x,yield,Python,List,Python 3.x,Yield,对于ProjectEuler问题24,我有以下(正确的)解决方案。我对Python比较陌生,在一些Python要点上遇到了难题 首先,守则: # A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. # If all of the permutations are listed numericall

对于ProjectEuler问题24,我有以下(正确的)解决方案。我对Python比较陌生,在一些Python要点上遇到了难题

首先,守则:

# A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4.
# If all of the permutations are listed numerically or alphabetically, we call it lexicographic order.
# The lexicographic permutations of 0, 1 and 2 are: 012 021 102 120 201 210
# What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?

permutations = []

def getLexicographicPermutationsOf(digits, state):
    if len(digits) == 0:
        permutations.append(str(state))

    for i in range(len(digits)):
        state.append(digits[i])
        rest = digits[:i] + digits[i+1:]
        getLexicographicPermutationsOf(rest, state)
        state.pop()

digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
getLexicographicPermutationsOf(digits, [])
print(permutations[999999])
我的第一个问题是关于yield语句的使用。我的第一个设计不是在顶部定义排列列表,而是用
屈服状态
替换
排列。然后,我会将该方法的返回值赋给一个变量。我检查了,返回值是一个生成器,正如预期的那样。然而,对其内容的循环表明没有生成任何值。我是不是遗漏了什么


我的第二个查询是关于最后一行的——从列表中打印一个值。当我运行它时,它输出的值就像它是一个列表一样,而它应该是一个字符串。事实上,将
打印(排列[999999])
替换为
打印(类型(排列[999999])
会导致
。那么为什么要像列表一样打印(用方括号,用逗号分隔)?

当您递归调用
getLexicographicPermutationsOf
时,您也需要从中生成结果

for result in getLexicographicPermutationsOf(rest, state):
    yield result

permutations.append(str(state))
创建
state
的字符串表示形式,它是一个列表。这就解释了为什么它在打印时看起来像一个列表。

有一种计算量小得多的方法来计算它。编写一个程序可能并不容易,但它可以让你手工计算出答案。:)(提示:有多少个排列?有多少个以0开头?)

而且,
range(len(x))
是高度非Pythonic的。当然,最好有索引,以便对列表进行切片以删除“当前”元素。。。但是还有另一种方法:只要让Python删除具有该值的元素(因为只有一个这样的元素)。这允许我们直接循环元素值:

for digit in digits:
    state.append(digit)
    rest = digits[:]
    rest.remove(digit) # a copy with the current value removed.
    getLexicographicPermutationsOf(rest, state)
    state.pop()
范围
主要用于实际创建数据范围-例如,使用以下命令初始化
数字

我是不是遗漏了什么

你没有意识到仅仅递归调用一个函数并不能神奇地将结果放在任何地方。事实上,即使您“产生”递归调用的结果,它仍然不会做您想要做的事情-当您需要一个生成器时,您将得到一个返回生成器的生成器(返回生成器,等等…直到递归的底部)。(FogleBird的回答解释了如何处理这个问题:您必须从递归调用中获取生成器,并将其生成的元素显式地“馈送”到当前生成器中。)

但无论如何,还有一种更简单的方法:该库已经内置了这种算法

整个程序可以这样完成:

from itertools import permutations, islice
print next(islice(permutations(range(10)), 1000000, None))
为什么要像列表一样打印(用方括号,用逗号分隔)


因为字符串包含方括号和逗号。这就是在列表上使用
str
state
)时得到的结果。

关于第二个问题,您打印的内容似乎是列表,因为
str(state)
其中state是列表,是列表的字符串表示形式。如果您想连接state的内容,请使用
'.join(state)
@Alex这是正确的想法,但是现在
state
的内容是整数而不是字符串,因此不能以这种方式连接它们。它们必须首先单独转换,例如,
'.join(str(x)表示处于状态的x)