我使用的是 Jupyter,Python 版本是 3.5。在我的while循环中,执行顺序不正确;一次迭代的结果input显示在上一次迭代的最后一次之前。print这是我的代码。
from IPython.display import display
import pandas as pd
df = pd.DataFrame({'a':[1,2],'b':[3,4]})
while(True):
a = input("please input:\n")
display(df.head())
print (a)
Run Code Online (Sandbox Code Playgroud)
所以我得到了这段代码:
OPTION_1 if (i in (0, 1, 2) and j in (0, 1, 2)) else OPTION_2
Run Code Online (Sandbox Code Playgroud)
OPTION_1只有当i和j都在给定的范围内时,它才会这样做。我想知道我是否可以以某种方式缩短它。试过这个:
OPTION_1 if (i and j) in (0, 1, 2) else OPTION_2
Run Code Online (Sandbox Code Playgroud)
但是如果i==4和j==2,它确实OPTION_1,尽管i不在范围内。
我有以下 Python 代码:
#!/usr/bin/python3
import time
class Greeter:
def __init__(self, person):
self.person = person
print("Hello", person);
def __del__(self):
print("Goodbye", self.person);
def inspect(self):
print("I greet", self.person);
if __name__ == '__main__':
to_greet = []
try:
to_greet.append(Greeter("world"));
to_greet.append(Greeter("john"));
to_greet.append(Greeter("joe"));
while 1:
for f in to_greet:
f.inspect()
time.sleep(2)
except KeyboardInterrupt:
while (len(to_greet) > 0):
del to_greet[0]
del to_greet
print("I hope we said goodbye to everybody. This should be the last message.");
Run Code Online (Sandbox Code Playgroud)
当我运行它并Ctrl+C在睡眠期间,我得到:
Hello world
Hello john
Hello joe
I greet world
I …Run Code Online (Sandbox Code Playgroud) 我想“分解”一个Image(即split()),通过将其中一个通道与另一个通道组合来修改它,然后再次重新“组合”(即merge())它们。
此示例应使用我希望存在的功能将边缘映射到红色通道来突出显示边缘:
from PIL import Image, ImageFilter
base = Image.open("example.jpg")
base.show()
Run Code Online (Sandbox Code Playgroud)
edges = (base
.convert("L")
.filter(ImageFilter.Kernel((3, 3), (-1, -1, -1, -1, 8,-1, -1, -1, -1), 1, 0))
.point(lambda p: 255 if p > 10 else 0))
edges.show()
Run Code Online (Sandbox Code Playgroud)
r, g, b = base.split()
# now I want the blue channel/band for each pixel to be whatever respective
# pixel is brighter on a given set of bands
# this is what I want …Run Code Online (Sandbox Code Playgroud) 我有一个整数列表,我想根据某个条件将它们分开。我想获取列表元素的总和和计数,当三个或更多连续元素等于 0 时停止;然后 sum 和 count 订单从停止的地方重新开始。
例如,列表的一部分是:
[8, 2, 1, 1, 2, 0, 0, 0, 0, 0, 6, 0, 2, 0, 0, 0, 8, 0, 0, 2, 0, 0, 0, 6, 0, 0]
Run Code Online (Sandbox Code Playgroud)
该过程将是:
[8, 2, 1, 1, 2, 0, 0, 0, 0, 0, 6, 0, 2, 0, 0, 0, 8, 0, 0, 2, 0, 0, 0, 6, 0, 0]
Run Code Online (Sandbox Code Playgroud)
所以我想要的输出是:
[[14, 5], [8, 3], [10, 4], [6, 3]]
Run Code Online (Sandbox Code Playgroud)
到目前为止我所写的计算总和没问题,但我的问题是部分内的零不计入长度。
当前(不正确)输出:
[[14, 5], [8, 2], [10, 2], [6, 2]] …Run Code Online (Sandbox Code Playgroud) 我有一个带有属性的类和一个带有空值的列表。当生成a和b的两个实例并向属性添加元素时,发现属性没有实例化。使用id查看a.property和b.property。内存地址是一样的。为什么?
如何才能property attribute成为instance attribute?
我的代码示例如下:
class MyClass:
property = []
def __init__(self):
pass
def append(self, value):
self.property.append(value)
a = MyClass()
b = MyClass()
a.append(1)
print(a.property)
b.append(1)
print(a.property)
print(b.property)
print(id(a.property))
print(id(b.property))
Run Code Online (Sandbox Code Playgroud)
结果是:
[1]
[1, 1]
[1, 1]
1866383694784
1866383694784
Run Code Online (Sandbox Code Playgroud)
我尊重的结果:
[1]
[1]
[1]
Run Code Online (Sandbox Code Playgroud) 我有两个形状为 (74395, 1) 的 NumPy 数组,存储浮点值,其中 arr1[0] 与 arr2[0] 相关,依此类推。我想根据第二个数组中存储的值按升序对它们进行排序。
举个例子:
arr1: [[1]
[2]
[3]]
arr2: [[6]
[2]
[4]]
Run Code Online (Sandbox Code Playgroud)
想要的结果:
arr1: [[2]
[3]
[1]]
arr2: [[2]
[4]
[6]]
Run Code Online (Sandbox Code Playgroud)
我怎样才能在Python中做到这一点?