有没有办法访问元组中的值?

时间:2017-12-03 00:51:10

标签: python string python-3.x tuples

我想知道是否有办法访问元组中的值?

例如:

print("The date is" + '17-12-07' + " and time is" + '14:00')

我想要'17 -12-07'和'14:00'。有没有办法可以获取这些值并将它们附加到字符串中。

 for id,row in inst.iterrows():
     if not pr.contains(Point(row['lng'].astype(float),row['lat'].astype(float))):
         inst.drop(id,inplace=True)

 for id,row in ht.iterrows():
     if not pr.contains(Point(row['lng'].astype(float),row['lat'].astype(float))):
         ht.drop(id,inplace=True)

3 个答案:

答案 0 :(得分:4)

a = [('ANTA01H3F', 'LEC02', '17-12-07', '14:00')]

它不是元组 - 它是带有一个元素的列表 - 元组

要获得价值,您必须使用两个索引

date = a[0][2]

这是元组

a = ('ANTA01H3F', 'LEC02', '17-12-07', '14:00')

date = a[2]

答案 1 :(得分:1)

您可以解压缩元组,然后使用字符串格式打印:

a = [('ANTA01H3F', 'LEC02', '17-12-07', '14:00')]
_, _, date, time = a[0]
print(f"The date is {date} and time is {time}")

答案 2 :(得分:0)

您可以像使用索引一样在普通列表中访问它们。 如果tup=(1,2,3)tup[1]为2