code_junkie

How do you create a list like PHP's in Python?

Nov 13th, 2011
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. list = []
  2.  
  3. for row in rows:
  4. c = list_of_categories.index(row["id"])
  5. print c
  6. list[c] = row["name"]
  7.  
  8. d = {}
  9.  
  10. for row in rows:
  11. c = list_of_categories.index(row["id"])
  12. print c
  13. d[c] = row["name"]
  14.  
  15. d = dict((list_of_categories.index(row['id']), row['name']) for row in rows)
  16. print d
  17.  
  18. n = 3
  19. array = n * [None]
  20. print array
  21. array[2] = 11
  22. array[1] = 47
  23. array[0] = 42
  24. print array
  25.  
  26. [None, None, None]
  27. [42, 47, 11]
  28.  
  29. l = list ()
  30. l.append ("foo")
  31. l.append (3)
  32. print (l)
  33.  
  34. l = list ()
  35. l.append ("foo")
  36. l.append (3)
  37. l.insert (1, "new")
  38. print (l)
  39.  
  40. lst = [0] * n
  41.  
  42. lst = [some_object() for i in xrange(n)]
  43.  
  44. for i in xrange(n):
  45. lst[i] += 1
  46.  
  47. dict = {'a': 1, 'b': 2, 'c': 3}
  48. dict['a']
  49.  
  50. lst = [list_of_categories.index(row["id"]) for row in rows]
  51.  
  52. list = {}
  53.  
  54. for row in rows:
  55. c = list_of_categories.index(row["id"])
  56. print c
  57. list[c] = row["name"]
  58.  
  59. for x in list.values():
  60. print x
  61.  
  62. if 3 in list:
  63. print "it's there"
  64.  
  65. class MyList(UserList.UserList):
  66.  
  67. NO_ITEM = 'noitem'
  68.  
  69. def insertAt(self, item, index):
  70.  
  71. length = len(self)
  72. if index < length:
  73. self[index] = item
  74. elif index == length:
  75. self.append(item)
  76. else:
  77. for i in range(0, index-length):
  78. self.append(self.NO_ITEM)
  79. self.append(item)
Add Comment
Please, Sign In to add comment