list declaration

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kasrav
    New Member
    • Apr 2007
    • 16

    #1

    list declaration

    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.
    Code:
    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
    Last edited by bartonc; Jun 1 '07, 05:34 PM. Reason: added [code][/code] tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Kasrav
    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:[code=Python]"""
    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[/code][code=Python]>>> numList = [1, 2, 4, 5, 6, 7, 8, 9, 10, 2, 53, 12]
    >>> indexList(lst, 2)
    [1, 9]
    >>> [/code]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.

    Comment

    • Smygis
      New Member
      • Jun 2007
      • 126

      #3
      [code=python]
      >>> 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]
      [/code]

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        Originally posted by Kasrav
        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
        Code:
        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:
        Code:
        {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.
        Code:
        sorted(a)
        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.

        Comment

        Working...