我有一个这样的列表列表
[[], [1, 2, 2], [1], [2], [2], [1, 2], [1, 2], [2, 1], [2, 2]]
Run Code Online (Sandbox Code Playgroud)
我想删除所有重复,这里的顺序并不重要,所以在上面的列表中我需要删除[2]
,[1,2]
和[2,1]
.我以为我可以这样做Counter()
from collections import Counter
counter_list = []
no_dublicates = []
for sub_list in all_subsets:
counter_dic = Counter(sub_list)
if counter_dic in counter_list:
pass
else:
no_dublicates.append(list(sub_list))
counter_list.append(counter_dic)
Run Code Online (Sandbox Code Playgroud)
哪个工作正常...但它是我的代码中最慢的部分.我想知道是否有更快的方法来做到这一点?
我想在我的不和谐机器人中实现一个功能,检查是否有任何成员离线,然后执行以下功能。我已经阅读了 API 参考页面,但不太明白如何去做,像这样的东西会起作用吗?
client = discord.Client()
@client.event
async def on_member_update(before, after):
if before == online:
if after == offline:
print("{} has gone offline.".format(member))
Run Code Online (Sandbox Code Playgroud)
我不认为代码会按预期工作,但它可能会为我的目标提供一些指导。
我正在用 python 做这些,但我想也许有一种更快的方法来做到这一点。
在处理pd.get_dummies(dataset[a column name])
序数变量后,我手动检查列数并在每个新列名称的末尾添加 1, 2, 3,..。
在 python 中,我们是否可以编写更高效的代码,以便 python 获得序数变量的虚拟值并重命名带有按顺序附加的数字的列名称?(即,如果我给出 g,它会将列重命名为 g1、g2、g3 列)
dummie_g = pd.get_dummies(d["gen"])
dummie_g.describe()
dummie_g.columns = ['g1','g2','g3']
dummie_e=pd.get_dummies(d["educ"])
dummie_e.describe()
dummie_e.columns = ['e1','e2','e3','e4']
dummie_a=pd.get_dummies(d["type"])
dummie_a.describe()
dummie_a.columns=['a1','a2','a3','a4','a5','a6']
dummie_n=pd.get_dummies(d["name"])
dummie_n.describe()
dummie_n.columns=['n1','n2']
dummie_dpt=pd.get_dummies(d["dpt"])
dummie_dpt.describe()
dummie_dpt.columns=['h1','h2','h3','h4','h5','h6','h7','h8','h9','h10','h11','h12','h13','h14','h15']
Run Code Online (Sandbox Code Playgroud) 因此,当我编写一个简单的刽子手游戏以获得乐趣时,因为初学者我仍然需要帮助:
我希望我的算法测试输入是否只有一个字符,例如:
while True:
x=str(input())
#code checking wheather it is only one character, if it is
character break and stop the loop,
otherwise repeat the input
Run Code Online (Sandbox Code Playgroud)让我说我的秘密词是'billy'
我想知道如何检查这一个字母输入是否与秘密词中的任何一个字母相同.例如
if x *code checking if the input has a same letter as the secret word*:
#carry on program
Run Code Online (Sandbox Code Playgroud)如果你可以帮助我解决这两个问题中的任何一个问题,你就可以成为救星!
这两个代码片段是完全相同的(就像它们在c ++中一样)还是会产生稍微不同的运行时间?
第一)
x = 'hello joe'
if x == 'hello':
print('nope')
elif x == 'hello joe':
print(x)
Run Code Online (Sandbox Code Playgroud)
第二)
x = 'hello jo'
if x == 'hello':
print('nope')
else:
if x == 'hello joe':
print(x)
Run Code Online (Sandbox Code Playgroud)
我想找出自己,但我不确定如何实时观看这个代码以汇编形式运行.这让我想到了第二个问题:我如何看到编译Python程序时编译的汇编指令?
如何!getuser
在不和谐频道中使用命令从特定角色获取成员列表。
@bot.command(pass_context=True)
async def getuser(ctx):
Run Code Online (Sandbox Code Playgroud)
机器人用他们的 ID 回复
1. @user1#123
2. @user2#123
Run Code Online (Sandbox Code Playgroud) 我正在为我的不和谐频道开发一个机器人,我正在学习 python,我想在用户添加反应时赋予角色我想出的代码是这样的
@client.event
async def on_reaction_add(reaction, user):
ChID = '487165969903517696'
if reaction.message != ChID:
return;
if user.reaction.emoji == "":
CSGO = discord.utils.get(user.server.roles, name="CSGO_P")
await client.add_roles(user, CSGO)
Run Code Online (Sandbox Code Playgroud)
但它不起作用我基本上想要的是有一条消息,我在带有此频道 ID 的频道中发送了一条消息:487165969903517696,然后我的机器人发送了一条带有“角色 = 表情符号”内容的嵌入消息。像 CSGO=:runner: 然后将这些反应表情符号添加到它的消息(嵌入)现在我想说如果用户点击这些表情符号之一机器人必须给他/她一个像 CSGO_P 这样的角色
顺便说一句,我一开始想出了一个 on_message 事件,它运行良好,但我认为如果他们添加反应而不是输入例如 !CSGO 会更加用户友好,而且我刚刚开始使用 python(2 天前)
我在Python中使用带有参数的3个函数:Func1(a,b), Func2(c,d), Func3(e,f)
.我想要的是如何随机调用这三个函数的两个不同函数.我有以下代码随机调用函数.我怎么能把这个代码转换为随机调用2个函数(不一样)的代码.
Mylist = [functools.partial(Func1, a, b), functools.partial(Func2, c, d), functools.partial(Func3, e, f)]
random.choice(Mylist)()
Run Code Online (Sandbox Code Playgroud) 我需要制定一个只能由具有特定角色的人执行的命令。我在 google 和 youtube 上四处寻找答案,但一无所获
堆数据结构的新手。
尝试从列表创建堆。
li = [5, 7, 9, 1, 3]
heapq.heapify(li)
Run Code Online (Sandbox Code Playgroud)
经过 heapity 后,输出为
[1, 3, 9, 7, 5]
Run Code Online (Sandbox Code Playgroud)
为什么有这个命令?
我认为对于最小优先级堆,元素应该从最小到最大排序,即
heapq.heapify(li)
应该与li.sort()
有人可以帮我理解吗?