Looping seems to not follow sequence

后端 未结 3 1928
耶瑟儿~
耶瑟儿~ 2020-12-20 20:24

I feel like I\'m missing something obvious here!

seq = {\'a\': [\'1\'], \'aa\': [\'2\'], \'aaa\': [\'3\'], \'aaaa\': [\'4\'], \'aaaaa\': [\'5\']}
for s in seq         


        
3条回答
  •  不知归路
    2020-12-20 21:04

    why you don't do (dictionary are not ordered):

    for s in range(5):
        print 'a'*s
    

    Edit : ok as you which :)

    the thing is in the expression : 'a'*s which mean create a new string which contain s time 'a'.

    in the python interpreter you can play with it (isn't python wonderful :) )

    >>> print 'a'*2
    aa
    >>> print 'a'*3
    aaa 
    

    PS: if you're new to python i will suggest that you use ipython if you don't use it yet.

提交回复
热议问题