Convert integers inside a list into strings and then a date in python 3.x

R.Salerno

i've just started studying python in college and i have a problem with this exercise: basically i have to take a list of integers, like for example [10,2,2013,11,2,2014,5,23,2015], turn the necessary elements to form a date into a string, like ['1022013',1122014,5232015] and then put a / between the strings so i have this ['10/2/2013', '11/22/2014','05/23/2015']. It needs to be a function, and the length of the list is assumed to be a multiple of 3. How do i go about doing this? I wrote this code to start:

def convert(lst):
     ...:     for element in lst:
     ...:      result = str(element)
     ...:      return result
     ...:

but from a list [1,2,3] only returns me '1'.

Patrick Haugh

To split your list into size 3 chunks you use a range with a step of 3

for i in range(0, len(l), 3):
    print(l[i:i+3])

And joining the pieces with / is as simple as

'/'.join([str(x) for x in l[i:i+3]])

Throwing it all together into a function:

def make_times(l):
    results = []
    for i in range(0, len(l), 3):
        results.append('/'.join([str(x) for x in l[i:i+3]]))
    return results

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert a list of strings of lists to a list of integers

How to convert strings into integers in Python?

list containing integers and strings in python

In PyParsing, how to define a setParseAction function to convert a list of strings to a list of integers?

convert a list of strings list into integers

Limiting user input in a list of integers in Python 3.x

How to convert list of strings to list of integers?

Convert list of integers to an integer python

How to convert a list of strings to a list of ints in Python 3x

Python converting list of strings to list of integers

Convert a list of strings containing list-of-integers into an array

Find Index Of Lowest Number In List of Integers and Strings in Python3

How to convert numerical strings inside a mixed list of list to int (Python)

Convert a list of strings to a list of integers

Convert list of integers to list of bits Python

Convert list of strings into tuple of integers

Trying to use list comprehension to convert list of strings to integers?

How to convert nested list strings to integers then sort them in python 3?

python capitalize nested list with strings and integers

Convert strings to list Python

convert list of strings from file to list of integers

Convert array of Strings to list of Integers?

Convert string representing timestamp to date format inside python list

How do I convert a list of strings to integers in Python

Python list: Extract integers from list of strings

Convert a list of integers to list of strings

Encode and decode list of strings or integers in python

Pandas convert column where every cell is list of strings to list of integers

Convert list of strings into list of integers