簡體   English   中英

如何測試每個特定的數字或字符

[英]How to test each specific digit or character

我希望收到用戶輸入的5位數字 ,然后打印每個特定數字的內容。

例如,如果用戶輸入12345,我想首先打印1的特定輸出,然后打印另一個輸出2,等等。

我該怎么做呢? 如果可能的話,我更願意創建一個函數。

#!/usr/bin/python3

zipcode = int(raw_input("Enter a zipcode: "))

if zipcode == 1:
       print ":::||"
elif zipcode == 2:
       print "::|:|"
elif zipcode == 3:
       print "::||:"
elif zipcode == 4:
       print ":|::|"
elif zipcode == 5:
       print ":|:|:"
elif zipcode == 6:
       print ":||::"
elif zipcode == 7:
       print "|:::|"
elif zipcode == 8:
       print "|::|:"
elif zipcode == 9:
       print "|:|::"
elif zipcode == 0:
       print "||:::"

您可以使用字典 ,然后遍歷輸入:

zipcode = raw_input("Enter a zipcode: ")

codes={1:":::||",2:"::|:|",3:"::||:",4:":|::|",5:":|:|:",6:":||::",7:"|:::|",8:"|::|:",9:"|:|::",0:"||:::"}

for num in zipcode:
    print codes[int(num)], #add a comma here if you want it on the same line

這會給你:

>>> 
Enter a zipcode: 54321
:|:|: :|::| ::||: ::|:| :::||

編輯:

沒有空格:

zipcode = raw_input("Enter a zipcode: ")

codes={1:":::||",2:"::|:|",3:"::||:",4:":|::|",5:":|:|:",6:":||::",7:"|:::|",8:"|::|:",9:"|:|::",0:"||:::"}

L = [] #create a list

for num in zipcode:
    L.append(codes[int(num)]) #append the values to a list

print ''.join(L) #join them together and then print

現在這將打印:

>>> 
Enter a zipcode: 54321
:|:|::|::|::||:::|:|:::||

一個很好的解決方案

  • 將它們存儲在tuple (而不是字典,因為所有值都按順序排列, listtuple在這種情況下比通過鍵和值訪問更好)

     list_bars = (":::||","::|:|",...) 

    通過這種方式,您不需要眾多ifelif東西

  • 不要將它轉換為int將其保留為str本身。 使用此方法,您可以迭代字符串而不是轉換的數字。

最后在一個地方獲取所有代碼,

zipcode = raw_input("Enter a zipcode: ")
list_bars = (":::||","::|:|","::||:",":|::|",":|:|:",":||::","|:::|","|::|:","|:|::","||:::")
for i in zipcode:
    print(list_bars[int(i)-1])

現在進行一個小型演示

Enter a zipcode: 123
:::||
::|:|
::||:

使用timeit模塊測試listtupledictionary之間的差異作為數據結構

bhargav@bhargav:~$ python -m timeit 'list_bars = [":::||","::|:|","::||:",":|::|",":|:|:",":||::","|:::|","|::|:","|:|::","||:::"]; [list_bars[int(i)-1] for i in "12345"]'
100000 loops, best of 3: 3.18 usec per loop
bhargav@bhargav:~$ python -m timeit 'list_bars={1:":::||",2:"::|:|",3:"::||:",4:":|::|",5:":|:|:",6:":||::",7:"|:::|",8:"|::|:",9:"|:|::",0:"||:::"}; [list_bars[int(i)] for i in "12345"]'
100000 loops, best of 3: 3.61 usec per loop
bhargav@bhargav:~$ python -m timeit 'list_bars = (":::||","::|:|","::||:",":|::|",":|:|:",":||::","|:::|","|::|:","|:|::","||:::"); [list_bars[int(i)-1] for i in "12345"]'
100000 loops, best of 3: 2.6 usec per loop

如您所見,與其他tuple相比, tuple最快的。

將郵政編碼保留為字符串,創建從輸入到輸出的映射:

def print_zip(zipcode):

    mapping = {
        '1': ':::||',
        '2': '::|:|',
        ...etc...
    }

    for char in zipcode:
        try:
            print mapping[char]
        except KeyError:
            print 'Oops, {} not valid in a zipcode!'.format(char)


zipcode = raw_input('Enter a zipcode: ')
print_zip(zipcode)

您可以為此創建字典,然后使用.get訪問其元素:

def print_for_zipcode():
    zipcode = raw_input("Enter a zipcode: ")
    relationship = {"1": ":::||",
        "2": "::|:|",
        "3": "::||:",
        "4": ":|::|",
        "5": ":|:|:",
        "6": ":||::",
        "7": "|:::|",
        "8": "|::|:",
        "9": "|:|::",
        "0": "||:::"}
    for ch in zipcode:
        print relationship.get(ch, "Not Found")

實際運行會像這樣:

>>> print_for_zipcode()
Enter a zipcode: 123412
:::||
::|:|
::||:
:|::|
:::||
::|:|

迭代然后對字符串中的每個項執行函數:

def something(zipcode):
    if zipcode == 1:
       print ":::||"
    elif zipcode == 2:
           print "::|:|"
    elif zipcode == 3:
           print "::||:"
    elif zipcode == 4:
           print ":|::|"
    elif zipcode == 5:
           print ":|:|:"
    elif zipcode == 6:
           print ":||::"
    elif zipcode == 7:
           print "|:::|"
    elif zipcode == 8:
           print "|::|:"
    elif zipcode == 9:
           print "|:|::"
    elif zipcode == 0:
           print "||:::"

for letter in raw_input():
    something(int(letter))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM