Is it possible to convert the following to a python list comprehension:
values = [a,b,c,d,...]
converted_values = []
for item in values:
if type(item) == datetime.date:
converted_values.append(item)
else:
converted_values.append(item.decode('utf-8'))
You can use Conditional Expressions to make this work.
converted_values = [item if type(item) == datetime.data
else item.decode('utf-8')
for item in values]
Python conditionals are fairly readable. Here's some examples to show how they work:
print("yes" if True else "no") # prints "yes"
print("yes" if False else "no") # prints "no"
converted_values = [item if type(item) == datetime.date else item.decode('utf-8')
for item in values]
I assume you meant utf-8
.
Also, the Python docs recommend using isinstance(item, datetime.date)
rather than type(item) == datetime.date
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.