简体   繁体   English

删除元组列表中的括号

[英]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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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