简体   繁体   English

如何根据 python 中的顺序比较和合并两个列表?

[英]how to compare and merge two list according to its sequence in python?

Let say i have two lists that is intended to be merged:假设我有两个要合并的列表:

a=[0.1,0.2,'-','-',0.3]
b=['-','-',0.4,0.5,'-']

how to merge a and b to its current position to be like this?如何将 a 和 b 合并到其当前的 position 成为这样?

c=[0.1,0.2,0.4,0.5,0.3]

thanks谢谢

Assuming either a or b has a float for a given position.假设 a 或 b 对于给定的 position 具有浮点数。

One possibility:一种可能:

c = [x if isinstance(x, float) else y for x,y in zip(a,b)]

output: [0.1, 0.2, 0.4, 0.5, 0.3] output: [0.1, 0.2, 0.4, 0.5, 0.3]

Using :使用

pd.Series(a).replace('-', pd.NA).fillna(pd.Series(b))

output: output:

0    0.1
1    0.2
2    0.4
3    0.5
4    0.3
dtype: float64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM