我已经在python中编程了多年,但现在我正在阅读一个程序来进行linnear回归,我发现了这个.
if X.ndim == 1:
X = X[:, None]
d = X - self.mean
precision = np.linalg.inv(self.var)
return (
np.exp(-0.5 * np.sum(d @ precision * d, axis=-1))
* np.sqrt(np.linalg.det(precision))
/ np.power(2 * np.pi, 0.5 * self.ndim))
Run Code Online (Sandbox Code Playgroud)
这段代码中的@是什么?
为什么不能有多个on_message活动?
import discord
client = discord.Client()
@client.event
async def on_ready():
print('in on_ready')
@client.event
async def on_message(message):
print("in on_message #1")
@client.event
async def on_message(message):
print("in on_message #2")
@client.event
async def on_message(message):
print("in on_message #3")
client.run("TOKEN")
Run Code Online (Sandbox Code Playgroud)
例如,如果我输入不一致的内容,则总是只有最后on_message一个被触发。我怎样才能使这三个都正常工作?
我正在尝试输出与最高值对应的宠物名称:
import operator
pets = {"Dog":3, "Cat":5, "Rabbit":0}
sorted_pets = sorted(pets.items(), key=operator.itemgetter(1), reverse = True)
print (sorted_pets[0])
Run Code Online (Sandbox Code Playgroud)
上面的代码输出['Cat', 5].如何更改它以便输出Cat?
谢谢.
I output the scraped data in json format. Default scrapy exporter outputs list of dict in json format. Item type looks like:
[{"Product Name":"Product1", "Categories":["Clothing","Top"], "Price":"20.5", "Currency":"USD"},
{"Product Name":"Product2", "Categories":["Clothing","Top"], "Price":"21.5", "Currency":"USD"},
{"Product Name":"Product3", "Categories":["Clothing","Top"], "Price":"22.5", "Currency":"USD"},
{"Product Name":"Product4", "Categories":["Clothing","Top"], "Price":"23.5", "Currency":"USD"}, ...]
Run Code Online (Sandbox Code Playgroud)
But I want to export the data in a specific format like this:
{
"Shop Name":"Shop 1",
"Location":"XXXXXXXXX",
"Contact":"XXXX-XXXXX",
"Products":
[{"Product Name":"Product1", "Categories":["Clothing","Top"], "Price":"20.5", "Currency":"USD"},
{"Product Name":"Product2", "Categories":["Clothing","Top"], "Price":"21.5", "Currency":"USD"},
{"Product Name":"Product3", "Categories":["Clothing","Top"], "Price":"22.5", "Currency":"USD"},
{"Product Name":"Product4", …Run Code Online (Sandbox Code Playgroud) 我知道我实际上可以合并两个列表(在 Python 2.7 中)如下
list1 = ['one', 'two', 'three', 'four', 'five']
list2 = ['A', 'B', 'C', 'D', 'E']
merged = list1 + list2
print merged
# ['one', 'two', 'three', 'four', 'five', 'A', 'B', 'C', 'D', 'E']
Run Code Online (Sandbox Code Playgroud)
问题是,我希望在 list1 的每两个之后插入一个 list2。例子:
list1 = ['one', 'two', 'three', 'four', 'five']
list2 = ['A', 'B', 'C', 'D', 'E']
after 2 of list1:
add 1 of list2
print merged
# ['one', 'two', 'A', 'three', 'four', 'B', 'five', 'six', 'C', 'seven', 'eight', 'D', 'nine', 'ten']
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激!
我尝试做:
def create_l():
if 'l' in globals():
l.destroy()
l = Listbox(root)
Run Code Online (Sandbox Code Playgroud)
这工作正常,但它返回一个语法警告:
Warning (from warnings module):
File "C:\Users\User\Desktop\l.py", line 4
l = Listbox(root)
SyntaxWarning: name 'l' is used prior to global declaration
Run Code Online (Sandbox Code Playgroud)
我只是想知道是否有办法在没有语法警告的情况下做到这一点。
我想使用简单的搜索和替换从文件中删除所有 python 文档字符串,以下(极其)简单的正则表达式可以完成一行文档字符串的工作:
""".*"""
Run Code Online (Sandbox Code Playgroud)
我如何将其扩展到多线工作?
\s尝试在许多地方添加但无济于事。
使用以下代码行:
@bot.command()
async def report(ctx):
author = ctx.message.author
server = ctx.message.server
wait ctx.send("Author: " + str(author) + "\nServer: " + str(server))
Run Code Online (Sandbox Code Playgroud)
我得到错误:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Message' object has no attribute 'server'
Run Code Online (Sandbox Code Playgroud)
导入库不是问题,因为我可以将行更改为ctx.message.channel并获取频道名称。在author太收集罚款。但server事实并非如此。我也尝试Guild过根据一些建议没有运气。
当 python bot 中的命令之间有空格时,如何使 bot 工作。我知道我们可以使用子命令来做到这一点,或者on_message有没有其他选项可以只针对选定的命令而不是所有命令来做到这一点。
以下代码将不起作用。
@bot.command(pass_context=True)
async def mobile phones(ctx):
msg = "Pong. {0.author.mention}".format(ctx.message)
await bot.say(msg)
Run Code Online (Sandbox Code Playgroud)
所以我尝试使用别名,但仍然无法正常工作。
@bot.command(pass_context=True, aliases=['mobile phones'])
async def phones(ctx):
msg = "Pong. {0.author.mention}".format(ctx.message)
await bot.say(msg)
Run Code Online (Sandbox Code Playgroud) 如果代码能够正常工作,那么每当有人在聊天中键入某些内容时,他们都会获得5点经验,并将这些信息保存到.json文件中,但是,当有人在聊天中键入某些内容时,会发生此错误。
on_message users = json.dumps(f)
TypeError: Object of type TextIOWrapper is not JSON serializable
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的代码:
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
import json
from json import dumps, loads, JSONEncoder, JSONDecoder
import os
client = commands.Bot(command_prefix='^')
os.chdir(r'C:\Users\quiny\Desktop\sauce')
@client.event
async def on_ready():
print ("Ready when you are xd")
print ("I am running on " + client.user.name)
print ("With the ID: " + client.user.id)
@client.event
async def on_member_join(member):
with open('users.json', 'r') as f:
users = …Run Code Online (Sandbox Code Playgroud) python ×9
discord.py ×4
discord ×3
json ×2
python-3.x ×2
append ×1
dictionary ×1
list ×1
operators ×1
python-3.7 ×1
regex ×1
scrapy ×1
tkinter ×1
variables ×1