我正在尝试更换列表中的项目,而不是最后的项目,只是原始项目的位置,然后在要求您输入密码时需要新项目。我是Python的新手。
location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
答案 0 :(得分:0)
要替换列表中的项目,只需按列表位置分配:
mylist = ['hello', 'goodbye', 'goodnight']
mylist[2] = 'good day'
答案 1 :(得分:0)
如果你知道列表中元素的价值(让我们说你要改变F Block的密码,'mrjoefblock'
),你可以使用{{ 3}}函数返回索引,就像那样:
In [1]: passwords = ['mrjoebblock' , 'mrjoefblock' , 'mrjoegblock', 'mrjoeadmin' ]
In [2]: passwords.index('mrjoefblock')
Out[2]: 1
In [3]: passwords[passwords.index('mrjoefblock')] = 'newjoefblock'
In [4]: passwords
Out[4]: ['mrjoebblock', 'newjoefblock', 'mrjoegblock', 'mrjoeadmin']
通过这种方式,您可以更新密码并保持相同的位置(如果您要问的话)。
但是我改变这样的密码似乎很奇怪。我不是把它作为一个列表,而是改为字典:
In [1]: credentials = {
...: 'B Block': 'mrjoebblock',
...: 'F Block': 'mrjoefblock',
...: 'G Block': 'mrjoegblock',
...: 'Admin': 'mrjoeadmin'
...: }
我们现在可以找到具有给定密码的所有用户:
In [2]: [user for user, password in credentials.iteritems() if password == 'mrjoeadmin']
Out[2]: ['Admin']
并且可以轻松更改任何用户的密码:
In [3]: credentials['F Block'] = 'newjoefblock'
In [4]: credentials
Out[4]:
{'Admin': 'mrjoeadmin',
'B Block': 'mrjoebblock',
'F Block': 'newjoefblock',
'G Block': 'mrjoegblock'}
对不起,最初还不清楚你在问什么。
我提出了这个简单的例子:
def login():
"""
This function asks for the password and returns either the username (if logged in successfully)
or False (if the password was incorrect).
"""
password = raw_input("Password: ")
if password == 'admin':
return 'Administrator'
elif password == 'ggguest':
return 'Guest'
else:
return False
if __name__ == '__main__':
# We'll introduce a new variable which will be either a logged in user's name
# or False (based on login()'s function result)
user_logged_in = login()
while True: # An endless loop
if user_logged_in:
# the login() function didn't return False, so we're logged in!
print "Yay! You're now logged in as {0}!".format(user_logged_in)
# Here we'll do whatever you need to.
# I kept the example simple so it just asks if the user wants to log out.
# You can change the user's password here and ask for the password again.
want_to_logout = raw_input("Want to log out? [y/n]: ")
if want_to_logout == 'y':
user_logged_in = login() # Ask for the password again
else:
print 'Alright then! Do your thing!'
break # Break out of loop and continue
else:
# wrong password, ask again
print "Wrong password :("
user_logged_in = login() # Ask for the password again
print "Keep up the good job, {0}!".format(user_logged_in)
以及它的工作方式:
$ python admin.py
Password: wrong_password
Wrong password :(
Password: admin
Yay! You're now logged in as Administrator!
Want to log out? [y/n]: y
Password: ggguest
Yay! You're now logged in as Guest!
Want to log out? [y/n]: n
Alright then! Do your thing!
Keep up the good job, Guest!
这样,您可以在需要时继续询问用户密码,否则继续。