简体   繁体   中英

Removing brackets in a list of tuples

I have a list of tuple as below.

>>>> date_lst 

[(2013, 8, 13, 17),
 (2013, 8, 5, 17),
 (2013, 6, 26, 17),
 (2013, 8, 7, 17),
 (2013, 8, 7, 18),
 (2013, 8, 8, 16),
 (2013, 8, 8, 18),
 (2013, 8, 7, 17),
 (2013, 8, 7, 17)]

I want this list to print output as below. I would like to remove tuple brackets. What are some possible ways to do so?

2013, 8, 13, 17
2013, 8, 5, 17
2013, 6, 26, 17
2013, 8, 7, 17
2013, 8, 7, 18
2013, 8, 8, 16
2013, 8, 8, 18
2013, 8, 7, 17
2013, 8, 7, 17

将元组中的每个项目转换为字符串,然后加入它们。

[', '.join(map(str, x)) for x in date_lst]

The brackets aren't in the data, so there is nothing to remove. The brackets and parenthesis only show up when you try to print the data. You simply need to extract the data from the list:

for data in data_lst:
    print("%s, %s, %s, %s" % data)
datelist =[(2013, 8, 13, 17),
...  (2013, 8, 5, 17),
...  (2013, 6, 26, 17),
...  (2013, 8, 7, 17),
...  (2013, 8, 7, 18),
...  (2013, 8, 8, 16),
...  (2013, 8, 8, 18),
...  (2013, 8, 7, 17),
...  (2013, 8, 7, 17)]

now command like

for date in datelist:
    print(date[0], date[1], date[2], date[3])

then you will get

2013 8 13 17
2013 8 5 17
2013 6 26 17
2013 8 7 17
2013 8 7 18
2013 8 8 16
2013 8 8 18
2013 8 7 17
2013 8 7 17

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