我的输出困惑

时间:2013-12-19 15:31:39

标签: python output

目前我只是在Python中创建一个赌场。只是为了愚弄,我来的不是一个问题。但是混乱。我一直试图解决这个问题一段时间了。我的输出没有遵循任何明显的模式,但每次都是相同的。

import random
import time
import string
import os
import platform

def clear():
    operSys = platform.system()
    if operSys.lower() == "Windows":
        os.system('cls')
    else:
        os.system('clear')

cards = {"Ace of Spades":'1',
         "Ace of Clubs":'1', 
         "Ace of Diamonds":'1', 
         "Ace of Hearts":'1', 

         "Two of Spades":'2', 
         "Two of Clubs":'2', 
         "Two of Diamonds":'2',
         "Two of Hearts":'2',

         "Three of Spades":'3',
         "Three of Clubs":'3',
         "Three of Diamonds":'3',
         "Three of Hearts":'3',

         "Four of Spades":'4',
         "Four of Clubs":'4',
         "Four of Diamonds":'4',
         "Four of Hearts":'4',

         "Five of Spades":'5',
         "Five of Clubs":'5',
         "Five of Diamonds":'5',
         "Five of Hearts":'5',

         "Six of Spades":'6',
         "Six of Clubs":'6',
         "Six of Diamonds":'6',
         "Six of Hearts":'6',

         "Seven of Spades":'7',
         "Seven of Clubs":'7',
         "Seven of Diamonds":'7',
         "Seven of Hearts":'7',

         "Eight of Spades":'8',
         "Eight of Clubs":'8',
         "Eight of Diamonds":'8',
         "Eight of Hearts":'8',

         "Nine of Spades":'9',
         "Nine of Clubs":'9',
         "Nine of Diamonds":'9',
         "Nine of Hearts":'9',

         "Ten of Spades":'10',
         "Ten of Clubs":'10',
         "Ten of Diamonds":'10',
         "Ten of Hearts":'10',

         "Jack of Spades":'11',
         "Jack of Clubs":'11',
         "Jack of Diamonds":'11',
         "Jack of Hearts":'11',

         "Queen of Spades":'12',
         "Queen of Clubs":'12',
         "Queen of Diamonds":'12',
         "Queen of Diamonds":'12',

         "King of Spades":'13',
         "King of Clubs":'13',
         "King of Diamonds":'13',
         "King of Hearts":'13'}

for x in cards:
    print x + ' : ' + cards[x]

输出:

Queen of Clubs : 12
Ten of Hearts : 10
Three of Spades : 3
Nine of Spades : 9
Ace of Clubs : 1
King of Spades : 13
Ace of Spades : 1
Queen of Diamonds : 12
Three of Hearts : 3
Seven of Hearts : 7
Two of Spades : 2
Two of Hearts : 2
Five of Hearts : 5
Three of Clubs : 3
Queen of Spades : 12
Two of Clubs : 2
Four of Diamonds : 4
King of Diamonds : 13
Ace of Diamonds : 1
Five of Diamonds : 5
Four of Spades : 4
Six of Spades : 6
Ten of Spades : 10
Eight of Clubs : 8
Jack of Clubs : 11
Six of Diamonds : 6
Two of Diamonds : 2
Eight of Hearts : 8
Jack of Hearts : 11
Nine of Hearts : 9
King of Hearts : 13
Ace of Hearts : 1
Seven of Spades : 7
Six of Hearts : 6
Ten of Clubs : 10
Five of Spades : 5
Jack of Spades : 11
Eight of Spades : 8
Ten of Diamonds : 10
Four of Clubs : 4
Eight of Diamonds : 8
Jack of Diamonds : 11
Six of Clubs : 6
King of Clubs : 13
Three of Diamonds : 3
Nine of Clubs : 9
Nine of Diamonds : 9
Seven of Diamonds : 7
Four of Hearts : 4
Five of Clubs : 5
Seven of Clubs : 7

3 个答案:

答案 0 :(得分:4)

Python中的字典(以及大多数其他语言)没有隐式顺序。它们实际上是用于无序键值对,甚至不会尝试保留键值对的顺序。作为一种数据结构,我们的目标是尽可能便宜地查找价值,以牺牲其他特征(如订单)。

因此,如果您确实需要订单,可以将密钥放入有序数组中,并从字典中查找值,甚至可以使用数组来存储所有数据。

答案 1 :(得分:2)

dict顺序是任意的。试试这个:

for k, v in sorted(cards.items()):
  print(k+':'+v)

按键排序。按项目值排序需要一两步:

reversed = [(v,k) for k, v in cards.items()]
for v,k in sorted(reversed):
       print(k+':'+v)

另外顺便说一句:任何字符串的.lower()输出都不会匹配像'Windows'这样的混合大小写字符串。

答案 2 :(得分:0)

你的意思是字典是你的模式吗?如果是这样,这是错误的,因为Python中的字典是无序结构(输出可能与您的“模式”不同)。