所以它是一个在线聊天机器人,我的名字叫chatango。 它是一个白名单,显示谁都可以使用该机器人。
代码是:
whitelist = []
f = open("users/"+name+".txt", "r") # read-only
print("[INFO]LOADING WHITELIST-LVL-1...")
soundz("info")
time.sleep(1)
for name in os.listdir(path):
if len(name.strip()[:-4])>0: whitelist.append(name.strip()[:-4])
f.close()
当我使用
向列表中添加一个人时 open("users/"+name+".txt", 'w')
当它重新加载时,它会在列表中产生重复
eg
Before: man1,man2,man3
[Reloaded]
After: man1,man2,man3man1,man2,man3,man4
我该怎么做
man1,man2,man3,man4 instead of man1,man2,man3man1,man2,man3,man4
重新加载后?
请帮忙
最佳答案
尝试使用一组( Python 2 | Python 3 )。集合不会保留项目的顺序,而是仅包含唯一的项目,因此您不会有重复的条目。
whitelist = set()
f = open("users/"+name+".txt", "r")
print("[INFO]LOADING WHITELIST-LVL-1...")
soundz("info")
time.sleep(1)
for name in os.listdir(path):
if len(name.strip()[:-4])>0: whitelist.add(name.strip()[:-4])
f.close()
关于Python 仅 append 唯一名称而不是重复名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27568949/