小编Ana*_*mar的帖子

类实例不可迭代

在我的功能中,我有:

        """
        Iterates 300 times as attempts, each having an inner-loop
        to calculate the z of a neighboring point and returns the optimal                 
        """

        pointList = []
        max_p = None

        for attempts in range(300):

            neighborList = ( (x - d, y), (x + d, y), (x, y - d), (x, y + d) )

            for neighbor in neighborList:
                z = evaluate( neighbor[0], neighbor[1] )
                point = None
                point = Point3D( neighbor[0], neighbor[1], z)
                pointList += point
            max_p = maxPoint( pointList …
Run Code Online (Sandbox Code Playgroud)

python iterable class hill-climbing

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

关于python中dir和import的问题

我对Python的dir函数有一些疑问

>>>import urllib
>>>dir(urllib)
['__builtins__', '__cached__', '__doc__', '__file__','__loader__','__name__','__package__', '__path__', '__spec__']
Run Code Online (Sandbox Code Playgroud)

当我这样做的时候

>>>import urllib.request
>>>dir(urllib)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__','__package__', '__path__', '__spec__', 'error', 'parse', 'request', 'response']
Run Code Online (Sandbox Code Playgroud)

为什么又多了三个属性呢?为什么 request 属性一开始不在 dir(urllib) 中?

我真的很感谢你的帮助!

python python-import python-3.x

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

JavaScript:Internet Explorer中的document.getElementById()出错

我目前在JavaScript中遇到错误.代码如下

function loader() {
    size = window.innerWidth;
    size = size - 300;
    mainj = document.getElementById('main1');
    mainj.style.left = size.toString() + 'px';
    submainj = document.getElementById('submain1');
    submainj.style.left = size.toString() + 'px';
    size = mainj.style.top + 26;
    document.getElementById('submain1').style.top = size.toString() + 'px';
}
onload = loader();
Run Code Online (Sandbox Code Playgroud)

该错误仅在Internet Explorer中出现,并且代码在Firefox中完美运行.错误显示在第五行,错误是

Message: Object required
Line: 34
Char: 1
Code: 0
URI: http://localhost/home.php
Run Code Online (Sandbox Code Playgroud)

第34行是代码中给出的第5行 - 'mainj.style.left .....'如何解决这个问题?

javascript internet-explorer

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

类型错误:get() 需要 1 个位置参数,但给出了 3 个

此代码将运行一个基本数据输入窗口,通过tkinter该窗口将保存用户的输入。当我运行代码并用鼠标单击“提交”按钮时,出现错误:

print ("Información Adicional: {}".format(self.entry_name.get(1.0, "end")))

TypeError: get() takes 1 positional argument but 3 were given
Run Code Online (Sandbox Code Playgroud)

这是代码:

import tkinter
from tkinter import *
from tkinter import ttk
from tkinter import messagebox

class ImprintPlusApp:

    def __init__(self, master):
        self.frame_header = ttk.Frame(master)
        self.frame_header.pack()

        ttk.Label(self.frame_header, text = "Bienvenidos a Imprint Plus Manager")

        self.frame_crearorden = ttk.Frame(master)
        self.frame_crearorden.pack()

        ttk.Label(self.frame_crearorden, text = "Nombre").grid(row = 0, column = 0, padx = 5)
        ttk.Label(self.frame_crearorden, text = "Email").grid(row = 2, column = 0, padx = 5)
        ttk.Label(self.frame_crearorden, text …
Run Code Online (Sandbox Code Playgroud)

python tkinter

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

向dict添加列表时输出错误

我正在尝试将列表添加l为不同dict d键的值.对于数组a,[6,12,18,24,30]我试图让dict d包含以下键值对:

d[6] = [0, 0, 0.....0]
d[12] = [6, 0, 0, ..0]
d[18] = [6, 12, 0, ...0]
d[24] = [6, 12, 18, 0, ..0]
Run Code Online (Sandbox Code Playgroud)

上面每个列表中有59个元素.

我使用下面的代码执行此操作,但我的密钥输出24是:

{24: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
Run Code Online (Sandbox Code Playgroud)

python numpy

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

0-23小时军用时钟到标准时间(hh:mm)

我该如何处理这个过程?假设有人输入"3:5",我应该能够得到输出"3:05 AM".或者,如果有人输入"00:00",我应该得到"12:00 AM".如果时间格式只是输入为"hhmm",我会意识到过多的方法,但输入的范围可以是"h:m","hh:m","hh:m"和"hh:毫米".所以它非常具有情境性.

def timeConvert():
    miliTime = int(input("Enter a time in hh:mm (military) format: "))
    miliTime.split(":")
    if len(miliTime) == 3:
        hours = miliTime[0]
        minutes = miliTime[2]
        if hours < 0:
            print("Hours can't be less than 0.")
    elif len(miliTime) == 4:
        if miliTime[0:2] >= 10:
            hours = militime[0:2]
            minutes = militime[3]
            if minutes < 0:
                print("Minutes can't be less than 0.")
            if minutes < 10:
                minutes = 0 + minutes
        else:
            hours = miliTime[0]
            minutes = militime[2:]
            if minutes >= 60: …
Run Code Online (Sandbox Code Playgroud)

python time datetime python-3.x

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

TypeError:'datetime.datetime'对象不可迭代

我正在编写一个python脚本,它可以访问API,然后将各种数据写入MySQL数据库.

我正在尝试编写的一个字段是日期对象,但我一直遇到"TypeError:'datetime.datetime'对象不可迭代"错误,似乎无法通过它.我确信这很简单,但我无法弄清楚是什么.

首先,我调用API来获取一个json对象,其中包含几个日期和指标.然后我遍历日期,并尝试将它们写入数据库.

for day in results:

   mydate = datetime.datetime.strptime(day['date'], '%Y-%m-%d')

   print mydate

   date_query = "INSERT INTO sendgrid_stats (date) VALUES (%s)"

   cursor.execute(date_query, mydate)
   connection.commit()
Run Code Online (Sandbox Code Playgroud)

如果您打印试图插入的日期,它看起来像"2015-07-26 00:00:00"(没有引号).

有谁知道我做错了什么?把我的头撞到键盘上,我的脸很疼.

python mysql datetime

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

Splinter:ImportError:无法导入名称Browser

我是Splinter的新手,但我几次使用python.所以我希望使用splinter自动化网站.但是当我执行它时,我得到"ImportError:无法导入名称浏览器"错误.

这是我的代码.

from splinter import Browser

browser = Browser()
browser.visit('http://google.com')
browser.fill('q', 'splinter - python acceptance testing for web applications')
browser.find_by_name('btnG').click()

if browser.is_text_present('splinter.readthedocs.org'):
    print "Yes, the official website was found!"
else:
    print "No, it wasn't found... We need to improve our SEO techniques"

browser.quit()
Run Code Online (Sandbox Code Playgroud)

在终端这是我得到的.

Traceback (most recent call last):
  File "splinter.py", line 3, in <module>
    from splinter import Browser
  File "/var/www/project/splinter.py", line 3, in <module>
    from splinter import Browser
ImportError: cannot import name Browser
Run Code Online (Sandbox Code Playgroud)

如何在没有任何错误的情况下运行此程序?我已经提到类似问题的解决方案,如删除splinter.pyc,但它没有帮助我.

python selenium automation python-import splinter

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

python:在包中找不到模块

我正在创建一个具有以下目录结构的python项目:

demos
   demo1.py
kernels
   rbf.py
   ___init__.py
Run Code Online (Sandbox Code Playgroud)

该文件rbf.py定义了一个名为的类RBF.将__init__.py在文件kernels目录中包含以下行:

from .rbf import *
Run Code Online (Sandbox Code Playgroud)

现在demo1.py包含以下导入:

from kernels import RBF
Run Code Online (Sandbox Code Playgroud)

然而,这失败了

ImportError: No module named kernels
Run Code Online (Sandbox Code Playgroud)

因此该demos文件夹看不到kernels包目录.我不知道如何做到这一点.

python python-import

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

如何仅基于外部索引转置多级熊猫数据帧

下面是我的数据框架,具有两级索引。我希望“仅”将外部索引转置为列。我想要的输出将是2X2数据帧,而不是现在的4X1数据帧。你们任何人能帮忙吗?

        0
0    0  232

     1  3453

1    0  443

     1  3241
Run Code Online (Sandbox Code Playgroud)

python transpose hierarchical-data dataframe pandas

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