Python矩阵索引(Python matrix indexing)
我有以下代码
l = len(time) #time is a 300 element list ll = len(sample) #sample has 3 sublists each with 300 elements w, h = ll, l Matrix = [[0 for x in range(w)] for y in range(h)] for n in range(0,l): for m in range(0,ll): x=sample[m] Matrix[m][n]= x
当我运行代码来填充矩阵时,我得到一条错误消息,说“列表索引超出范围”我输入了一个print语句来查看错误发生的位置,当m = 0且n = 3时,矩阵超出索引。
根据我对代码第四行的理解,我初始化了一个3X300矩阵,那为什么它会在0X3时超出索引?
I have the following code
l = len(time) #time is a 300 element list ll = len(sample) #sample has 3 sublists each with 300 elements w, h = ll, l Matrix = [[0 for x in range(w)] for y in range(h)] for n in range(0,l): for m in range(0,ll): x=sample[m] Matrix[m][n]= x
When I run the code to fill the matrix I get an error message saying "list index out of range" I put in a print statement to see where the error happens and when m=0 and n=3 the matrix goes out of index.
from what I understand on the fourth line of the code I initialize a 3X300 matrix so why does it go out of index at 0X3 ?
原文:https://stackoverflow.com/questions/39648251
更新时间:2023-05-14 17:05
最满意答案
$id = get_the_ID(); $taxonomy = get_term_by('id', $id)['taxonomy'];
Tou正在获取空的$ taxonomy,因为你试图使用一个帖子的id来获取术语细节,而不是术语id。 要获得与帖子相关的条款,您必须这样做:
$terms = wp_get_post_terms( $post_id, $taxonomy, $args );
$id = get_the_ID(); $taxonomy = get_term_by('id', $id)['taxonomy'];
Tou are getting empty $taxonomy because you are trying to get term details using the id of a post and Not the term id. To get the terms associated with the post you have to do this:
$terms = wp_get_post_terms( $post_id, $taxonomy, $args );
相关问答
更多-
最好使用数组来安排它,你可以注意到很多东西,将slug改为组合类别,我认为它现在可以正常工作。 add_action( 'init', 'create_portfolio_taxonomies', 0 ); function create_portfolio_taxonomies(){ $labels = array( 'labels' => array( 'name' ...
-
没关系! 我找到了 :) get_queried_object()->term_id; Nevermind! I found it :) get_queried_object()->term_id;
-
$id = get_the_ID(); $taxonomy = get_term_by('id', $id)['taxonomy']; Tou正在获取空的$ taxonomy,因为你试图使用一个帖子的id来获取术语细节,而不是术语id。 要获得与帖子相关的条款,您必须这样做: $terms = wp_get_post_terms( $post_id, $taxonomy, $args ); $id = get_the_ID(); $taxonomy = get_term_by('id', $id)['t ...
-
如何在wordpress循环中显示当前帖子自定义分类名称?(How to display current posts custom taxonomy name inside wordpress loop?)[2023-10-24]
对于任何有兴趣的人我现在用以下方法解决了这个问题 ID, 'department', array("fields" => "all")); ?> 并通过使用 slug ; ?> 作为班级名称。 谢谢 To anyone who is interested I have now solved this using:分类法不与其他分类法直接交互。 它们只与Post Objects交互并封装。 我能想到的唯一方法是使用WP_Query运行分类查询来收集所有利用BOTH分类法的帖子,然后遍历每个帖子以构建一系列独特的术语: $args = array( 'post_type' => 'post', 'tax_query' => array( 'relation' => 'AND', array('taxonomy' => 'auto_brand'), arra ...我通常通过wp_get_nav_menu_items过滤器来做这类事情。 当我第一次尝试这个时,我花了很长时间才能模拟现有的WP_Post或WP_Term作为导航菜单项。 您不能只是添加项目本身,但您需要稍微调整它,以便导航项目仍然链接到正确的目的地。 此外,如果您只是添加菜单项,它可能会弄乱您的菜单顺序。 所以我们必须使用自己的菜单顺序计数器重建菜单数组。 我在几个项目中使用了辅助函数和wp_get_nav_menu_items过滤器的变体的以下组合,以快速向菜单中添加新项目,无论是自定义帖子类型还是分类 ...解决了,“类别”是我的分类法的名称: @{ var categoryName = ""; foreach (dynamic term in Model.ContentItem.BlogPost.Category.Terms.Value) { categoryName = term.Name; } } Solved, 'Category' is the name of my taxonomy: @{ var categoryName = " ...获取分类中的所有帖子(Get all posts from a taxonomy)[2023-12-28]
使用税务查询( http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters ) $args['tax_query'] = array( array( 'taxonomy' => 'property_types' ,'field' => 'slug' ...尝试下面的代码: function people_init() { // create a new taxonomy register_taxonomy( 'people', 'new_post', array( 'label' => __( 'People' ), 'rewrite' => array( 'slug' => 'person' ), ) ); }add_action( 'init', 'people_init' ); try ...在查询Wordpress分类法时,Foreach循环返回多个项目(Foreach loop returning more than one item when querying Wordpress taxonomy)[2022-06-06]
我发现我的问题是我使用这个术语作为分类法,它给我带来了分类法下的所有帖子而不是类别(术语)。 这是一个快速修复,我只是希望我昨晚能够抓住它: $args = array( 'post_type' => 'restaurant-menu', 'tax_query' => array( array( 'taxonomy' => 'menu-category', 'field' => 'slug ...相关文章
更多- Python资源索引 【转载】
- 【转帖】Python 资源索引
- 企业级搜索引擎Solr 第三章 索引数据(Indexing Data)[1] (转)
- Python:渗透测试开源项目【源码值得精读】
- Matrix67: My Blog » Blog Archive » 漫话中文自动分词和语义识别(下):句法结构和语义结构
- Pick定理的几个出人意料的应用(转载自Matrix67)
- 探索 Python,第 1 部分: Python 的内置数值类型
- python2和python3的区别
- Python的文件类型
- python的下载与安装
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)