首页 \ 问答 \ 如何在Python中有效地提取列表元素的特定子集(How to efficiently extract specific subsets of list elements in Python)

如何在Python中有效地提取列表元素的特定子集(How to efficiently extract specific subsets of list elements in Python)

我有一个长度为N的浮点数列表,称为vals ,长度为N的列表为0和1,称为bits 。 我想提取两个列表: vals的元素对应于0的bits ,以及其余的元素对应于1的bits 。 我现在正在做:

valsbits = zip(vals,bits)

els0 = [v for v,b in valsbits if b == 0]

els1 = [v for v,b in valsbits if b == 1]

但必须有更好的方法。 此外,我正在为许多不同的bits向量执行此操作,因此可能有一种聪明的方法来执行此操作。


I have a a length N list of floats calledvals and a length N list of 0s and 1s called bits. I want to extract two lists: the elements of vals that correspond to the 0s in bits, and the remaining elements that correspond to the 1s in bits. I'm currently doing:

valsbits = zip(vals,bits)

els0 = [v for v,b in valsbits if b == 0]

els1 = [v for v,b in valsbits if b == 1]

but there must be a better way. Also, I'm doing this for many different bits vectors, so there may be a clever way to do this entire operation.


原文:https://stackoverflow.com/questions/18726986
更新时间:2023-11-23 08:11

最满意答案

你可以这样做:

$(document).ready(function(){
  $(document).on('click', ".btn2", function () {
    var buttonContent = $(this).html();
    $("ol").append("<li>" + buttonContent + "</li>");
  });
});

You can do it like this:

$(document).ready(function(){
  $(document).on('click', ".btn2", function () {
    var buttonContent = $(this).html();
    $("ol").append("<li>" + buttonContent + "</li>");
  });
});

相关问答

更多
  • “<%= j post.content%>”确实解决了问题,就像评论中的“vee”一样。 http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#method-i-escape_javascript "<%= j post.content %>" did solve the problem, like "vee" write in the comment. http://api.rubyonrails.org/c ...
  • 我找到了问题的解决方案。 在rails 4中,当你加载jquery时,你必须将gem“jquery-turbolinks”添加到你的Gemfile中。 然后加载: //= require jquery.turbolinks 在你的application.js文件中。 这条线必须在“// = require jquery”和BEFORE“// = require turbolinks”之后出现。 之后,jquery-mobile在rails 4中运行良好 I figured out the solution ...
  • 您可以为任何对象使用CSS选择器。 例如: 使用jQuery,您可以: $('.my-target').click(); 您可以在按钮中添加类属性 <%= button_to 'Renew', [:renew, post], method: :post, class: 'my-target' %> You can use a CSS selector for any object. For example:
  • 您可以检查index.js.erb中的条件,是否有params [:page]。 例如。 index.js.erb的 <% if params[:page] %> #if clicked on load more, we will get params[:page] $('.gig-search-list').append("<%= j render(partial: 'gigs') %>"); <% else %> $('.gig-search-list').html("<%= j render(pa ...
  • 在Application.js中更改订单Replce this //= require jquery //= require jquery_ujs //= require turbolinks //= require_tree . //= require signup_validation 同 //= require jquery //= require jquery_ujs //= require turbolinks //= require signup_validation //= require_ ...
  • 在您的路线中,设置路线以接受此请求: 路线: get '/setmessagestatus/:status/:id', to: 'messages#set_message_status', as: 'set_message_status' 在您的控制器中创建设置状态的方法: 消息控制器: def set_message_status @message = Message.find(params[:id]) @message.status = params[:status] rend ...
  • 我假设你使用rails 3,如果是的话,使用jquery最简单的方法是使用jquery -rails gem来安装它(添加gem'jquery gem 'jquery-rails' )到你的Gemfile,运行bundle然后运行rails generate jquery:install和jquery can使用! I assume your using rails 3, if so the easiest way to use jquery is with the jquery-rails gem jus ...
  • 这个问题可能对你有所帮助。 另外我注意到你的js-fiddle中Javascript没有缩进。 如果您使用HAML,则缩进至关重要。 确保你的JS在第二行的第一个缩进开始之后:javascript :javascript $(document).ready( function() { alert('working'); } ); 另外。 如果您尝试使用HAML包含js 文件 ,则需要 = javascript_include_tag "my_js" 从这里 。 此外,如果这没有帮助。 查 ...
  • 你可以这样做, $('#moveslotsbutton').on('click', function () { var source = 'lots'; var rows = []; var rowi = -1; $.each(moveslotstable.rows('.active').data(), function (i, val) { $(rowi = rowi + 1, rows[rowi] = val[0 ...
  • 相关文章

    更多
  • Python 列表(list)操作
  • Mod_python: The Long Story
  • 【转帖】Python 资源索引
  • python2和python3的区别
  • Python资源索引 【转载】
  • Python 写的Hadoop小程序
  • Python基础 运算
  • python字典操作
  • 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)