Pop()列表列表中列表的元素

时间:2015-11-26 03:28:40

标签: python list pop

我有一份清单清单。我想从列表列表中的第二个列表中弹出()一个元素。 这是一个例子:

    '$pdf_creates_controller.rb'

class PdfCreatesController < ApplicationController
  before_action :set_pdf_create, only: [:show, :edit, :update, :destroy]

  # GET /pdf_creates
  # GET /pdf_creates.json
  def index
    @pdf_creates = PdfCreate.all
  end

  # GET /pdf_creates/1
  # GET /pdf_creates/1.json
  def show
  end

  # GET /pdf_creates/new
  def new
    @pdf_create = PdfCreate.new
  end

  # GET /pdf_creates/1/edit
  def edit
  end

  # POST /pdf_creates
  # POST /pdf_creates.json
  def newpdf
    @pdf_create = PdfCreate.new(pdf_create_params)
    respond_to do |format|
      if @pdf_create.save
        format.html { redirect_to @pdf_create, notice: 'Pdf create was successfully created.' }
        format.json { render :show, status: :created, location: @pdf_create }
      else
        format.html { render :new }
        format.json { render json: @pdf_create.errors, status: :unprocessable_entity }
      end
    end
  end

  def create
    @pdf_create = PdfCreate.new(pdf_create_params)
    #data = File.read(Rails.root + "tmp/consent(1).pdf")
    #Document.create pdfload: data 

    respond_to do |format|
      if @pdf_create.save
        format.html { redirect_to @pdf_create, notice: 'Pdf create was successfully created.' }
        format.json { render :show, status: :created, location: @pdf_create }
        format.pdf { send_data @pdf_create.render}

        else
           format.html { render :new }
        #  format.json { render json: @pdf_create.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /pdf_creates/1
  # PATCH/PUT /pdf_creates/1.json
  def update
    respond_to do |format|
      if @pdf_create.update(pdf_create_params)
        format.html { redirect_to @pdf_create, notice: 'Pdf create was successfully updated.' }
        format.json { render :show, status: :ok, location: @pdf_create }
      else
        format.html { render :edit }
        format.json { render json: @pdf_create.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /pdf_creates/1
  # DELETE /pdf_creates/1.json
  def destroy
    @pdf_create.destroy
    respond_to do |format|
      format.html { redirect_to pdf_creates_url, notice: 'Pdf create was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_pdf_create
      @pdf_create = PdfCreate.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def pdf_create_params
      params.require(:pdf_create).permit(:pdfload)
    end
end

所以,打印列表列表给了我:

>>> list1=[1,2]
>>> list2=[3,4]
>>> listoflists=[list1, list2]

我想要弹出列表列表中第二个列表的第一个元素,即3。

>>>listoflists
[[1, 2], [3, 4]]

给我以下错误;

>>>listoflists.pop([1][0])

2 个答案:

答案 0 :(得分:2)

listoflists[1].pop(0)

listoflists [1]等于list2

所以

listoflists [1] .pop(0)等于list2.pop(0)

答案 1 :(得分:0)

弹出2d数组的正确方法就是这样

list1=[1,2]
list2=[3,4]
listoflists=[list1, list2]

print listoflists

listoflists[0].pop(0)//correct way to pop

print listoflists

here是另一篇类似于你的帖子,在弹出也可能有用的2d列表中。