【问题标题】:How to replace the items from given dictionary?如何替换给定字典中的项目?
【发布时间】:2019-02-03 20:16:48
【问题描述】:

我是python的新手。我在玩字典,想知道给定问题的解决方案

list_ = [['any', 'window', 'says', 'window'], ['dog', 'great'], ['after', 'end', 'explains', 'income', '.', '?']] 

dictionary=[('dog', 'cat'), ('window', 'any')]

def replace_matched_items(word_list, dictionary):
    int_word = []
    int_wordf = []
    for lst in word_list:
        for ind, item in enumerate(lst):
            for key,value in dictionary:
                if item in key:
                    lst[ind] = key 
                else:
                    lst[ind] = "NA"
        int_word.append(lst)
    int_wordf.append(int_word)
    return int_wordf
list_ = replace_matched_items(list_, dictionary)
print(list_ )

生成的输出是:

[[['NA', 'window', 'NA', 'window'], ['NA', 'NA'], ['NA', 'NA', 'NA', 'NA', 'NA', 'NA']]]

预期的输出是:

 [[['NA', 'window', 'NA', 'window'], ['dog', 'NA'], ['NA', 'NA', 'NA', 'NA', 'NA', 'NA']]]

我正在使用 python 3 提前致谢

【问题讨论】:

  • 名为dictionary 的变量不是字典:它是一个元组列表。

标签: python-3.x dictionary


【解决方案1】:

对 python 中的数据结构的一些快速介绍只是为了澄清您的问题。

  • list 类似于您的数组,可以通过索引访问它们并且是可变,这意味着可以更改列表中的项目。列表通常由方括号 [] 标识。 例如:
my_array = [4, 8, 16, 32]
print(my_array[0]) # returns 4
print(my_array[3]) # returns 32

my_array[2] = 0
print(my_array) # returns [4, 8, 0, 32]
  • 元组类似于列表,但主要区别在于它们是不可变,这意味着元组中的项目不能更改。仍然可以通过它们的索引访问项目。它们通常由括号 () 标识。 例如:
my_tuple = ('this', 'is', 'a', 'tuple', 'of', 'strings')
print(my_tuple[0]) # returns 'this'
my_tuple[1] = 'word' # throws a 'TypeError' as items within tuples cannot be changed.
  • 字典使用键和值对来访问、存储和更改字典中的数据。与列表类似,它们是可变的,但是,每个值都有自己的唯一键。要访问字典中的值,您必须传递字典中的键。字典通常由 花括号 {} 标识。 例如:
my_dictionary = {'John':13, 'Bob':31, 'Kelly':24, 'Ryan':17}
print(my_dictionary['Kelly']) # Returns 24

my_dictionary['Kelly'] = 56 # Assigns 56 to Kelly
print(my_dictionary['Kelly']) # Returns 56

key:value 在字典中采用这种形式,随后的每个键值对用逗号分隔。

我强烈建议阅读有关可用于 python 的数据结构的官方文档:Link Here


回答问题

从您给出的代码中,您使用了一个 tuple,您的键值对将元组封装在一个 list 中作为您的字典数据结构。

您的预期输出是您遍历整个字典的结果,并且没有处理找到字典键后会发生的情况。一旦找到键,可以通过在 if 语句中添加 break 语句来解决此问题。 break 语句将在找到键后退出 for 循环,并继续到下一个列表项。

你的函数最终看起来像:

def replace_matched_items(word_list, dictionary):
    int_word = []
    int_wordf = []
    for lst in word_list:
        for ind, item in enumerate(lst):
            for key,value in dictionary:
                if item in key:
                    lst[ind] = key
                    break
                else:
                    lst[ind] = "NA"
        int_word.append(lst)
    int_wordf.append(int_word)
    return int_wordf

建议使用字典

为您的键值对使用 Dictionary 数据结构将让您可以访问可让您检查字典中是否存在键的方法。

如果您有一个键列表,并且您想检查列表中是否存在字典键:

this_list = ['any', 'window', 'says', 'window', 'dog', 
'great', 'after', 'end', 'explains', 'income', '.', '?']

dictionary = {'dog':'cat', 'window':'any'}

matched_list = []
for keys in dictionary:
    if keys in this_list:
        matched_list.append(keys) # adds keys that are matched 
    else:
        # do something if the key is in not the dictionary

print(matched_list)
# Returns ['dog', 'window']

【讨论】:

  • 感谢 JeReq 问题出在字典中,因为在 python 3 中它返回列表中的元组列表。
猜你喜欢
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-24
相关资源
最近更新 更多