python - 如何添加两个列表 - 一个带有正号,一个带有负号
问题描述
我有以下数据:
2
[-0.09891464 -0.09715325 -0.09410605 -0.09019411 -0.0860636 -0.08205132
-0.07875871 -0.07614547 -0.07443062 -0.07346302 -0.07298417 -0.07290273
-0.07287797 -0.07287593 -0.07287593] code_length
[-0.98949882 -0.97240346 -0.94268702 -0.90432065 -0.86363176 -0.82404481
-0.79160852 -0.76596087 -0.74920381 -0.73978155 -0.73512854 -0.73433788
-0.73409758 -0.7340778 -0.7340778 ] code_length
[-0.08209141 0.24530752 0.57179519 0.89738478 1.22269259 1.54813354
1.87437147 2.20121635 2.52864319 2.85637075 3.18420369 3.51207073
3.83993948 4.16780833 4.49567718] code_length
[0.09891464 0.09715325 0.09410605 0.09019411 0.0860636 0.08205132
0.07875871 0.07614547 0.07443062 0.07346302 0.07298417 0.07290273
0.07287797 0.07287593 0.07287593] code_length
[-0.98949882 -0.97240346 -0.94268702 -0.90432065 -0.86363176 -0.82404481
-0.79160852 -0.76596087 -0.74920381 -0.73978155 -0.73512854 -0.73433788
-0.73409758 -0.7340778 -0.7340778 ] code_length
[-0.08209141 0.24530752 0.57179519 0.89738478 1.22269259 1.54813354
1.87437147 2.20121635 2.52864319 2.85637075 3.18420369 3.51207073
3.83993948 4.16780833 4.49567718] code_length
print(len(pos_list))
print(streamline_x[0])
print(streamline_y[0])
print(streamline_z[0])
print(streamline_x[1])
print(streamline_y[1])
print(streamline_z[1])
我想绘制它们,并且我想用负 z 分量绘制它们。它提供了这两个循环:
for i in range(len(pos_list)):
ax.plot3D(streamline_x[i], streamline_y[i], streamline_z[i], color=cfg._sections['colors'].get('mag_field'), linewidth=cfg._sections['styles'].get('lines'))
for i in range(len(pos_list)):
ax.plot3D(streamline_x[i], streamline_y[i], -streamline_z[i], color=cfg._sections['colors'].get('mag_field'), linewidth=cfg._sections['styles'].get('lines'))
但是,我想简化它并创建 3 个用于绘图的列表而不是 6 个。我尝试了以下操作:
minus_streamline_z1 = []
minus_streamline_z2 = []
# Create a function that multiplies each element in list by (-1)
for x in streamline_z[0].tolist():
minus_streamline_z1.append(x * (-1))
for x in streamline_z[1].tolist():
minus_streamline_z2.append(x * (-1))
minus_streamline_z = [minus_streamline_z1, minus_streamline_z2]
# Adding symmetric parts of streamline to plot them
for i in range(len(pos_list)):
xs = streamline_x[i].tolist() + streamline_x[i].tolist()
ys = streamline_y[i].tolist() + streamline_y[i].tolist()
zs = streamline_z[i].tolist() + minus_streamline_z[i]
ax.plot3D(xs, ys, zs)
请问有什么问题吗?
前两个周期给出了这个数字:
第二种方式给出了一些奇怪的线
解决方案
问题是您当前的第一个列表从(-0.09891464, -0.98949882, -0.08209141)
z 方向开始并向上移动到(-0.07287593, -0.7340778, 4.49567718)
. 当您将两个列表连接在一起时,它会跳转到 (-0.09891464, -0.98949882, 0.08209141)
,然后在 z 方向上向下移动到(-0.07287593, -0.7340778, -4.49567718)
。
列表连接处的这种跳转导致您在第二个图中看到的直线段。
[::-1]
为了解决这个问题,您可以在将两个列表中的第一个列表连接在一起时颠倒它们的顺序(用于索引列表)。
例如:
for i in range(2):
xs = streamline_x[i].tolist()[::-1] + streamline_x[i].tolist()
ys = streamline_y[i].tolist()[::-1] + streamline_y[i].tolist()
zs = streamline_z[i].tolist()[::-1] + minus_streamline_z[i]
ax.plot3D(xs, ys, zs)
产生:
z=-0.08209141
请注意,当您从两个列表连接的地方跳回时,仍然有一个小的线性段z=0.08209141
,但是对于这个似乎并不明显的特殊情节。
推荐阅读
- docker - 在另一个容器的本地主机上发布端口
- xcode10.1 - Xcode 10.1 在故事板文件的版本编辑器中突出显示一行(或多行)
- unix - 通过 SOCKS 路由所有控制台流量
- c# - 使用 EF6 更新实体的某些属性而不加载整个实体
- encoding - CloudCDN 是否支持 Brotli 编码的服务?
- swift - 如何在 Swift 存储属性中创建协议及其扩展?
- mysql - Mariadb 加载,但几分钟后崩溃。从“快照”恢复服务器后发生
- jenkins - jenkinsfile 没有将 env 传递给 sh
- gitlab - 如何仅获取 Gitlab API 响应中的特定字段?
- python - Subprocess check_output cutting my output short