首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Python打印字典中的重复值

用Python打印字典中的重复值
EN

Stack Overflow用户
提问于 2022-11-11 10:50:03
回答 4查看 76关注 0票数 3

我只是想知道是否可以从字典中打印重复的值。

举个例子,我有一个二分法:

代码语言:javascript
复制
responses={
    'greet':'Hello! How can I help you?',
    'types':'Our coffee types are: light roasted, medium roasted, medium dark roasted, dark roasted.',
    'light':'Coffee Bros Paraideli Cup Of Excellence, Lifeboost Coffee, Driftaway Coffee Colombia Antioquia And Burundi Kayanza, Peets Coffee Costa Rica Aurora, Fresh Roasted Coffee Ethiopian Sidamo Guji Coffee.',
    'medium':'Volcanica Coffee Kenya AA, Coffee Bros Decaf, Purity Coffee Flow, Out Of The Grey Costa Rica La Minita, Kicking Horse Three Sisters.',
    'dark':'Koa Coffee Estate, Atlas Coffee Club, Lifeboost Coffee Organic Dark Roast, Peets House Blend, Coffee Bros Dark Roast, Death Wish Coffee, Kicking Horse Coffee Grizzly Claw.',
    'fruit':'Frutis notes coffee: Coffee Bros Paraideli Cup Of Excellence, Peets Coffee Costa Rica Aurora, Fresh Roasted Coffee Ethiopian Sidamo Guji Coffee, Volcanica Coffee Kenya AA, Peets House Blend.',
    'vanilla':'Vanilla notes coffee: Lifeboost Coffee, Driftaway Coffee Colombia Antioquia And Burundi Kayanza.',
    'chocolate':'Chocolate notes coffee: Coffee Bros Decaf, Purity Coffee Flow, Out Of The Grey Costa Rica La Minita, Lifeboost Coffee Organic Dark Roast, Coffee Bros Dark Roast, Death Wish Coffee, Kicking Horse Coffee Grizzly Claw.',
    'timings':'We are open from 9AM to 5PM, Monday to Friday. We are closed on weekends and public holidays.',
    'fallback':'I dont quite understand. Could you repeat that?',
}

所以,如果我选择两个不同的键,比如:

代码语言:javascript
复制
'chocolate':'Chocolate notes coffee: Coffee Bros Decaf, Purity Coffee Flow, Out Of The Grey Costa Rica La Minita, Lifeboost Coffee Organic Dark Roast, Coffee Bros Dark Roast, Death Wish Coffee, Kicking Horse Coffee Grizzly Claw.',
'medium':'Volcanica Coffee Kenya AA, Coffee Bros Decaf, Purity Coffee Flow, Out Of The Grey Costa Rica La Minita, Kicking Horse Three Sisters.',

他们有一个共同的咖啡名字,就像:

纯咖啡流,出灰哥斯达黎加La Minita

所以,如果我为它插入键,比如:巧克力medium

程序只需要打印这两个副本:

纯咖啡流,出灰哥斯达黎加La Minita

是否可以在控制台中打印出这两个单词,其中的两个字是复制的?

只有当值完全相同时,我才能打印重复的值,但这不是我的用例。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2022-11-11 11:06:20

编辑:在OP做了一些澄清之后,这些键需要从控制台输入,所以我也将保留旧的答案,添加一种从用户输入中获取键的方法:

代码语言:javascript
复制
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("key1",)
parser.add_argument("key2")

command = input() #Input something like "chocolate medium"
args = parser.parse_args(command.split(" "))


#If user has input only one key, print values from that key
if args.key2 == None:
    print(responses[args.key1])
else
    # Then use the find common
    print(find_common_values(responses,args.key1,args.key2))

原来的答案:

这段代码首先通过itertool.product (两个列表的笛卡儿乘积)计算出你在dict中拥有的所有键对。

然后使用一个函数在两个列表中查找公共元素。这个想法如下

代码语言:javascript
复制
    common = [ k for k in list1 if k in list2]

但是,我们可以使用list1list2而不是dict[key]中的值。我注意到它们是字符串,所以我使用split方法将逗号分隔开来。

代码语言:javascript
复制
from itertools import product

