如何用float d删除列表中的括号

2024-05-23 17:36:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个float列表,我想把它放在一个容器中,得到没有括号的值

tt1= [102, 0.5, 0.591, 0.529, 10, 42, 26, 6, 8, 17, 24]

container = solution in my problem

简单的期望结果

102, 0.5, 0.591, 0.529, 10, 42, 26, 6, 8, 17, 24

我尝试了一些其他的解决方案,但它变成了字符串,这是不好的,因为我需要它在浮动例如

In [1]:','.join( str(a) for a in tt1 )
Out[1]: '102,0.5,0.591,0.529,10,42,26,6,8,17,24'

plss帮助


Tags: 字符串in列表mycontainer解决方案float容器
1条回答
网友
1楼 · 发布于 2024-05-23 17:36:03

啊,我知道你的问题了。你知道吗

this is really what i want to do. i want to add tt1 in another list but the thing is this just happen [0, [102, 0.5, 0.591, 0.529, 10, 42, 26, 6, 8, 17, 24], 1, 27, 109, 0.41100000000000003, 0.308, 0.818, 16, 48, 26, 13, 9, 9, 22

当您将一个列表添加到另一个列表时,只需将整个列表作为一个项添加到新列表中即可。假设您想将tt1中的所有值添加到一个组合列表tt2。你知道吗

tt1= [102, 0.5, 0.591, 0.529, 10, 42, 26, 6, 8, 17, 24]
tt2= ["some", "other", "list", 6.5, 102, True]

for item in tt1[::-1]: # we insert backwards to make it appear forwards
    tt2.insert(2, item)

print(tt2)

这很难解释,但是我临时反转列表([::-1])的原因是,一旦插入一个项,它实际上就变成了索引2。如果我们再次插入,前一项将变为索引3,新项将变为索引2-向后。所以我把列表倒过来,我们倒过来加,然后插入-backwards+backwards=forwards

输出:

["some", "other", 102, 0.5, 0.591, 10, 42, 26, 6, 8, 17, 24, "list", 6.5, 102, True]

只需将tt2替换为您想要添加项的任何列表。你知道吗

相关问题 更多 >