如何从列表中插入的元组中删除括号?
how to remove brackets from an inserted tuple in a list?
有人可以帮我从元组列表中删除多余的括号吗?
latest_Value = {"17:00:00": 100.00}
Dict1 = {"current value": 100, "stock_purchased": "false", "Historic value": [('16:00:00', 55.50), ("15:00:00", 45.50), ("14:00:00", 75.50),("13:00:00", 65.50), ("12:00:00", 55.50)]}
# converting the latest_value into a tuple
List_it = [(k, v) for k, v in latest_Value.items()]
# insert the tuple into the tuple list
Dict1['Historic value'].insert(0, List_it)
data2 = Dict1["Historic value"]
print(data2)
#output
[[('17:00:00', 100.0)], ('16:00:00', 55.5), ('15:00:00', 45.5), ('14:00:00', 75.5), ('13:00:00', 65.5), ('12:00:00', 55.5)]
修改列表时,它会添加一个嵌套列表,而不仅仅是元组。
你如何避免这种情况?
亲切的问候,
安德鲁
# converting the latest_value into a tuple
List_it = [(k, v) for k, v in latest_Value.items()]
也就是说,其实是错误的。这会将整个字典转换为元组,而不仅仅是最后一个键值对,因此 Dict1['Historic value'].insert(0, List_it)
将整个元组列表添加到 Dict1['Historic value']
.
latest_Value
包含单个键值对的事实不会改变 List_it
将是元组列表的事实。
如果你改为Dict1['Historic value'].insert(0, List_it[0])
那么你会得到你想要的输出。
有人可以帮我从元组列表中删除多余的括号吗?
latest_Value = {"17:00:00": 100.00}
Dict1 = {"current value": 100, "stock_purchased": "false", "Historic value": [('16:00:00', 55.50), ("15:00:00", 45.50), ("14:00:00", 75.50),("13:00:00", 65.50), ("12:00:00", 55.50)]}
# converting the latest_value into a tuple
List_it = [(k, v) for k, v in latest_Value.items()]
# insert the tuple into the tuple list
Dict1['Historic value'].insert(0, List_it)
data2 = Dict1["Historic value"]
print(data2)
#output
[[('17:00:00', 100.0)], ('16:00:00', 55.5), ('15:00:00', 45.5), ('14:00:00', 75.5), ('13:00:00', 65.5), ('12:00:00', 55.5)]
修改列表时,它会添加一个嵌套列表,而不仅仅是元组。 你如何避免这种情况?
亲切的问候,
安德鲁
# converting the latest_value into a tuple
List_it = [(k, v) for k, v in latest_Value.items()]
也就是说,其实是错误的。这会将整个字典转换为元组,而不仅仅是最后一个键值对,因此 Dict1['Historic value'].insert(0, List_it)
将整个元组列表添加到 Dict1['Historic value']
.
latest_Value
包含单个键值对的事实不会改变 List_it
将是元组列表的事实。
如果你改为Dict1['Historic value'].insert(0, List_it[0])
那么你会得到你想要的输出。