小编abc*_*ccd的帖子

算术@在python?什么

我已经在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)

这段代码中的@是什么?

python operators

3
推荐指数
1
解决办法
103
查看次数

为什么多个on_message事件不起作用?

为什么不能有多个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一个被触发。我怎样才能使这三个都正常工作?

python discord discord.py

3
推荐指数
1
解决办法
1388
查看次数

对字典进行排序并输出对应于最高值的键?

我正在尝试输出与最高值对应的宠物名称:

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

谢谢.

python dictionary python-3.x

2
推荐指数
1
解决办法
62
查看次数

Scrapy: How to output items in a specific json format

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 json scrapy

2
推荐指数
2
解决办法
3290
查看次数

Python以特定方式附加列表

我知道我实际上可以合并两个列表(在 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)

任何帮助将非常感激!

python list append

2
推荐指数
1
解决办法
192
查看次数

删除变量(如果存在)的正确方法

我尝试做:

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 variables tkinter

2
推荐指数
1
解决办法
7580
查看次数

使用正则表达式去除多行 python 文档字符串

我想使用简单的搜索和替换从文件中删除所有 python 文档字符串,以下(极其)简单的正则表达式可以完成一行文档字符串的工作:

正则101.com

""".*"""
Run Code Online (Sandbox Code Playgroud)

我如何将其扩展到多线工作?

\s尝试在许多地方添加但无济于事。

python regex

2
推荐指数
1
解决办法
1838
查看次数

邮件对象没有属性“服务器”

使用以下代码行:

@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-3.x discord.py discord.py-rewrite

2
推荐指数
1
解决办法
2268
查看次数

如何使用带空格的命令名称?

当 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)

python discord discord.py

2
推荐指数
1
解决办法
6556
查看次数

TypeError:TextIOWrapper类型的对象不可JSON序列化

如果代码能够正常工作,那么每当有人在聊天中键入某些内容时,他们都会获得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 json discord python-3.7 discord.py

2
推荐指数
2
解决办法
9805
查看次数