Lists used to be so easy, now I can hardly change them.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BurnTard
    New Member
    • May 2007
    • 52

    Lists used to be so easy, now I can hardly change them.

    I can hardly solve the simplest thing without asking thescripts for help... Must be getting late. If I keep this up, I'll be an admin within the month.
    This time, I have a list with six random numbers in it. Since the numbers are random, I don't know at which indexes they are. What I want to do is basically:

    [Code=python]
    str= ""
    str=raw_input(" Random gibberish")
    a=[random numbers]

    if str in a:
    print "Random gibberish"
    del str in a # As in "I want the number he just typed to be removed from the list"

    [/Code]

    Which of course does not work. Python dislikes the "del str in a", of course, and I've been trying various things (basically just typed in english words and prayed) but to no avail. Does anyone have a smart way to do this? Try to keep it simple, mind...
  • Smygis
    New Member
    • Jun 2007
    • 126

    #2
    The string "1" is not equal to the integer "1".

    [code=python]
    >>> lst = [2,1,5,2]
    >>> num = "1"
    >>> num in lst
    False
    >>> num = int("1")
    >>> num in lst
    True
    >>> lst.index(num)
    1
    >>> dir(lst)
    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute __', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__' , '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
    >>> print lst.remove.__do c__
    L.remove(value) -- remove first occurrence of value
    >>> lst.remove(num)
    >>> lst
    [2, 5, 2]
    [/code]

    One tip is to lern how to use doc strings and the dir function.

    Comment

    • BurnTard
      New Member
      • May 2007
      • 52

      #3
      Originally posted by Smygis
      The string "1" is not equal to the integer "1".

      [code=python]
      >>> lst = [2,1,5,2]
      >>> num = "1"
      >>> num in lst
      False
      >>> num = int("1")
      >>> num in lst
      True
      >>> lst.index(num)
      1
      >>> dir(lst)
      ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute __', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__' , '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
      >>> print lst.remove.__do c__
      L.remove(value) -- remove first occurrence of value
      >>> lst.remove(num)
      >>> lst
      [2, 5, 2]
      [/code]

      One tip is to lern how to use doc strings and the dir function.
      Do tell, how do I use doc strings and the dir function?

      Comment

      • Smygis
        New Member
        • Jun 2007
        • 126

        #4
        simply do:
        [code=python]
        print watheveryouwatt oknowabut.__doc __
        [/code]
        that is <dot><underscor e><underscore>d oc<underscore>< underscore>
        exaple:
        [code=python]>>> print dir.__doc__
        dir([object]) -> list of strings

        Return an alphabetized list of names comprising (some of) the attributes
        of the given object, and of attributes reachable from it:

        No argument: the names in the current scope.
        Module object: the module attributes.
        Type or class object: its attributes, and recursively the attributes of
        its bases.
        Otherwise: its attributes, its class's attributes, and recursively the
        attributes of its class's base classes.
        [/code]

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by BurnTard
          I can hardly solve the simplest thing without asking thescripts for help... Must be getting late. If I keep this up, I'll be an admin within the month.
          This time, I have a list with six random numbers in it. Since the numbers are random, I don't know at which indexes they are. What I want to do is basically:

          [Code=python]
          str= ""
          str=raw_input(" Random gibberish")
          a=[random numbers]

          if str in a:
          print "Random gibberish"
          del str in a # As in "I want the number he just typed to be removed from the list"

          [/Code]

          Which of course does not work. Python dislikes the "del str in a", of course, and I've been trying various things (basically just typed in english words and prayed) but to no avail. Does anyone have a smart way to do this? Try to keep it simple, mind...
          Do not use a Python built-in function name for a variable (str). Maybe this will help:[code=Python]>>> numList = [random.randint( 0,9) for _ in range(10)]
          >>> numList
          [2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
          >>> n = int(raw_input(' Enter a number'))
          >>> n
          2
          >>> while True:
          ... if n in numList:
          ... numList.remove( 2)
          ... else:
          ... break
          ...
          >>> numList
          [5, 8, 3, 4, 5, 9]
          >>> numList = [2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
          >>> numList.remove( 2)
          >>> numList
          [5, 8, 3, 2, 2, 4, 2, 5, 9]
          >>> [/code]OR[code=Python]>>> numList = [2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
          >>> while True:
          ... try: numList.remove( n)
          ... except: break
          ...
          >>> numList
          [5, 8, 3, 4, 5, 9]
          >>> [/code]

          Comment

          • Smygis
            New Member
            • Jun 2007
            • 126

            #6
            Originally posted by bvdet
            Do not use a Python built-in function name for a variable (str). Maybe this will help:[code=Python]>>> numList = [random.randint( 0,9) for _ in range(10)]
            >>> numList
            [2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
            >>> n = int(raw_input(' Enter a number'))
            >>> n
            2
            >>> while True:
            ... if n in numList:
            ... numList.remove( 2)
            ... else:
            ... break
            ...
            >>> numList
            [5, 8, 3, 4, 5, 9]
            >>> numList = [2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
            >>> numList.remove( 2)
            >>> numList
            [5, 8, 3, 2, 2, 4, 2, 5, 9]
            >>> [/code]OR[code=Python]>>> numList = [2, 5, 8, 3, 2, 2, 4, 2, 5, 9]
            >>> while True:
            ... try: numList.remove( n)
            ... except: break
            ...
            >>> numList
            [5, 8, 3, 4, 5, 9]
            >>> [/code]
            OR
            [code=Python]
            while n in numList: numList.remove( n)
            [/code]
            God night. 2.21AM now.
            Signing off.

            Comment

            • ghostdog74
              Recognized Expert Contributor
              • Apr 2006
              • 511

              #7
              Originally posted by Smygis
              [code=Python]
              while n in numList: numList.remove( n)
              [/code]
              .
              Minor difference between while 1 method and this method in general. if using "while 1" , in the loop, one can code for more than one condition to break.

              Comment

              Working...