我试图在 python 中的嵌套列表中获取元素的索引 - 例如[[a, b, c], [d, e, f], [g,h]](并非所有列表的大小都相同)。我试过使用
strand_value= [x[0] for x in np.where(min_value_of_non_empty_strands=="a")]
Run Code Online (Sandbox Code Playgroud)
但这只是返回一个空列表,即使该元素存在。知道我做错了什么吗?
我有140个阵列,其尺寸为734 x 1468.我需要在734 x 1468条目中的140个阵列中的平均值,我开始通过嵌套for循环迭代数据,但它太慢了.我试过使用这个np.mean功能
res = np.mean([megadatalist[i].variables['analysed_sst'][0] for i in range(0,140)], axis=2)
Run Code Online (Sandbox Code Playgroud)
res.shape 给出(140,734)
我需要尺寸734,1468的结果
megadatalist 由140个netCDF文件组成.
我想在python上做一个练习(我的版本是python33练习如下:
编写一个函数find_longest_word1(),它接受一个单词列表并返回最长单词的长度.仅使用更高阶的功能.
我的尝试是:
def find_longest_word1(a):
out = max(list(map(len,a)))
print (out)
find_longest_word1(["This","is","a","listing"])
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
max() missing 1 required positional argument 'b'
Run Code Online (Sandbox Code Playgroud)
我已经传递了一个列表作为参数.我是否需要为max函数提供其他参数?
嗨,我正在尝试排序字典列表并使用attrgetter进行排序,但它显示未定义的attrgetter.
>>> a = ['{"count": 1, "places": [{"lat": 12.99455070495605, "lng": 77.6714096069336, "address": "Karnataka, India"}], "created_at": "2015-03-17 04:25:10", "updated_at": "", "environments": [{"display_address": "Karnataka, India", "name": "", "views": 0, "country": "India", "region": "Karnataka", "address": "4th Cross Street, Mottappa Compound, A Narayanapura, Mahadevapura, Bengaluru, Karnataka 560016, India", "comments": 0, "height": 758, "width": 1008, "image_url": "http://9cb448a50dcef7c3d28e-10dfb1f9ec7a490e45082ae9c51f52eb.r27.cf1.rackcdn.com/photos/4185354/ff90ae45348134c1345c49b2c853a31e.jpg", "likes": 0, "photo_where": "", "lat": 12.99455070495605, "photo_type": "panorama", "lng": 77.6714096069336, "thumb_url": "http://0f9cb149e5cc61745fd5-2870fdbe0c753e4bc418911d9614db21.r17.cf1.rackcdn.com/thumbs/534901/", "tiles_url": "http://c6c0b88e96acc3346aff-c40106bef0a12d7839dc46608748511d.r69.cf1.rackcdn.com/tiles/534901/", "id": 534901, "is_spherical": 0, "source": {"fov": 123.867, "scale": 0.0, "startx": 0, "vertical_fov": 39.4, "starty": …Run Code Online (Sandbox Code Playgroud) 给出以下数据帧:
import pandas as pd
df = pd.read_json('{"genre":{"0":"Drama","1":"Comedy","2":"Action","3":"Thriller"},"count":{"0":1603,"1":1200,"2":503,"3":492}}')
Run Code Online (Sandbox Code Playgroud)
是否有大熊猫单线/快速生成直方图,其中条形基于"计数"列,而x轴标签是"类型"列?
我发现在Python 3中使用.format()方法进行字符串格式化的可能性,但是我提出了一个我不理解的错误。
因此,为什么以下行可以[让我认为可以完全像传递给format()的参数那样使用“ 0”]:
s = 'First letter of {0} is {0[0]}'.format("hello")
#gives as expected: 'First letter of hello is h'
Run Code Online (Sandbox Code Playgroud)
但这不是[在{0}中将方法或函数应用于0无效吗?]:
s = '{0} becomes {0.upper()} with .upper() method'.format("hello")
Run Code Online (Sandbox Code Playgroud)
引发以下错误:
AttributeError: 'str' object has no attribute 'upper()'
Run Code Online (Sandbox Code Playgroud)
为什么引发的错误说明我已将upper用作属性而不是方法?还有另一种方法可以做到:
s = '{} becomes {} with .upper() method'.format("hello","hello".upper())
#gives as expected: 'hello becomes HELLO with .upper() method'
Run Code Online (Sandbox Code Playgroud)
谢谢!
我一直在绘制直方图。我的代码如下所示:
x0=-15000
x1=15000
b=np.arange(x0,x1,(x1-x0)/250.)
plt.plot(b[0:-1], plt.hist(non_zeros(-e[0]), bins=b, normed=1, visible=0)[0], color = "k", label=r'$\gamma$ = 1.0')
Run Code Online (Sandbox Code Playgroud)
我对直方图进行了标准化,因此曲线下的面积等于 1。e[0] 只是我从文档中获取的一些数据。
我现在想要的是仔细检查直方图下的值是否等于 1。如何才能做到这一点?
python ×7
numpy ×2
area ×1
arguments ×1
dimensions ×1
formatting ×1
histogram ×1
matplotlib ×1
max ×1
mean ×1
nested-lists ×1
netcdf ×1
pandas ×1
python-2.7 ×1
python-3.x ×1
typeerror ×1