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
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: - """
-
Return an index list of all occurrances of 'item' in string/list 's'.
-
Optional start search position 'i'
-
"""
-
def indexList(s, item, i=0):
-
i_list = []
-
while True:
-
try:
-
i = s.index(item, i)
-
i_list.append(i)
-
i += 1
-
except:
-
return i_list
- >>> numList = [1, 2, 4, 5, 6, 7, 8, 9, 10, 2, 53, 12]
-
>>> indexList(lst, 2)
-
[1, 9]
-
>>>
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.
-
>>> lst = [1, 2, 4, 5, 6, 7, 8, 9, 10, 2, 53, 12]
-
>>> for i in dir(lst): print i
-
...
-
# Cut away som stuff, All info you need down below
-
append
-
count
-
extend
-
index
-
insert
-
pop
-
remove
-
reverse
-
sort
-
>>> lst.count(2)
-
2
-
>>> lst.index(2)
-
1
-
# look at above post for this one
-
>>> len(lst)-1
-
11
-
>>> lst.sort()
-
>>> lst
-
[1, 2, 2, 4, 5, 6, 7, 8, 9, 10, 12, 53]
-
>>> lst.reverse()
-
>>> lst
-
[53, 12, 10, 9, 8, 7, 6, 5, 4, 2, 2, 1]
-
>>> lst.append(99)
-
>>> lst
-
[53, 12, 10, 9, 8, 7, 6, 5, 4, 2, 2, 1, 99]
-
>>> lst.insert(0,-1)
-
>>> lst
-
[-1, 53, 12, 10, 9, 8, 7, 6, 5, 4, 2, 2, 1, 99]
-
>>> lst.pop()
-
99
-
>>> lst
-
[-1, 53, 12, 10, 9, 8, 7, 6, 5, 4, 2, 2, 1]
-
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 -
d={}
-
a=[1, 2, 4, 5, 6, 7, 8, 9, 10, 2, 53, 12]
-
for num,item in enumerate(a):
-
d.setdefault(item,[])
-
d[item].append(num)
-
print d
-
output: -
{1: [0], 2: [1, 9], 4: [2], 5: [3], 6: [4], 7: [5], 8: [6], 9: [7], 10: [8], 12: [11], 53: [10]}
-
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: Xarky |
last post by:
Hi,
I am writing a linked list in the following way.
struct list
{
struct list *next;
char *mybuff;
};
|
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:
| { ...
|
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...
|
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=()=>{
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |