By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

How To Split

P: 16
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)
Jul 31 '09 #1
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Example:
Expand|Select|Wrap|Line Numbers
  1. >>> carlist = ['BMW.blue','VW.red','AUDI.blue']
  2. >>> [item.split('.')[0] for item in carlist if 'blue' in item]
  3. ['BMW', 'AUDI']
  4. >>> 
Jul 31 '09 #2

P: 16
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.
Aug 1 '09 #3

P: 16
Thanks I got It, I used:
Expand|Select|Wrap|Line Numbers
  1. if l__blue_cars:
  2.     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!
Aug 1 '09 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Here's another possibility:
Expand|Select|Wrap|Line Numbers
  1. >>> carlist = ['BMW.blue','VW.red','AUDI.blue']
  2. >>> for car in [item.split('.')[0] for item in carlist if 'blue' in item]:
  3. ...     print car
  4. ...     
  5. BMW
  6. AUDI
  7. >>> 
Aug 1 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.