responses={
    'greet':'Hello! How can I help you?',
    'types':'Our coffee types are: light roasted, medium roasted, medium dark roasted, dark roasted.',
    'light':'Coffee Bros Paraideli Cup Of Excellence, Lifeboost Coffee, Driftaway Coffee Colombia Antioquia And Burundi Kayanza, Peets Coffee Costa Rica Aurora, Fresh Roasted Coffee Ethiopian Sidamo Guji Coffee.',
    'medium':'Volcanica Coffee Kenya AA, Coffee Bros Decaf, Purity Coffee Flow, Out Of The Grey Costa Rica La Minita, Kicking Horse Three Sisters.',
    'dark':'Koa Coffee Estate, Atlas Coffee Club, Lifeboost Coffee Organic Dark Roast, Peets House Blend, Coffee Bros Dark Roast, Death Wish Coffee, Kicking Horse Coffee Grizzly Claw.',
    'fruit':'Frutis notes coffee: Coffee Bros Paraideli Cup Of Excellence, Peets Coffee Costa Rica Aurora, Fresh Roasted Coffee Ethiopian Sidamo Guji Coffee, Volcanica Coffee Kenya AA, Peets House Blend.',
    'vanilla':'Vanilla notes coffee: Lifeboost Coffee, Driftaway Coffee Colombia Antioquia And Burundi Kayanza.',
    'chocolate':'Chocolate notes coffee: Coffee Bros Decaf, Purity Coffee Flow, Out Of The Grey Costa Rica La Minita, Lifeboost Coffee Organic Dark Roast, Coffee Bros Dark Roast, Death Wish Coffee, Kicking Horse Coffee Grizzly Claw.',
    'timings':'We are open from 9AM to 5PM, Monday to Friday. We are closed on weekends and public holidays.',
    'fallback':'I dont quite understand. Could you repeat that?',
}

def find_common_values(d,key1,key2):
    common = [ k for k in d[key1].split(",") if k in d[key2].split(",")]
    return common

# get all pairs of keys
keys = responses.keys()
pairs = list(product(keys,keys))

for P in pairs: 
    if P[0] != P[1]:
        comm = find_common_values(responses, P[0] , P[1] )
        if len(comm) != 0:
            print( P , comm ) 

这意味着:

代码语言:javascript
复制
('light', 'fruit') [' Peets Coffee Costa Rica Aurora']
('medium', 'chocolate') [' Purity Coffee Flow', ' Out Of The Grey Costa Rica La Minita']
('dark', 'chocolate') [' Lifeboost Coffee Organic Dark Roast', ' Coffee Bros Dark Roast', ' Death Wish Coffee', ' Kicking Horse Coffee Grizzly Claw.']
('fruit', 'light') [' Peets Coffee Costa Rica Aurora']
('chocolate', 'medium') [' Purity Coffee Flow', ' Out Of The Grey Costa Rica La Minita']
('chocolate', 'dark') [' Lifeboost Coffee Organic Dark Roast', ' Coffee Bros Dark Roast', ' Death Wish Coffee', ' Kicking Horse Coffee Grizzly Claw.']
票数 3
EN

Stack Overflow用户

发布于 2022-11-11 11:07:47

代码语言:javascript
复制
a = 'Chocolate notes coffee: Coffee Bros Decaf, Purity Coffee Flow, Out Of The Grey Costa Rica La Minita, Lifeboost Coffee Organic Dark Roast, Coffee Bros Dark Roast, Death Wish Coffee, Kicking Horse Coffee Grizzly Claw.'

b = 'Volcanica Coffee Kenya AA, Coffee Bros Decaf, Purity Coffee Flow, Out Of The Grey Costa Rica La Minita, Kicking Horse Three Sisters.'

a_list = a.split(",")
b_list = b.split(",")

a_set = set(a_list)
b_set = set(b_list)


print(a_set.intersection(b_set))

这个怎么样?

票数 3
EN

Stack Overflow用户

发布于 2022-11-11 11:07:12

通过标点符号将每个切分值分割成部分句子是可能的。在此之后,您可以遍历dict并检查是否已经看到了部分。

代码语言:javascript
复制
import re
pattern = r'[\w\s]+'
partials  = set()
for value in responses.values():
    for partial in re.findall(pattern,value):
        if partial in partials:
            print(partial, partials)
        else:
            partials.add(partial)

输出

代码语言:javascript
复制
 Peets Coffee Costa Rica Aurora
 Fresh Roasted Coffee Ethiopian Sidamo Guji Coffee
 Peets House Blend
 Lifeboost Coffee
 Driftaway Coffee Colombia Antioquia And Burundi Kayanza
 Coffee Bros Decaf
 Purity Coffee Flow
 Out Of The Grey Costa Rica La Minita
 Lifeboost Coffee Organic Dark Roast
 Coffee Bros Dark Roast
 Death Wish Coffee
 Kicking Horse Coffee Grizzly Claw
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74401399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档