Python 列表中多个 nan 值的唯一索引

问题描述 投票:0回答:1

假设我有以下 python 列表,其中包含一些随机字符串和多个 nan 值:

l = [1, 'str', ',,,,', math.nan, 'another_str', math.nan, math.nan, 'last_str']

我想要实现的是一个字典,其中上述列表中元素的索引作为键,元素本身作为值,看起来像这样:

{0: 1,
 1: 'str',
 2: ',,,,',
 3: nan,
 4: 'another_str',
 5: nan
 6: nan
 7: 'last_str'}

以下代码:

d_l = {}
for i in l:
    idx = l.index(i)
    print(idx, i)
    d_l[idx] = i

退货

0 1
1 str
2 ,,,,
3 nan
4 another_str
3 nan
3 nan
7 last_str

所以我们看到nan值的索引已经被‘回收’了。这是非常合乎逻辑的,因为 nan 值彼此相同。

为了避免这种情况,我尝试用唯一的字符串替换 nan 值:

d_l = {}
for i in l:
    idx = l.index(i)
    # check if i is nan, replace for unique str if that's the case
    if pd.isna(i):
        print(f"{i} is nan, index is {idx}")
        i = f"{idx} to be replaced"
    else:
        pass
    d_l[idx] = i

返回:

nan is nan, index is 3
nan is nan, index is 3
nan is nan, index is 3
{0: 1, 1: 'str', 2: ',,,,', 3: '3 to be replaced', 4: 'another_str', 7: 'last_str'}

我还用

if pd.isna(i)
替换了
if el is np.nan
,但我得到了完全相同的结果。

python-3.x indexing nan
1个回答
0
投票

IIUC,您不需要知道这些项目是什么就能够创建密钥。

只需

enumerate
他们并传递给
dict
:

out = dict(enumerate(l))

输出:

{0: 1,
 1: 'str',
 2: ',,,,',
 3: nan,
 4: 'another_str',
 5: nan,
 6: nan,
 7: 'last_str'}
© www.soinside.com 2019 - 2024. All rights reserved.