我想从中得到:
keys = [1,2,3]
Run Code Online (Sandbox Code Playgroud)
对此:
{1: None, 2: None, 3: None}
Run Code Online (Sandbox Code Playgroud)
这是一种pythonic方式吗?
这是一种丑陋的方式:
>>> keys = [1,2,3]
>>> dict([(1,2)])
{1: 2}
>>> dict(zip(keys, [None]*len(keys)))
{1: None, 2: None, 3: None}
Run Code Online (Sandbox Code Playgroud) 该程序应该采用两个名称,如果它们的长度相同,则应检查它们是否是同一个单词.如果它是相同的单词,它将打印"名称相同".如果它们长度相同但字母不同,则会打印"名称不同但长度相同".我遇到问题的部分是在底部4行.
#!/usr/bin/env python
# Enter your code for "What's In (The Length Of) A Name?" here.
name1 = input("Enter name 1: ")
name2 = input("Enter name 2: ")
len(name1)
len(name2)
if len(name1) == len(name2):
if name1 == name2:
print ("The names are the same")
else:
print ("The names are different, but are the same length")
if len(name1) > len(name2):
print ("'{0}' is longer than '{1}'"% name1, name2)
elif len(name1) < len(name2):
print ("'{0}'is longer than '{1}'"% …Run Code Online (Sandbox Code Playgroud) import csv
with open('test.csv','rb') as file:
rows = csv.reader(file,
delimiter = ',',
quotechar = '"')
data = [data for data in rows]
Run Code Online (Sandbox Code Playgroud)
这是在Python中:读取csv文件并将列保存为变量.我无法评论,但我真的很困惑.'rb'是什么意思?
我想知道是否有等效的功能
fig, axarr = plt.subplots(3, 2, sharex='col', sharey='row')
Run Code Online (Sandbox Code Playgroud)
其中仅为现有图形生成轴数组,而不是创建新图形对象。我基本上需要创建一个 matplotlib 窗口,填充它,并在按下按钮时更新它。我想使用该subplots方法,因为它允许共享轴,但它会强制创建一个新的图形对象,这将打开一个新窗口。
我当前的代码如下所示:
fig = plt.figure()
# When button is pressed
fig.clear()
for i in range(6):
ax = fig.add_subplot(3, 2, i+1)
ax.plot(x, y)
plt.draw()
Run Code Online (Sandbox Code Playgroud)
我想做类似的事情
fig, axarr = plt.subplots(3, 2, sharex='col', sharey='row')
# When button is pressed
fig.clear()
axarr = fig.subplots(3, 2, sharex='col', sharey='row')
for i in range(3):
for j in range(2):
ax = axarr[i][j]
ax.plot(x, y)
plt.draw()
Run Code Online (Sandbox Code Playgroud)
或者,有没有办法直接使用 matplotlib 窗口?如果这也是一个选项,我可以将新图形对象绘制到现有窗口中。
如果这有什么不同,我正在使用 PySide 后端。谢谢。
我在读取文件后进行了一些计算,并希望将结果(单个数字)存储到另一个文件中.我希望以后可以使用此文件执行操作.我在将结果存储到文本文件时遇到问题.
我试过这个:
c = fdata_arry[:,2]*fdata_arry[:,4]
d = np.sum(c)
print d
f = open('test','w')
f.write(d)
f.close()
Run Code Online (Sandbox Code Playgroud)
这给了我这行的错误f.write(d):
非字符数组不能解释为字符缓冲区
我也试过用np.savetxt('test.dat',d)但是这给了我:
IndexError:元组索引超出范围
知道怎么解决这个问题?请注意,这d只是一个值,它是几个数字的总和.
我正在编写一个通用程序来读取和绘制来自.txt文件的大量数据.每个文件都有不同的列数.我知道每个文件都有8列我不感兴趣,所以我可以通过这种方式找出相关列的数量.如何读取数据并将每个相关列的数据分类到单独的变量中?
这是我到目前为止:
datafile = 'plotspecies.txt'
with open(datafile) as file:
reader = csv.reader(file, delimiter=' ', skipinitialspace=True)
first_row = next(reader)
num_cols = len(first_row)
rows = csv.reader(file, delimiter = ' ', quotechar = '"')
data = [data for data in rows]
num_species = num_cols - 8
Run Code Online (Sandbox Code Playgroud)
我见过有人说大熊猫对这类事情有好处,但我似乎无法导入它.我更喜欢没有它的解决方案.
python ×5
csv ×2
dictionary ×1
matplotlib ×1
output ×1
python-3.x ×1
string ×1
subplot ×1
typeerror ×1