473,796 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

list declaration

16 New Member
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 2927
bvdet
2,851 Recognized Expert Moderator Specialist
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,'appea rs 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 New Member
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 Recognized Expert Contributor
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,'appea rs 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
8711
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 <list> but it refuses to work. The following errors occure: "test.h" line 29 type name expected "test.h" line 29 declaration missing
57
4306
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
9
1510
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: | { declaration-list<opt> statement-list<opt> }
7
5774
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 compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
9679
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9527
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10453
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.