似乎找不到这个问题的直接答案。我只想知道如何导入保存在\scripts目录下的文件。
我将路径添加到sys.path...
import sys
sys.path.insert(0, "C:\\my_Stuff\\data_science\\scripts")
Run Code Online (Sandbox Code Playgroud)
我__init__.py在\scripts目录下添加了一个文件,该目录与文件所在的目录相同tree.py。
我跑...
import tree as tr
Run Code Online (Sandbox Code Playgroud)
...但是仍然出现错误。
我有一个名为“chinchars.txt”的 .txt 文件。在里面,我有一行包含这两个字符:
\n\n\xe8\x8a\x82\xe6\x97\xa5
\n\n我如何读取这个文本文件并将其返回给字符?\n使用此代码:
\n\ninputFile = open(\'chinchars.txt\').readlines()\nRun Code Online (Sandbox Code Playgroud)\n\n它输出这个错误:
\n\nUnicodeDecodeError: \'charmap\' codec can\'t decode byte 0x8f in position \n18: character maps to <undefined>\nRun Code Online (Sandbox Code Playgroud)\n\n我相信我需要以某种方式“解码”这些字符。这将如何实现?
\n我正在尝试使用数据帧列 ORIG 上的以下算法对 python 3.6 pandas 数据帧列的每个值进行散列:
HK_ORIG = base64.b64encode(hashlib.sha1(str(df.ORIG).encode("UTF-8")).digest())
Run Code Online (Sandbox Code Playgroud)
但是,上面提到的代码不会对列的每个值进行哈希处理,因此,为了对 df-column ORIG 的每个值进行哈希处理,我需要使用 apply 函数。不幸的是,我似乎还不够好,无法完成这项工作。
我想它看起来像下面的代码:
df["HK_ORIG"] = str(df['ORIG']).encode("UTF-8")).apply(hashlib.sha1)
Run Code Online (Sandbox Code Playgroud)
我非常期待您的回答!提前谢谢了!
IDLE 直接停止在我的桌面上工作,就像当我尝试打开它时没有任何反应,它甚至不会给我一条错误消息。不过在我的笔记本电脑上它运行良好。右键单击并尝试编辑脚本也没有任何作用。这完全是在一夜之间发生的,我没有更改或安装任何东西。我完全不知道这是怎么发生的。
我尝试重新安装并将文件从笔记本电脑复制到台式机,但到目前为止没有任何效果。
深度优先树遍历的示例:
class Node:
def __init__(self, value):
self._value = value
self._children = []
def add_child(self, child):
self._children.append(child)
def __iter__(self):
return iter(self._children)
def depth_first(self):
yield self
for c in self:
yield from c.depth_first()
Run Code Online (Sandbox Code Playgroud)
据我所知,yield from它不会立即消耗发生器,而是yield向上传递给它的调用者.
但是这个传递过程仍然存在,因此yield将从每个节点一直传递到它的根,我们可以通过重复来描述运行时间(假设它是一个简单的二叉树,但想法是一样的):
T(n)= 2*T(n/2)+Θ(n)
?(n)存在是因为此节点必须yield将从其后代传递的所有内容传递给其父节点.从上面的公式得出的时间复杂度是:
O(nlogn)
然而,树遍历的时间复杂性只有O(n)在我不使用yield或根本不使用yield from时.
我想知道我是否误解了如何yield工作或者编写这样的递归生成器根本不可行.
如何将以下两个类组合成一个类Rectangle,以便可以通过rect = Rectangle(side_a,side_b)或rect = Rectangle(side_a,area)创建Rectangle对象?
class Rectangle1:
def __init__(self, side_a, side_b):
self.side_a = side_a
self.side_b = side_b
self.area = self.side_a * self.side_b
class Rectangle2:
def __init__(self, side_a, area):
self.side_a = side_a
self.area = area
self.side_b = self.area / side_a
Run Code Online (Sandbox Code Playgroud) 我正在尝试让一个对象反序列化为自身。我尝试过以下方法:-
public class JobID
{
public string jobname;
public string first;
public string second;
public string third;
public string clientName;
public string workflow;
}
public void load(string fname)
{
string s = File.ReadAllText(fname);
this = JsonConvert.DeserializeObject<JobID>(s);
}
Run Code Online (Sandbox Code Playgroud)
但根据我得到的错误,这个词是“只读”的。我用过 'this.jobname = "X";' 之前很清楚“这个”不是只读的。我正在使用 Newtonsof.Json。
这是我的数据:
a3=pd.DataFrame({'OfficeName':['a','b','c','d','e','f','g','h'],
'Ratio': [0.1,0.15,0.2,0.3,0.2,0.25,0.1,0.4]})
Run Code Online (Sandbox Code Playgroud)
这是我绘制条形图的代码:
fig, ax = plt.subplots()
ind = np.arange(a3.loc[:,'OfficeName'].nunique()) # the x locations for the groups
width = 0.35 # the width of the bars
p1 = ax.bar(ind,a3.loc[:,'Ratio'],width)
ax.set_title('Ratio of refunds to purchases')
ax.set_xticklabels(a3.loc[:,'OfficeName'],ha='center')
#ax.set_xticklabels(['a','b','c','d','e','f','g','h'],ha='center')
ax.set_xlabel('x Group')
ax.set_ylabel('Ratio')
plt.show()
Run Code Online (Sandbox Code Playgroud)
但是,在我的图表中,第一个 x 标签丢失了:
我认为这个问题与Matplotlib不同:将x轴刻度标签向左移动一个位置,因为我什至不需要旋转x标签。
谁能解释为什么会发生这种情况以及如何解决它?
我有一个在 Python 2 中完美运行的正则表达式:
parts = re.split(r'\s*', re.sub(r'^\s+|\s*$', '', expression)) # split expression into 5 parts
Run Code Online (Sandbox Code Playgroud)
这个正则表达式将一个表达式分成 5 个部分,例如,
'a * b = c' will be split into ['a', '*', 'b', '=', 'c'],
'11 + 12 = 23' will be split into ['11', '+', '12', '=', '23'],
'ab - c = d' will be split into ['ab', '-', 'c', '=', 'd'],
Run Code Online (Sandbox Code Playgroud)
等等。
但是在 Python 3 中,这个正则表达式的工作方式完全不同,
'a * b = c' will be split into ['', 'a','', '*', '', 'b','', …Run Code Online (Sandbox Code Playgroud) 我有一个D非负浮点数的对称 NumPy 矩阵。i第行第列中的数字表示对象和j之间的距离,无论它们是什么。矩阵很大(约 10,000 行/列)。我想检查矩阵中的所有距离是否服从三角不等式,即:对于所有, , 。ijD[i,j]<=D[i,k]+D[k,j]ijk
通过使用三重嵌套循环可以非常低效地解决该问题。但有没有更快的矢量化解决方案?
python ×8
python-3.x ×3
pandas ×2
algorithm ×1
apply ×1
c# ×1
constructor ×1
dataframe ×1
decode ×1
distance ×1
file ×1
hash ×1
json ×1
label ×1
matplotlib ×1
numpy ×1
python-2.7 ×1
python-idle ×1
regex ×1
utf-8 ×1
yield ×1