我怎样才能证明这段代码的输出是正确的?
N = int(input())
case = '#'
print(case)
for i in range(N):
case += '#'
print(case)
Run Code Online (Sandbox Code Playgroud) 我有一个元组列表,其中包含11个点的值和坐标
dotted_array = [(0, 0, '.'), (2, 0, '.'), (3, 0, '.'), (0, 1, '.'), (2, 1, '.'), (0, 2, '.'), (2, 2, '.'), (3, 2, '.'), (0, 3, '.'), (2, 3, '.'), (3, 3, '.')]
Run Code Online (Sandbox Code Playgroud)
我列出了5个清单:
list_of_signs = [['+', '+', '-', '+', '+', '+', '+', '-', '+', '+', '-'], ['+', '+', '-', '+', '-', '+', '+', '-', '+', '+', '-'], ['+', '+', '-', '+', '+', '+', '+', '-', '+', '+', '-'], ['+', '-', '-', '+', '+', '+', '+', '-', …Run Code Online (Sandbox Code Playgroud) 我写了一个图像雕刻脚本来协助我的工作.该工具通过指定的扩展来雕刻图像,并与哈希数据库进行比较.
该工具用于搜索已安装的驱动器,其中一些驱动器上安装了操作系统.
我遇到的问题是当一个驱动器安装了操作系统时,它正在"所有用户"目录中搜索,因此包含来自我本地光盘的图像.
我无法弄清楚如何跳过"所有用户"目录,只是坚持已安装的驱动器.
我的os.walk部分如下:
for path, subdirs, files in os.walk(root):
for name in files:
if re.match(pattern, name.lower()):
appendfile.write (os.path.join(path, name))
appendfile.write ('\n')
log(name)
i=i+1
Run Code Online (Sandbox Code Playgroud)
任何帮助深表感谢
我有一个如下所示的 CSV 文件:
patient_id, age_in_years, CENSUS_REGION, URBAN_RURAL_STATUS
11511, 7 Northeast, Urban,
9882613, 73, South, Urban,
32190339, 49, West, Urban,
32190339, 49, West, Urban,
32190339, 49, West, Urban,
32190339, 49, West, Urban,
.....
Run Code Online (Sandbox Code Playgroud)
现在我的代码是这样的:
df = pd.read_csv(filename, index_col = 0)
Run Code Online (Sandbox Code Playgroud)
这给出了以下输出:
patient_id age_in_years CENSUS_REGION URBAN_RURAL_STATUS YEAR MONTH
11511 7 Northeast Urban 2011 6
9882613 73 South Urban 2011 7
32190339 49 West Urban 2011 8
32190339 49 West Urban 2011 8
32190339 49 West Urban 2011 8
32190339 49 West Urban …Run Code Online (Sandbox Code Playgroud) 正如在http://matplotlib.org/users/event_handling.html中所描述的那样,以下示例代码可以正常工作
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.random.rand(10))
def onclick(event):
print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
event.button, event.x, event.y, event.xdata, event.ydata)
cid = fig.canvas.mpl_connect('button_press_event', onclick)
Run Code Online (Sandbox Code Playgroud)
但为什么呢
from matplotlib.figure import Figure
fig = Figure()
ax = fig.add_subplot(111)
ax.plot(np.random.rand(10))
def onclick(event):
print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
event.button, event.x, event.y, event.xdata, event.ydata)
cid = fig.canvas.mpl_connect('button_press_event', onclick)
Run Code Online (Sandbox Code Playgroud)
不工作(虽然它基本相同)?错误是
AttributeError: 'NoneType' object has no attribute 'mpl_connect'
Run Code Online (Sandbox Code Playgroud)
这真让我困惑,因为
type(fig)
Run Code Online (Sandbox Code Playgroud)
在两种情况下都按预期给出相同的结果:
<class 'matplotlib.figure.Figure'>
Run Code Online (Sandbox Code Playgroud) 我有这个数据结构:
[((u'die', u'die'), (u'welt', u'WELT')),
((u'welt', u'WELT'), (u'ist', u'ist'))]
Run Code Online (Sandbox Code Playgroud)
如何以最pythonic的方式将上述结构转换为以下结构?带拉链?
[((u'die', u'welt'), (u'die', u'WELT')),
((u'welt', u'ist'), (u'WELT', u'ist'))]
Run Code Online (Sandbox Code Playgroud) 我想将所有的string值转换Pandas DataFrame成float,我可以定义一个简短的函数来做到这一点,但它不是Pythonic的方法.我的DataFrame看起来像这样:
>>> df = pd.DataFrame(np.array([['1', '2', '3'], ['4', '5', '6']]))
>>> df
0 1 2
0 1 2 3
1 4 5 6
>>> df.dtypes
0 object
1 object
2 object
dtype: object
>>> type(df[0][0])
<type 'str'>
Run Code Online (Sandbox Code Playgroud)
我只是想知道是否有一些内置函数Pandas DataFrame将所有string值转换为float.如果你知道Pandas doc上的内置函数,请发布链接.
我在python 3中制作了一个简单的疯狂libs程序,用户输入名词和代词,程序应打印出来自用户的输入.
这是我的代码:
print ("Welcome to Mad Libs. Please enter a word to fit in the empty space.")
proper_noun = input("One day _________ (Proper Noun)").lower()
ing_verb = input("Was __________ (Verb + ing) to the").lower()
noun1= input("to the _________ (Noun)").lower()
pronoun1 = input("On the way, _____________ (Pronoun)").lower()
noun2 = input("Saw a ________ (Noun).").lower
pronoun2 = input("This was a surprise so ________ (Pronoun)").lower()
verb2 = input("_________ (verb) quickly.").lower()
#Asks user to complete the mad libs
print ("One day " + proper_noun)
print …Run Code Online (Sandbox Code Playgroud) 我正在处理一个需要多个同名模块的项目。这是架构的代表性摘录,其中包含__init__.py显示哪些文件夹是模块的文件:
/path1
/ProjectA
__init__.py
/src
__init__.py
/ctrl
__init__.py
somectrl.py
...
/path2
/ProjectA
__init__.py
/src
__init__.py
someclass.py
Run Code Online (Sandbox Code Playgroud)
在我的课堂上someclass.py,我想像这样导入somectrl.py:
from ProjectA.src.ctrl import somectrl
Run Code Online (Sandbox Code Playgroud)
但是导入失败:它告诉我没有ctrl包。似乎它只是查看ProjectAfrom path2,而完全忽略ProjectAfrom path1!双方path1并path2都在我PYTHONPATH。那他们两个都联系不上吗?
有没有干净的方法摆脱这种令人讨厌的情况?
我正在尝试创建一个函数来测试函数是否存在,然后根据它是否存在返回一个布尔值.
这是我的代码; 但是,Python IDLE 3.5告诉我,我的eval()语句有错误,但我没有看到错误:
def testFunction(entity):
try eval(entity)():
return True
except NameError:
return False
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
python ×10
python-3.x ×3
list ×2
pandas ×2
csv ×1
function ×1
matplotlib ×1
os.walk ×1
python-2.7 ×1
string ×1
try-except ×1
tuples ×1
typeerror ×1