code_junkie

Confusing […] List in Python: What is it?

Nov 13th, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. >>> a
  2. [[[[[], [], 8, 3], [[], [], 3, 2], 6, 3], [], 1, 4], [[], [], -4, 2], 0, 0]
  3. >>> Keys(a)#With a+b
  4. [0, 1, 6, 8, 3, -4]
  5. >>> Keys(a)#With [a,b]
  6. [8, [...], [...], 3, [...], [...], 6, [...], [...], 1, [...], [...], -4, [...], [...], 0, [...], [...]]
  7. >>> Keys(a)[1]#??
  8. [8, [...], [...], 3, [...], [...], 6, [...], [...], 1, [...], [...], -4, [...], [...], 0, [...], [...], 8, [...], [...], 3, [...], [...], 6, [...], [...], 1, [...], [...], -4, [...], [...], 0, [...], [...]]
  9.  
  10. def Keys(x,y=[]):
  11. if len(x):y+=[x[2]]+Keys(x[0],y)+Keys(x[1],y)#Though it seems I was using y=y[:]+, this actually outputs an ugly mess
  12. return y
  13.  
  14. def Keys(x,y=[]):
  15. if len(x):y+=[x[2],Keys(x[0],y),Keys(x[1],y)]
  16. return y
  17.  
  18. >>> a = [1,2]
  19. >>> a.append(a)
  20. >>> a
  21. [1, 2, [...]]
  22. >>>
  23.  
  24. return [x[2]]+keys(x[0])+keys(x[1])
  25.  
  26. y += [x[2]]
  27. keys(x[0], y) #Add left children to y...
  28. keys(x[1], y) #Add right children to y...
  29. return y
  30.  
  31. >>> a = [0, 1]
  32. >>> a[0] = a
  33. >>> a
  34. [[...], 1]
  35.  
  36. >>> a = [0, 1]
  37. >>> a[0] = a
  38. >>> a
  39. [[...], 1]
  40.  
  41. >>> a = [0, 1]
  42. >>> a[0] = a
  43. >>> a
  44. [[...], 1]
  45.  
  46. >>> a = [0, 1]
  47. >>> a[0] = a
  48. >>> a
  49. [[...], 1]
  50.  
  51. >>> a = [0, 1]
  52. >>> a[0] = a
  53. >>> a
  54. [[...], 1]
  55.  
  56. >>> a = [0]
  57. >>> a.append(a)
  58. >>> print a
  59. [0, [...]]
  60.  
  61. [0, [...]]
  62.  
  63. [0, [0, [...]]]
  64.  
  65. [0, [0, [0, [...]]]]
  66. [0, [0, [0, [0, [...]]]]]
  67. [0, [0, [0, [0, [0, [...]]]]]]
  68. ...
  69.  
  70. print
  71.  
  72. y = y[:]
  73.  
  74. y+=[x[2]]+Keys(x[0],y)+Keys(x[1],y)
  75.  
  76. y+=[x[2]]+Keys(x[0],y)+Keys(x[1],y)
  77.  
  78. y+=[x[2]]+Keys(x[0],y)+Keys(x[1],y)
  79.  
  80. y+=[x[2]]+Keys(x[0],y)+Keys(x[1],y)
  81.  
  82. y+=[x[2],Keys(x[0],y),Keys(x[1],y)]
Add Comment
Please, Sign In to add comment