-6
['From', 'stephen.marquard@uct.ac.za', 'Sat', 'Jan', '5', '09:14:16', '2008',
 'From:', 'stephen.marquard@uct.ac.za', 'From', 'louis@media.berkeley.edu',
 'Fri', 'Jan', '4', '18:10:48', '2008', 'From:', 'louis@media.berkeley.edu',
 'From', 'zqian@umich.edu', 'Fri', 'Jan', '4', '16:10:39', '2008', 'From:',
 'zqian@umich.edu', 'From', 'rjlowe@iupui.edu', 'Fri', 'Jan', '4', '15:46:24',
 '2008', 'From:', 'rjlowe@iupui.edu', 'From', 'zqian@umich.edu', 'Fri', 'Jan',
 '4', '15:03:18', '2008', 'From:', 'zqian@umich.edu', 'From',
 'rjlowe@iupui.edu', 'Fri', 'Jan', '4', '14:50:18', '2008', 'From:',
 'rjlowe@iupui.edu', 'From', 'cwen@iupui.edu', 'Fri', 'Jan', '4', '11:37:30',
 '2008', 'From:', 'cwen@iupui.edu', 'From', 'cwen@iupui.edu', 'Fri', 'Jan', '4',
 '11:35:08', '2008', 'From:', 'cwen@iupui.edu', 'From', 'gsilver@umich.edu',
 'Fri', 'Jan', '4', '11:12:37', '2008', 'From:', 'gsilver@umich.edu', 'From',
 'gsilver@umich.edu', 'Fri', 'Jan', '4', '11:11:52', '2008', 'From:',
 'gsilver@umich.edu', 'From', 'zqian@umich.edu', 'Fri', 'Jan', '4', '11:11:03',
 '2008', 'From:', 'zqian@umich.edu', 'From', 'gsilver@umich.edu', 'Fri', 'Jan',
 '4', '11:10:22', '2008', 'From:', 'gsilver@umich.edu', 'From',
 'wagnermr@iupui.edu', 'Fri', 'Jan', '4', '10:38:42', '2008', 'From:',
 'wagnermr@iupui.edu', 'From', 'zqian@umich.edu', 'Fri', 'Jan', '4', '10:17:43',
 '2008', 'From:', 'zqian@umich.edu', 'From', 'antranig@caret.cam.ac.uk', 'Fri',
 'Jan', '4', '10:04:14', '2008', 'From:', 'antranig@caret.cam.ac.uk', 'From',
 'gopal.ramasammycook@gmail.com', 'Fri', 'Jan', '4', '09:05:31', '2008',
 'From:', 'gopal.ramasammycook@gmail.com', 'From', 'david.horwitz@uct.ac.za',
 'Fri', 'Jan', '4', '07:02:32', '2008', 'From:', 'david.horwitz@uct.ac.za',
 'From', 'david.horwitz@uct.ac.za', 'Fri', 'Jan', '4', '06:08:27', '2008',
 'From:', 'david.horwitz@uct.ac.za', 'From', 'david.horwitz@uct.ac.za',]

Is there any of extracting a string of specific type like hh:mm:ss from a list? I want to extract this information without importing any module.

user3400176
  • 49
  • 1
  • 4
  • Did you check python's datetime to get time from string? – Jérôme Nov 25 '15 at 13:08
  • Alternatively you can use regular expressions – Unni Nov 25 '15 at 13:08
  • Possible duplicate of [Extracting date from a string in Python](http://stackoverflow.com/questions/3276180/extracting-date-from-a-string-in-python) – Mel Nov 25 '15 at 13:11
  • You're more likely to get better help if you show that you've put some effort into this. People are usually more willing to help fix failing code than generate code from nothing. – glibdud Nov 25 '15 at 13:14

2 Answers2

2

RegEx way:

>>> [i for i in your_list if re.match('\d{2}:\d{2}:\d{2}', i)]
['09:14:16', '18:10:48', '16:10:39', 
 '15:46:24', '15:03:18', '14:50:18', 
 '11:37:30', '11:35:08', '11:12:37', 
 '11:11:52', '11:11:03', '11:10:22', 
 '10:38:42', '10:17:43', '10:04:14', 
 '09:05:31', '07:02:32', '06:08:27', 
 '04:49:08', '04:33:44', '04:07:34', 
 '19:51:21', '17:18:23', '17:07:00', 
 '16:34:40', '16:29:07', '16:23:48']
>>> 
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
  • 1
    It's useful.But I want to find a way without importing any module.For this I have to "import re" .Just using list,tuple,string and dictionary methods.I tried finding ":" in list but that didnot work. – user3400176 Nov 25 '15 at 14:35
  • 1
    @user3400176 why can't you import any modules? `re` is part of the standard library... – MattDMo Nov 25 '15 at 18:44
1

RegExp would be the obvious option here, but we could emulate some checks:

  • string length 8
  • occurrence of two colon signs
  • integer-able parts after split (hh, mm, ss)
  • hours less than 24
  • minutes and seconds less than 60

better define a function here:

def hms(str):
  if len(str) != 8:
    return False
  parts = str.split(':')
  if len(parts) != 3:
    return False
  try:
    parts = [int(part) for part in parts]
  except ValueError as e:
    return False
  return parts[0] < 24 and parts[1] < 60 and parts[2] < 60

and feed it into the builtin filter() function:

filter(hms, big_data)

or cram them in a lambda for a one liner version:

filter(lambda x: len(x) == 8 and len(x.split(':')) == 3 and int(x.split(':')[0]) < 24 and int(x.split(':')[1]) < 60 and int(x.split(':')[2]) < 60, big_data)
CSᵠ
  • 10,049
  • 9
  • 41
  • 64