At the entrance we are given the following data:

4 A B : A C : A D : BC 4 AB BD CD DA 

The code is:

 n=int(input()) d={} for i in range(n): line=input().split(':') if len(line) == 1 : d[line[0]] = [''] else: d[line[0].strip()] = line[1].split() z = int(input()) list = [] for i in range(z): request = input().split() list.append(request[0]) 

We will have a dictionary like:

 {'C': ['A'], 'D': ['B', 'C'], 'B': ['A'], 'A': ['']} 

The list will be like this:

 ['A', 'B', 'C', 'D'] 

And the question itself: At the end of the code I want to write something like

 for j in list: if list in d.values(): print('Yes') else: print('No') 

But I’ll get "No" everywhere as output. 2 objects 'A' and A will be compared, instead of A and A (without quotes). How to turn this check?

  • I saw something similar on a steppe :) - gil9red
  • it is from there) - Eugene
  • edit the question and remove everything to the words "The dictionary we will have the form:" - this is not relevant to the question in the title. At the end, add what result you expect from the comparison (explicitly indicate моё_сравние(['A', 'B', 'C', 'D'], ['A']) == 'Yes' or моё_сравние(['A', 'B', 'C', 'D'], ['A']) == 'No' - what do you want to receive and why? - jfs

1 answer 1

Blame corrected. This code seems to work correctly.

 d = {'C': ['A'], 'D': ['B', 'C'], 'B': ['A'], 'A': ['']} list = ['A', 'B', 'C', 'D'] dlist = [val for sublist in d.values() for val in sublist] print(dlist) for j in list: print(j) if j in dlist: print('Yes') else: print('No') 
  • one
    In your example, the answer will always be "yes", although the D value from the list in the dictionary values ​​is no, and the answer should not be "yes." - Eugene