By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,842 Members | 2,186 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,842 IT Pros & Developers. It's quick & easy.

list declaration

P: 16
hey guys it me again i got stuck in the middle of this program. The program starts with a list declaration of these numbers [1, 2, 4, 5, 6, 7, 8, 9, 10. 2, 53, 12] , i have to make sure the program outputs the following

1 The number of times 2 occurs in the list numbers
2 The position of the first occurrence of 2 in numbers.
3 The position of the next occurrence of 2 in numbers
4 The position of the last item in numbers
5 Sort the items of numbers.
6 The reversal of items in numbers.
7 Appending the value 99 to numbers.
8 Inserting -1 into numbers before position 0
9 Deletes and returns the last item of numbers.

and after each operation, i have to print out the list of numbers to show the
operation has been successful.

here is what i have got stuck on so far.
Expand|Select|Wrap|Line Numbers
  1. def declaration():
  2.     numbers = ['1','2','4','5','6','7','8','9','10.2','53','12'];
  3.     for i in range(1):
  4.         print'number of times',2,'appears in the list:',numbers.count('2');
  5.  
Sorry i tried to look for the posting guidelines to make sure that i post this programme right but i couldnt if someone can let me where i get it from thanks
Jun 1 '07 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
hey guys it me again i got stuck in the middle of this program. The program starts with a list declaration of these numbers [1, 2, 4, 5, 6, 7, 8, 9, 10. 2, 53, 12] , i have to make sure the program outputs the following

1 The number of times 2 occurs in the list numbers
2 The position of the first occurrence of 2 in numbers.
3 The position of the next occurrence of 2 in numbers
4 The position of the last item in numbers
5 Sort the items of numbers.
6 The reversal of items in numbers.
7 Appending the value 99 to numbers.
8 Inserting -1 into numbers before position 0
9 Deletes and returns the last item of numbers.

and after each operation, i have to print out the list of numbers to show the
operation has been successful.

here is what i have got stuck on so far.

def declaration():
numbers = ['1','2','4','5','6','7','8','9','10.2','53','12'];
for i in range(1):
print'number of times',2,'appears in the list:',numbers.count('2');

Sorry i tried to look for the posting guidelines to make sure that i post this programme right but i couldnt if someone can let me where i get it from thanks
I have posted this function a few times on the forum, and here it is again:
Expand|Select|Wrap|Line Numbers
  1. """
  2. Return an index list of all occurrances of 'item' in string/list 's'.
  3. Optional start search position 'i'
  4. """
  5. def indexList(s, item, i=0):
  6.     i_list = []
  7.     while True:
  8.         try:
  9.             i = s.index(item, i)
  10.             i_list.append(i)
  11.             i += 1
  12.         except:
  13.             return i_list
Expand|Select|Wrap|Line Numbers
  1. >>> numList = [1, 2, 4, 5, 6, 7, 8, 9, 10, 2, 53, 12]
  2. >>> indexList(lst, 2)
  3. [1, 9]
  4. >>> 
Code tags should placed before and after your code. The beginning code tag is '[code=Python]'. The closing code tag is '[ / c o d e ]'. I put spaces in between the letters of the closing code tag so it would display.
Jun 1 '07 #2

Smygis
100+
P: 126
Expand|Select|Wrap|Line Numbers
  1. >>> lst = [1, 2, 4, 5, 6, 7, 8, 9, 10, 2, 53, 12]
  2. >>> for i in dir(lst): print i
  3. ... 
  4. # Cut away som stuff, All info you need down below
  5. append
  6. count
  7. extend
  8. index
  9. insert
  10. pop
  11. remove
  12. reverse
  13. sort
  14. >>> lst.count(2)
  15. 2
  16. >>> lst.index(2)
  17. 1
  18. # look at above post for this one
  19. >>> len(lst)-1
  20. 11
  21. >>> lst.sort()
  22. >>> lst
  23. [1, 2, 2, 4, 5, 6, 7, 8, 9, 10, 12, 53]
  24. >>> lst.reverse()
  25. >>> lst
  26. [53, 12, 10, 9, 8, 7, 6, 5, 4, 2, 2, 1]
  27. >>> lst.append(99)
  28. >>> lst
  29. [53, 12, 10, 9, 8, 7, 6, 5, 4, 2, 2, 1, 99]
  30. >>> lst.insert(0,-1)
  31. >>> lst
  32. [-1, 53, 12, 10, 9, 8, 7, 6, 5, 4, 2, 2, 1, 99]
  33. >>> lst.pop()
  34. 99
  35. >>> lst
  36. [-1, 53, 12, 10, 9, 8, 7, 6, 5, 4, 2, 2, 1]
  37.  
Jun 1 '07 #3

Expert 100+
P: 511
1 The number of times 2 occurs in the list numbers
you already know how to use count
2 The position of the first occurrence of 2 in numbers.
3 The position of the next occurrence of 2 in numbers
you can store them in a dictionary
Expand|Select|Wrap|Line Numbers
  1. d={}
  2. a=[1, 2, 4, 5, 6, 7, 8, 9, 10, 2, 53, 12]
  3. for num,item in enumerate(a):
  4.     d.setdefault(item,[])
  5.     d[item].append(num)
  6. print d
  7.  
output:
Expand|Select|Wrap|Line Numbers
  1. {1: [0], 2: [1, 9], 4: [2], 5: [3], 6: [4], 7: [5], 8: [6], 9: [7], 10: [8], 12: [11], 53: [10]}
  2.  
the values will be the indexes of each number
4 The position of the last item in numbers
use len().
5 Sort the items of numbers.
Expand|Select|Wrap|Line Numbers
  1. sorted(a)
  2.  
6 The reversal of items in numbers.
reversed(a) #note this is iterator
7 Appending the value 99 to numbers.
use the append() method
8 Inserting -1 into numbers before position 0
insert()
9 Deletes and returns the last item of numbers.
pop(), remove()

here is what i have got stuck on so far.

def declaration():
numbers = ['1','2','4','5','6','7','8','9','10.2','53','12'];
for i in range(1):
print'number of times',2,'appears in the list:',numbers.count('2');
and you should have already read the Python tutorial/references. these based stuffs are all covered in the Python documents.
Jun 1 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.