【发布时间】:2016-02-22 00:58:10
【问题描述】:
如何对忽略特殊字符的数字列表进行排序。 例如:
[45, 10, 15, 30, "18*"] 其中“18*”为字符串,其余为整数
我希望它像这样排序:
[10, 15, "18*", 30, 45]
如果您需要,这是我的全部代码 - 它是一个数字猜谜游戏,可以在列表中跟踪您的猜测并用星号标记正确的猜测:
# 1. Generate random integer
import random
randomNumber = random.randint( 1, 100 )
# 2. Take user input
guessList = []
userGuess = int( raw_input( "Guess an integer between 1 and 100\n> " ))
guessList.append(userGuess)
if userGuess == randomNumber:
print( "Wow! First try!" )
# 4. High/low loop that terminates if user types "113" or guesses number
while userGuess != randomNumber and userGuess != 113:
if userGuess > randomNumber:
userGuess = int( raw_input( 'Your guess is too high! Guess another integer or type "113" to quit.\n> ' ))
if userGuess != randomNumber:
guessList.append(str(userGuess))
if userGuess < randomNumber:
userGuess = int( raw_input( 'Your guess is too low! Guess another integer or type "113" to quit.\n> ' ))
if userGuess != randomNumber:
guessList.append(str(userGuess))
# 5. Endgame outputs depending on if they user quit or guessed the number
if userGuess == 113:
print ( "You have quit the game. Better luck next time." )
if userGuess == randomNumber:
guessList.append( str(userGuess) + "*" )
print ( " You guessed the number! You are a magician! Your guesses were: " )
print ( guessList )
请告诉我我需要使用什么排序算法。
【问题讨论】:
标签: python list python-2.7 sorting