0

using Python 3.6.1

time_vector = ['06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '00', '01', '02', '03', '04', '05', '06']

doublezeroes = "00"

time=map(str(time_vector), doublezeroes)

print(time)

I get map object at 0x7ff64a3580f0 with the <>

I need

time = ['0600', '0700', '0800', '0900', '1000', '1100', '1200', '1300', '1400', '1500', '1600', '1700', '1800', '1900', '2000', '2100', '2200', '2300', '0000', '0100', '0200', '0300', '0400', '0500', '0600']

I am sure there might be an easier like append or insert just not sure how to get there. Thanks

5 Answers 5

1

The easiest way to do this is probably the following:

time = [v + doublezeroes for v in time_vector]
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need to use map, simply append the string to the list:

In [1132]: new = [ s + '00' for s in time_vector]

In [1133]: new
Out[1133]: 
['0600',
 '0700',
 '0800',
 '0900',
 '1000',
 '1100',
 '1200',
 '1300',
 '1400',
 '1500',
 '1600',
 '1700',
 '1800',
 '1900',
 '2000',
 '2100',
 '2200',
 '2300',
 '0000',
 '0100',
 '0200',
 '0300',
 '0400',
 '0500',
 '0600']

Comments

0

Try this one which is almost same of your code with lambda expression`added:

time_vector = ['06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '00', '01', '02', '03', '04', '05', '06']

time=list(map(lambda k: k+'00', time_vector))

print(time)

Comments

0

Use lambda function along with map to get output

Small anonymous functions can be created with the lambda keyword. This function returns the sum of its two arguments: lambda a, b: a+b. Lambda functions can be used wherever function objects are required. They are syntactically restricted to a single expression.

time_vector = ['06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '00', '01', '02', '03', '04', '05', '06']

doublezeroes = "00"

timelist=list(map(lambda x:x+doublezeroes, time_vector))

print(timelist)

OUTPUT

['0600', '0700', '0800', '0900', '1000', '1100', '1200', '1300', '1400', '1500', '1600', '1700', '1800', '1900', '2000', '2100', '2200', '2300', '0000', '0100', '0200', '0300', '0400', '0500', '0600']

Comments

0

You can use str.ljust

Ex:

time_vector = ['06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '00', '01', '02', '03', '04', '05', '06']
doublezeroes = "00"

print(list(map(lambda x: x.ljust(4, "0"), time_vector)))

Output:

['0600',
 '0700',
 '0800',
 '0900',
 '1000',
 '1100',
 '1200',
 '1300',
 '1400',
 '1500',
 '1600',
 '1700',
 '1800',
 '1900',
 '2000',
 '2100',
 '2200',
 '2300',
 '0000',
 '0100',
 '0200',
 '0300',
 '0400',
 '0500',
 '0600']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.