A story about Python pop and append

Sometimes I want to use a FIFO-like array by turning a for statement. It seems that there is a queue in Python, but since I am dealing with Javascript, MATLAB, Objective-C, Java, etc. at the same time, I definitely want to write with the same code.

This time I got really into Python. I don't think pop and append are bad, but maybe some people will implement it using pop and append as well, so make a note.

What I wanted to do this time was to get an array that is shifted by one character from the array that is arranged like [1,2,3,4,5,6,7,8,9,10], and the following Make a matrix like this.

無題2.png

So, this time I wrote it in Python, and this is the code that fits nicely. (This code is simplified and there is no problem if you do not write it like this, but the original code had to be written like this)

data_array=[1,2,3,4,5,6,7,8,9,10]

A=[]
R=[]

for i in range(10):
    if(len(A)<5):
        A.append(data_array[i])
    else:
        R.append(A)
        A.pop(0)
        A.append(data_array[i])

The idea is that the first 5 characters should be append normally, and after that, pop and append should be used to create a FIFO-like array and add it.

However, the result is miserably below.

無題.png

Hmm! ?? You're popping properly, right? Append, right? ?? what! ?? I was tempered with that feeling, but I made a rudimentary mistake that I should be careful about in Python. You have to be careful about the memory of the array.

The details are introduced in detail here. http://qiita.com/utgwkk/items/5ad2527f19150ae33322

And here is the correct code. It seems that you have to move it to another memory with copy.deepcopy.

import copy

data_array=[1,2,3,4,5,6,7,8,9,10]

A=[]
R=[]

for i in range(10):
    if(len(A)<5):
        A.append(data_array[i])
    else:
        temp_data = copy.deepcopy(A)
        R.append(temp_data)
        A.pop(0)
        A.append(data_array[i])

Recommended Posts

A story about Python pop and append
A story about modifying Python and adding functions
A story about Go's global variables and scope
A story about running Python on PHP on Heroku
A story about making 3D space recognition with Python
About python objects and classes
About Python variables and objects
A story about making Hanon-like sheet music with Python
About Python, len () and randint ()
About Python datetime and timezone
A story about kindergartens, nursery schools, and children's gardens
A memorandum about correlation [Python]
A memorandum about Python mock
About Python and regular expressions
A story about cross-compiling a python package for AWS Lambda and deploying it serverless
About Python and os operations
Python # About reference and copy
About Python sort () and reverse ()
A note about [python] __debug__
A story about how to specify a relative path in python.
[Python] Chapter 01-03 About Python (Write and execute a program using PyCharm)
A story about an amateur making a breakout with python (kivy) ②
A story about an amateur making a breakout with python (kivy) ①
A story about trying to implement a private variable in Python.
A story about a python beginner stuck with No module named'http.server'
Python a + = b and a = a + b are different
The story of Python and the story of NaN
About installing Pwntools and Python2 series
A refreshing story about Python's Slice
Python: A Note About Classes 1 "Abstract"
A sloppy story about Python's Slice
About python dict and sorted functions
About dtypes in Python and Cython
About Python pickle (cPickle) and marshal
[Python] About Executor and Future classes
About Python, from and import, as
[Python] return A [or / and] B
A story about using Python's reduce
Strange and horrifying Python error story
A note about mock (Python mock library)
A story about adding a REST API to a daemon made with Python
A little more about references ~ Using Python and Java as examples ~
A story about trying to run multiple python versions (Mac edition)
About _ and __
A story about everything from data collection to AI development and Web application release in Python (3. AI development)
A story connecting Slack and google spreadsheets
A Java programmer studied Python. (About type)
A story about machine learning with Kyasuket
A memo with Python2.7 and Python3 on CentOS
Connect a lot of Python or and and
Difference between append and + = in Python list
The story of blackjack A processing (python)
A story about a 503 error on Heroku open
Talking about old and new Python classes
Talking about Python class attributes and metaclasses
About February 02, 2020 * This is a Python article.
A story about custom users having a sweet and painful look at Django
A story about a beginner making a VTuber notification bot from scratch in Python
A story about automating online mahjong (Mahjong Soul) with OpenCV and machine learning
A story about my new study of Python after 3 years of MATLAB experience
A story about trying to connect to MySQL using Heroku and giving up