How To Split

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ekasII
    New Member
    • Jul 2009
    • 16

    How To Split

    I have a list:
    BMW.blue
    VW.red
    AUDI.blue

    I used re.search to get all blue cars
    BMW.blue
    AUDI.blue

    How do I use split to get only the cars without the dotColour:
    BMW
    AUDI
    (without specifing the cars, as the list changes depending on the cur dir)
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Example:
    Code:
    >>> carlist = ['BMW.blue','VW.red','AUDI.blue']
    >>> [item.split('.')[0] for item in carlist if 'blue' in item]
    ['BMW', 'AUDI']
    >>>

    Comment

    • ekasII
      New Member
      • Jul 2009
      • 16

      #3
      Nice Work! But how do i get the end results without the square brackets and the single quote commas?
      I have This:
      ['BMW','AUDI']
      I want This:
      BMW
      AUDI

      But again Nice Work! and I realised that using [1] splits to the right.

      Comment

      • ekasII
        New Member
        • Jul 2009
        • 16

        #4
        Thanks I got It, I used:
        Code:
        if l__blue_cars:
        	l__split = (l__item.split('.')[0])
        notice that I used Round Brackets, there after i used "\n" to print on a different line.

        Nice Work Man!
        Last edited by bvdet; Aug 1 '09, 02:05 PM. Reason: Add code tags

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Here's another possibility:
          Code:
          >>> carlist = ['BMW.blue','VW.red','AUDI.blue']
          >>> for car in [item.split('.')[0] for item in carlist if 'blue' in item]:
          ... 	print car
          ... 	
          BMW
          AUDI
          >>>

          Comment

          Working...