The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,089 other followers

Python – list transformation; string formatting – Stack Overflow

Posted by jpluimers on 2020/01/08

Sometimes simple examples are the best: [WayBack] Python – list transformation – Stack Overflow.

Interactive example (note you can run and save at repl.it in either [WayBack] Repl.it – Python 3 or [WayBack] Repl.it – Python 2; you can run but not save it at [WayBack] Welcome to Python.org: interactive Python shell):

# Links the documentation are Python 2, though everything works in Python 3 as well.

x = [1,2,3,4,5,11]
print("x: ", repr(x))

y = ['01','02','03','04','05','11']
print("y: ", repr(y))

# List comprehension https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions
# ... using `str.format()` (Python >= 2.6): https://docs.python.org/2/library/stdtypes.html#str.format and https://docs.python.org/2/library/string.html#formatstrings
y = ["{0:0>2}".format(v) for v in x]
print("y: ", repr(y))

# ... using the traditional `%` formatting operator (Python < 2.6): https://docs.python.org/2/library/stdtypes.html#string-formatting y = ["%02d" % v for v in x] print("y: ", repr(y)) # ... using the format()` function (Python >= 2.6): https://docs.python.org/2/library/functions.html#format and https://docs.python.org/2/library/string.html#formatstrings
# this omits the "{0:...}" ceremony from the positional #0 parameter
y = [format(v, "0>2") for v in x]
print("y: ", repr(y))

# Note that for new style format strings, the positional argument (to specify argument #0) is optional (Python >= 2.7) https://docs.python.org/2/library/string.html#formatstrings
y = ["{:0>2}".format(v) for v in x]
print("y: ", repr(y))

# Using `lambda`
# ... Python < 3 return a list y = map(lambda v: "%02d" %v, x) print("y: ", repr(y)) # ... Python >= 3 return a map object to iterate over https://stackoverflow.com/questions/1303347/getting-a-map-to-return-a-list-in-python-3-x/1303354#1303354
y = list(map(lambda v: "%02d" %v, x))
print("y: ", repr(y))

Output:

Python 3 Python 2
Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
   
x:  [1, 2, 3, 4, 5, 11]
y:  ['01', '02', '03', '04', '05', '11']
y:  ['01', '02', '03', '04', '05', '11']
y:  ['01', '02', '03', '04', '05', '11']
y:  ['01', '02', '03', '04', '05', '11']
y:  ['01', '02', '03', '04', '05', '11']
y:  <map object at 0x7fe1218200b8>
y:  ['01', '02', '03', '04', '05', '11']
Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.8.2] on linux
   
('x: ', '[1, 2, 3, 4, 5, 11]')
('y: ', "['01', '02', '03', '04', '05', '11']")
('y: ', "['01', '02', '03', '04', '05', '11']")
('y: ', "['01', '02', '03', '04', '05', '11']")
('y: ', "['01', '02', '03', '04', '05', '11']")
('y: ', "['01', '02', '03', '04', '05', '11']")
('y: ', "['01', '02', '03', '04', '05', '11']")
('y: ', "['01', '02', '03', '04', '05', '11']")

–jeroen

# Links the documentation are Python 2, though everything works in Python 3 as well.
x = [1,2,3,4,5,11]
print("x: ", repr(x))
y = ['01','02','03','04','05','11']
print("y: ", repr(y))
# List comprehension https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions
# … using `str.format()` (Python >= 2.6): https://docs.python.org/2/library/stdtypes.html#str.format and https://docs.python.org/2/library/string.html#formatstrings
y = ["{0:0>2}".format(v) for v in x]
print("y: ", repr(y))
# … using the traditional `%` formatting operator (Python < 2.6): https://docs.python.org/2/library/stdtypes.html#string-formatting
y = ["%02d" % v for v in x]
print("y: ", repr(y))
# … using the format()` function (Python >= 2.6): https://docs.python.org/2/library/functions.html#format and https://docs.python.org/2/library/string.html#formatstrings
# this omits the "{0:…}" ceremony from the positional #0 parameter
y = [format(v, "0>2") for v in x]
print("y: ", repr(y))
# Note that for new style format strings, the positional argument (to specify argument #0) is optional (Python >= 2.7) https://docs.python.org/2/library/string.html#formatstrings
y = ["{:0>2}".format(v) for v in x]
print("y: ", repr(y))
# Using `lambda`
# … Python < 3 return a list
y = map(lambda v: "%02d" %v, x)
print("y: ", repr(y))
# … Python >= 3 return a map object to iterate over https://stackoverflow.com/questions/1303347/getting-a-map-to-return-a-list-in-python-3-x/1303354#1303354
y = list(map(lambda v: "%02d" %v, x))
print("y: ", repr(y))

view raw
main.py
hosted with ❤ by GitHub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: