gpt4 book ai didi

python - 如何将列表迭代到元组中

转载 作者:行者123 更新时间:2023-11-28 19:48:58 25 4
gpt4 key购买 nike

我正在编写一个程序,其中我在这样的列表中有一堆元组:

[('a01', '01-24-2011', 's1'), ('a03', '01-24-2011', 's2')

元组是格式

(animal ID, date(month, day, year), station# )

我不知道如何访问有关月份的信息。

我试过:

months = []    
for item in list:
for month in item:
if month[0] not in months:
months.append(month[0])

我在 python 3 中工作。

最佳答案

L = [('a01', '01-24-2011', 's1'), ('a03', '01-24-2011', 's2')]
for animal, date, station in L:
month, day, year = date.split('-')
print("animal ID {} in month {} at station {}".format(animal, month, station))

输出:

animal ID a01 in month 01 at station s1
animal ID a03 in month 01 at station s2

关于python - 如何将列表迭代到元组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27081463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com