Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
89 views
in Technique[技术] by (71.8m points)

python - check if all the values for different keys are the same

I want to return true if all the values in dict for different keys is same else false:

for example:

this should return true:

{1: [1, 1, 1], 2: [1, 1, 1], 3: [1, 1, 1]}) 

this should return false.

{1: [1, 1, 1], 2: [1, 1, 1], 3: [1, 2, 1]}) 

I tried the following but it didnt work.

        # for i in range(len(votes_grid[0])):
        #     isTie = any(v == i for v in columnTable.values())
        #also tried
              isTie = all(v == i for v in columnTable.values())
question from:https://stackoverflow.com/questions/66056590/check-if-all-the-values-for-different-keys-are-the-same

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Same as @sarartur but more efficient, because I'm not nesting .values() inside itself. That makes this O(n) rather than O(n**2)

pattern = list(columnTable.values())[0]
tie = all(value == pattern for value in columnTable.values())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...