简体   繁体   中英

using python join to join first element of each list

I'd like to do this:

for qb in qb_data:
    qb_in += "'" + qb[0] + "'" + ","

using a join. Obviously the problem is that join just concatenates each member of the list. But here I have a list of lists and only need to join the first element of each list.

",".join("'%s'" % qb[0] for qb in qb_data)

You can still selectively pick out stuff in your generator expression/list comprehension, of course!

For example:

>>> qb_data = [['1', '2'], ['3', '4'], ['5', '6'], ['7', '8', '9']]
>>> ",".join("'%s'" % qb[0] for qb in qb_data)
"'1','3','5','7'"

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM