【问题标题】:Python dictionary: How to assign a default value to every Null entry foundPython字典:如何为找到的每个Null条目分配默认值
【发布时间】:2022-10-14 09:22:54
【问题描述】:

我在下面有一个列表,我正在通过 for 循环进行迭代

ner_issue_lst = [
    'device no power',
    'speaker not work',
    'charge port not work laptop plug charger',
    '',
    'keyboard not functional',
    '',
    ' ',
    'system not charge'
]

我想在遇到 null 或空的地方分配一个默认值。我当前的代码如下所示。 if 条件对我来说工作正常。在 else 条件下,每当我遇到 null 时,我应该能够为字典中每次出现的 null 值分配一个默认值,例如 ('Empty Item | Null', 5.9999)。它是我正在努力解决的语法。

res_final = {}
default_val = ('Empty Item | Null', 5.9999)
for index_val, row_val in enumerate(ner_issue_lst):
    if row_val and row_val.strip():
        print('String is neither empty nor blank')
        embed_query_val = model.encode([row_val])
        distance, faiss_value = index.search(embed_query_val, k)
        res_final[row_val] = [labels_lst[index_val] for index_val in faiss_value[0]][
        0
    ], distance.item()
    else:
        print('String is either None or Empty or contain spaces only')
        res_final[row_val] = default_val # This is not working # # correct syntax required here #

values_lst = list(res_final.values())
faiss_label_lst = [list(tup) for tup in values_lst]
if len(ner_issue_lst) != len(faiss_label_lst):
    print("Input issue list and Output label list must have the same length")

在 if 语句中,下面的代码生成这样的输出

res_final[row_val] = [labels_lst[index_val] for index_val in faiss_value[0]][
        0
    ], distance.item()
{'device no power': ('Power | Battery | Does Not Work', 0.5679274201393127),
 'speaker not work': ('Sound | Audio Sound | External Speakers Not Working',
  0.6274498105049133),
 'charge port not work laptop plug charger': ('Port | Charge Faulty',
  0.5269668698310852), 'keyboard not functional': ('Keyboard | Functional | Not Detected',
  0.3686383068561554),'system not charge': ('Power | Battery | Does Not Charge',
  0.40705418586730957) }

因此,对于每个进入 else 条件的 null 或空白条目,我如何向字典添加值

else:
    res_final[row_val] = ??????
{'': ('Empty Item | Null', 5.9999),'': ('Empty Item | Null', 5.9999),
' ': ('Empty Item | Null', 5.9999)}

稍后,通过执行从字典中获取所有值

values_lst = list(res_final.values())

所需的输出应如下所示

faiss_label_lst = [['Power | Battery | Does Not Work', 0.5679274201393127],
 ['Sound | Audio Sound | External Speakers Not Working', 0.6274498105049133],
 ['Port | Charge Faulty', 0.5269668698310852],
 ['Empty Item | Null', 5.9999],
 ['Keyboard | Functional | Not Detected', 0.3686383068561554],
 ['Empty Item | Null', 5.9999],
 ['Empty Item | Null', 5.9999],
 ['Power | Battery | Does Not Charge', 0.40705418586730957]]

终于解决了我的问题,如果有任何改进,请随时发表评论。删除了初始代码中的字典使用列表

res_final = []
default_val = ['Empty Item | Null', 5.9999]
for index_val, row_val in enumerate(ner_issue_lst):
    if row_val and row_val.strip():
        print('String is neither empty nor blank')
        embed_query_val = model.encode([row_val])
        distance, faiss_value = index.search(embed_query_val, k)
        res_final.append([[labels_lst[index_val] for index_val in faiss_value[0]][
        0
    ], distance.item()])
    else:
        print('String is either None or Empty or contain spaces only')
        res_final.append(default_val)

【问题讨论】:

  • 我不清楚字典在哪里输入 - 你有一个字符串列表,你想用默认值替换空字符串,但是你如何从那里得到字典?键应该是什么,值应该是什么?你能举一个你想要的输出的例子,或者提供足够的代码来实际运行(例如labels_lst是什么)?所需的输出是字典还是您只想要字符串列表(已交换默认值)并且您正在尝试使用字典作为中间步骤?
  • 你遇到了什么问题?这是一个错误吗?行为与预期有何不同?此外,当列表中有多个 '' 或重复字符串时,您只会在字典中保留它的一个实例。
  • @MYousefi 我不想要一个实例,而是想为遇到的每个 '' 或 ' ' 分配一个默认值。这个想法是为每个空值事件分配一个默认值,而不是它的单个实例。希望它现在澄清
  • @Samwise 所需的输出应该看起来像(更易读的编辑可以在原始问题中找到 ``` [['Power | Battery | Does Not Work', 0.5679274201393127], ['Sound | Audio Sound | External Speakers Not Working' ,0.6274498105049133],['端口|充电故障',0.5269668698310852],['空项目|空',5.9999],['键盘|功能|未检测到',0.3686383068561554],['空项目|空',5.9999] ['空项目 | Null',5.9999],['电源 | 电池 | 不充电',0.40705418586730957]] ```
  • 在这里使用字典作为中介是不好的。当您分配字典中已经存在的键时,它将覆盖它的价值。

标签: python python-3.x dictionary


【解决方案1】:

这应该只是构建一个列表而不是字典。

def get_value(row_val):
    if row_val and row_val.strip():
            print('String is neither empty nor blank')
            embed_query_val = model.encode([row_val])
            distance, faiss_value = index.search(embed_query_val, k)
            return [labels_lst[index_val] for index_val in faiss_value[0]][0], distance.item()
        else:
            print('String is either None or Empty or contain spaces only')
            return default_val

faiss_label_lst = [get_value(v) for v in ner_issue_lst]
# faiss_label_lst = list(map(get_value, ner_issue_lst))

【讨论】:

    猜你喜欢
    • 2014-10-10
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 2021-07-27
    相关资源
    最近更新 更多