社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Nick Chapman  »  全部回复
回复总数  2
8 年前
回复了 Nick Chapman 创建的主题 » python-打印特定对象列表属性

你这儿有虫子。

class tests():

    def __init__(self,grade):
         self.grade = grade


test_list = []

for x in range(1,6):
    test_object = tests(x)
    test_list.append(test_object)

list_a = [5,3,2,1,4]

# THIS LINE BELOW IS THE PROBLEM    
for x in [test_list]:
    # in this line below X = test_list and indeed test_list.grade doesn't make sense.
    test_list.append(sorted(x.grade,key=list_a.index))


print (test_list[1].grade)
7 年前
回复了 Nick Chapman 创建的主题 » 如何在python中创建深嵌套循环

你是对的,有更好的方法来做这件事。您只需使用 product 功能来自 itertools 这完全等同于你要做的。这个 docs on it are here .

这很简单:

>>> from itertools import product
>>> outcomes = list(product(range(2), repeat=16))
>>> print(len(outcomes))
65536
>>> print(outcomes[0])
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
>>> print(outcomes[10000])
(0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0)

你会注意到这并没有给你 [0, 1] 你所期待的对,但很明显,因为每场比赛只有两队,只要把每个位置显示的数字作为第一队的得分,你就可以推断出另一队的得分与此相反。