Python list and the mutual conversion between list and array, pythonarray

Source: Internet
Author: User
Tags python list

Python list and the mutual conversion between list and array, pythonarray

This article describes how to convert list and array in Python. We will share this with you for your reference. The details are as follows:

List in python is an ordered set that allows you to add or delete elements at any time;

#-*-Coding: UTF-8-*-frameID = 1frameID_list = [] frameID_list.append (frameID) print (frameID_list) frameID = 2frameID_list.append (frameID) print (frameID_list) name = 'srx' frameID _ list. append (name) print (frameID_list) print (len (frameID_list) # print the length (frameID_list [2]) # print indexes using [] (frameID_list [-1]) frameID_list.insert (1, 'wjc') # Insert the element print (frameID_list) frameID_list.pop () # Delete the last element print (frameID_list) frameID_list.pop (0) # Delete the print (frameID_list) element at the specified position)

The following describes the conversion between list and array in numpy.

First, the element attributes in the list must be consistent before conversion, as shown below. The element in frame_ID_list is the frame number of the str record. array converts it to an array of the int type, and then performs a mathematical operation on each element of the converted array. Because the result after the operation is float, therefore, in the second row, it is strongly converted to the int type, and finally the np array is converted back to the list. The entire operation is equivalent to completing the list (which contains elements of the str type) the process of performing mathematical operations on each element and then switching back to the list.

frame_ID_list_np = np.array(frame_ID_list, dtype = int)frame_ID_list_np = np.array(frame_ID_list_np / 1000 * fps, dtype = int)frame_ID_list = frame_ID_list_np.tolist()

In addition, you need to note that when you determine whether two variables are equal after type conversion, for example:

frame_ID = '1000'ID_count = 1000frame_ID = '1000'ID_count = 1000if frame_ID == ID_count:  ...

In this case, the if statement is not valid, so you can easily see the error. However, when frame_ID and ID_count are obtained from other variables, the two types are not so clear, so pay attention to these small details during programming;

Similarly, when storing files, for example:

str_name_FID = str(frame_ID_list_np_save[count_save_frame_np])cv2.imwrite('image/' + video_name_save + '_' + str_name_FID + '.jpg',frame) #save the imagecount_save_frame_np = count_save_frame_np + 1

When you store an image, the image name must be string of the str type. count_save_frame_np is an int type variable. If you directly use this variable in a program as part of the image name, it will not save any files, so the first line of strong conversion is required, first convert it to the str type, then use it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.