gpt4 book ai didi

python - 带 zip 的 For 循环

转载 作者:行者123 更新时间:2023-12-01 06:42:10 25 4
gpt4 key购买 nike

我正在寻求一些帮助,我有这个例子

numbers=['Sub1','Sub2','Sub3']
Value=[60,45,30]
Y = pd.Series()
for number,i in zip(numbers,Value):
Y[number]=math.cos(i)
print(Y)

我得到这个结果

Sub1   -0.952413
dtype: float64
Sub1 -0.952413
Sub2 0.525322
dtype: float64
Sub1 -0.952413
Sub2 0.525322
Sub3 0.154251
dtype: float64

但我希望得到的是

Sub1   -0.952413
Sub2 0.525322
Sub3 0.154251

-0.952413
0.525322
0.154251

因为我需要这些值来完成代码。谢谢BR初学者

最佳答案

试试这个:

list(zip(numbers,[f'"{math.cos(i):.6f}' for i in Value]))                                                                                                                           
[('Sub1', '"-0.952413'), ('Sub2', '"0.525322'), ('Sub3', '"0.154251')]

或者:

pd.DataFrame(list(zip(numbers,[f'{math.cos(i):.6f}' for i in Value])))


0 1
0 Sub1 -0.952413
1 Sub2 0.525322
2 Sub3 0.154251

关于python - 带 zip 的 For 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59400508/

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