472,971 Members | 1,949 Online

# list declaration

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
3 2884
bvdet
2,851 Expert Mod 2GB
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
126 100+
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
ghostdog74
511 Expert 256MB
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

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

### Similar topics

 5 by: G?nter Omer | last post by: Hi there! I'm just trying to compile a header file (unsing Borland C++ Builder 10)implementing a class, containing the declaration of the STL but it refuses to work. The following... 57 by: Xarky | last post by: Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; }; 9 by: Michael Mair | last post by: Hello, in C89 (at least in the last public draft), "3.6.2 Compound statement, or block", we have ,--- | Syntax | | compound-statement: | { ... 7 by: QiongZ | last post by: Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it... 0 by: lllomh | last post by: Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{ 2 by: DJRhino | last post by: Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing... 2 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central... 0 by: tracyyun | last post by: Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to... 2 by: giovanniandrean | last post by: The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions... 3 by: NeoPa | last post by: Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all... 0 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on... 0 by: isladogs | last post by: The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike... 3 by: GKJR | last post by: Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.