在Python中,文档__class__被描述为属性.在对象type(元类)中,__class__似乎是一种方法.
如果我们这样做:
>>> class Foo:
pass
>>> a = Foo()
>>> a.__class__ == type.__class__(a)
True
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是:
a.__class__,我们真的在调用这个方法type.__class__(a)吗?__class__是不是__dict__属性成员的原因a吗?我正在尝试运行我的代码,但收到此错误:
File "C:\Users\JOSHUA\Documents\ypgforum\myproject\boards\models.py", line 13, in <module>
class Topic(models.Model):
File "C:\Users\JOSHUA\Documents\ypgforum\myproject\boards\models.py", line 16, in Topic
board = models.ForeignKey(Board, related_name='topics')
TypeError: __init__() missing 1 required positional argument: 'on_delete'
Run Code Online (Sandbox Code Playgroud)
这是它指向的 models.py:
class Topic(models.Model):
subject = models.CharField(max_length=255)
last_updated = models.DateTimeField(auto_now_add=True)
board = models.ForeignKey(Board, related_name='topics')
starter = models.ForeignKey(User, related_name='topics')
last_updated = models.DateTimeField(auto_now_add=True)
Run Code Online (Sandbox Code Playgroud) 我有一个创建建筑的小班.我想更多地了解国际热核实验堆.以下作品:
class Building(object):
def __init__(self, floors):
self._floors = [None] * floors
def __setitem__(self, floor_number, data):
self._floors[floor_number] = data
def __getitem__(self, floor_number):
return self._floors[floor_number]
def __iter__(self):
for floor_number in self._floors:
yield self._floors[floor_number]
building1 = Building(9) # Construct a building with 9 floors
building1[0] = 'Reception'
building1[1] = 'ABC Corp'
building1[2] = 'DEF Inc'
building1[3] = 'Apple'
building1[4] = 'Cisco'
building1[5] = 'Microsoft'
building1[6] = 'Dell'
building1[7] = 'EMC'
building1[8] = 'HPE'
'''
for floor in building1:
print(building1[floor])
'''
print(building1[6]) …Run Code Online (Sandbox Code Playgroud) u'\\u02c7'.isalpha()如果符号\xcb\x87不是字母,为什么返回 True?此方法仅适用于 ASCII 字符吗?
我希望能够计算输入中有多少韩文字母,就像我处理英文字母和数字一样:
a=0
b=0
c=0
d=0
e=0
num = input("type something ")
for i in num:
if(i.isupper()):
a=a+1
elif(i.islower()):
b=b+1
elif(i.isdigit()):
c=c+1
print("uppercase letters: ",a)
print("lowercase letters: ",b)
print("numbers: ",c)
print("korean letters: ",d)
Run Code Online (Sandbox Code Playgroud)
但我不知道该怎么做,我是否必须以ord()某种方式合并?
我无法以起始索引 1 初始化数组。我使用了插入附加,并且我希望索引以 1 而不是 0 开头:
n=int(input('enter '))
array=[]
for i in range(1,n+1):
print(i)
element=int(input('element '))
array.insert(i,element)
#array.append(element)
print(i,array[i])
Run Code Online (Sandbox Code Playgroud)
这给出了一个IndexError例外print(i,array[i]):list index out of range.
说我有一个像这样的字符串
'1 - hello.mp3'
'22 - hellox.mp3'
'223 - hellox.mp3'
'hellox.mp3'
Run Code Online (Sandbox Code Playgroud)
我希望输出是
'001 - hello.mp3'
'022 - hellox.mp3'
'223 - hellox.mp3'
'hellox.mp3'
Run Code Online (Sandbox Code Playgroud)
也就是说,如果开头是数字,则附加 0 使其成为三位数。
有没有办法在python中使用正则表达式来实现?
我正在使用 Python 将数据从 Oracle 表导出到 Pandas DataFrame,然后导出到 CSV 文件。
但我收到此错误:
AttributeError: 'generator' object has no attribute 'to_csv'
Run Code Online (Sandbox Code Playgroud)
我无法找出我的代码的问题出在哪里:
import cx_Oracle
import csv
import pandas as pd
import sqlalchemy
from sqlalchemy import create_engine
DATABASE = "MY database"
SCHEMA = "MY USER"
PASSWORD = "MY PASS"
connstr = "oracle://{}:{}@{}".format(SCHEMA, PASSWORD, DATABASE)
conn = sqlalchemy.create_engine(connstr)
result=pd.read_sql('My QUERY' , con=conn, chunksize=10000)
result.to_csv("test",sep=',',chunksize=10000)
Run Code Online (Sandbox Code Playgroud) 我的想法是否正确,这会自动关闭文件?
def get_file():
with open("file.csv", "rb") as f:
yield f
f = get_file()
do_stuff(f)
Run Code Online (Sandbox Code Playgroud)
如果没有,我如何编写一个返回文件对象的函数,同时确保它在接收器使用完文件后关闭该文件?
我std::vector在这一行中使用:
std::vector<bool> visited(length);
Run Code Online (Sandbox Code Playgroud)
解决LeetCode 问题:
给定一个整数数组 arr,您最初位于数组的第一个索引处。
在一个步骤中,您可以从索引 i 跳转到索引:
- i + 1 其中: i + 1 < arr.length。
- i - 1 其中:i - 1 >= 0。
- j 其中: arr[i] == arr[j] 和 i != j。
返回到达数组最后一个索引的最小步数。
请注意,您不能在任何时候跳出数组。
示例 1:
输入:arr = [100,-23,-23,404,100,23,23,23,3,404]
输出:3
解释:你需要从索引 0 --> 4 --> 3 --> 9 跳三下。请注意,索引 9 是数组的最后一个索引。
约束:
1 <= arr.length <= 5 * 10^4 -10^8 <= arr[i] <= 10^8
#include <vector>
#include <unordered_map>
#include <queue>
class …Run Code Online (Sandbox Code Playgroud) python ×9
python-3.x ×4
unicode ×2
arrays ×1
c++ ×1
django ×1
file ×1
index-error ×1
iterator ×1
object-model ×1
oracle ×1
pandas ×1
python-3.6 ×1
regex ×1
sqlalchemy ×1
std ×1
types ×1
vector ×1
yield ×1