【问题标题】:Create function to find the key of the values in dictionary that matches alphabetical letters found in string (Python)创建函数以查找字典中与字符串中找到的字母匹配的值的键(Python)
【发布时间】:2020-06-26 09:10:30
【问题描述】:

我想创建一个函数来查找字典中包含的与字符串中的字母匹配的值的键。 这是我的字典:

dict = {'0': {'a','c','d','e','b','f'},
             '1': {'c','b'},
             '2': {'a','d','e','b','g'},
             '3': {'a','c','d','b','g'},
             '4': {'g','c','f','b'},
             '5': {'a','c','d','g','f'},
             '6': {'a','c','d','e','g','f'},
             '7': {'a','c','b'},
             '8': {'a','c','d','e','b','g','f'},
             '9': {'a','c','d','b','g','f'}}

这是我迄今为止尝试创建的函数:

def guess_character(display, state):
    for key, value in display.items():
        if state == "".join(list(value)[:len(state)]):
            return key
        else:
            pass

当我用这些打印语句测试我的函数时:

print(guess_character(seven_segment, 'abed'))
print(guess_character(seven_segment, 'a'))
print(guess_character(seven_segment, 'abcdefg'))

这是我的当前输出

None
None
None

但是,这是我想要的输出

{'2', '8', '0'}
{'5', '7', '6', '0', '3', '9', '2', '8'}
{'8'}

如何实现我想要的输出?

【问题讨论】:

  • 您的函数 guess_character 中没有 return 语句。
  • @Ch3steR 感谢您指出这一点!我现在已经添加了它,但它仍然打印None

标签: python python-3.x string function dictionary


【解决方案1】:

Ch3steR 的回答非常好。这个实际上是相同的,但可能更容易理解:

dict = {'0': ['a','c','d','e','b','f'],
             '1': ['c','b'],
             '2': ['a','d','e','b','g'],
             '3': ['a','c','d','b','g'],
             '4': ['g','c','f','b'],
             '5': ['a','c','d','g','f'],
             '6': ['a','c','d','e','g','f'],
             '7': ['a','c','b'],
             '8': ['a','c','d','e','b','g','f'],
             '9': ['a','c','d','b','g','f']}

def listfromstr(input1):
        c = []
        for i in input1:
                c.append(i)
        return c

def guesschar(dict1, str1):
        listofTruekeys = []
        listofstr = listfromstr(str1)
        for i in dict1:
                if all(elem in dict1[i] for elem in listofstr):
                        listofTruekeys.append(i)

        return listofTruekeys

print(guesschar(dict, 'abed'))

输出:

['0', '2', '8']

【讨论】:

  • 你好黑客!我尝试运行您的代码,它也可以工作!!谢谢你的帮助!!:)
  • 很高兴为您提供帮助! :) 如果它有帮助,请点赞!我需要那个名声。 ;)
【解决方案2】:

你可以试试。

您可以使用all来检查key对应的value中是否存在所有字符。

def guess_character(d,state):
    return {k for k,v in d.items() if all(i in v for i in state)}

my_dict =   {'0': {'a','c','d','e','b','f'},
             '1': {'c','b'},
             '2': {'a','d','e','b','g'},
             '3': {'a','c','d','b','g'},
             '4': {'g','c','f','b'},
             '5': {'a','c','d','g','f'},
             '6': {'a','c','d','e','g','f'},
             '7': {'a','c','b'},
             '8': {'a','c','d','e','b','g','f'},
             '9': {'a','c','d','b','g','f'}}

print(guess_character(my_dict, 'abed'))
# {'0', '2', '8'}
print(guess_character(my_dict, 'a'))
# {'0', '2', '3', '5', '6', '7', '8', '9'}
print(guess_character(my_dict, 'abcdefg'))
# {'8'}

【讨论】:

  • 这对切斯特有效!谢谢!但是有什么方法可以保留所需输出中显示的键顺序吗?
  • 什么命令@jackie?能详细说说{'2', '8', '0'}是怎么来的吗?
  • 对不起,切斯特,我能理解一下吗:当我运行创建的函数时,键的顺序会混乱吗?因为我得到的输出序列是 {'2', '0', '8'} 但您的注释行显示 {'0', '2', '8'} 。还是您对它进行了排序,以便我更容易按顺序查看它? :)
  • @jackie 我返回了一个set,它是一个无序的集合数据类型。因此,订单无法保证。
  • @jackie 很高兴为您提供帮助;)如果它有助于接受这个作为答案并投票。
猜你喜欢
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多