If you want to set the same initial value and desired length
>>> a=[None]*4
>>> print(a)
[None, None, None, None]
If we know the length of the list in advance, we can initialize the list of that length in advance, and then assign values to each list, which will be faster than each time list.append () more efficient.
If you want the sequence initial value, you can use the range function, but note that the range function returns an iterative object, which needs to be converted into a list
>>> b=list(range(10))
>>> print(b)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b=range(10)
>>> print(b)
range(0, 10)
If you want to eliminate unwanted data, you can use the list derivation
>>> c=[i for i in range(10) if i%2==0 and i<8]
>>> print(c)
[0, 2, 4, 6]
Life is short, You need Python~
Read More:
- The problem of “value error: zero length field name in format” in Python 2.6.6 of CentOS 6.9
- Use Python to insert the specified content in the specified line of the specified file
- Python implements inserting content in the specified position of text
- python3 Error in sitecustomize; set PYTHONVERBOSE for traceback: NameError: name ‘modules_list’ …
- Python: How to Fix “Ord() expected string of length 1, but int found”
- 【Bug-python】IndexError: list index out of range
- Python modifies word document content and inserts pictures
- 206. Reverse Linked List [easy] (Python)
- Error in Maven packaging web project: webxml attribute is required (or pre existing WEB-INF)/ web.xml if executing in update)
- python-TypeError: list indices must be integers, not tuple Solution
- Python + selenium drop-down list error message: element not interactive
- Common attributes and methods of list and map in Dar
- The problem that the content extra data in the notification cannot be updated
- JNI calls the add function of list in Java to flash back
- 【R】【unimplemented type ‘list’ in ‘orderVector1’】
- Mmdetection reports an error when running its own coco dataset. Does not match the length of \ ` classes \ ` 80) in cocodataset
- Common errors in k8s initialization_ [WARNING NumCPU]_ [WARNING SystemVerification]_ WARNING Hostname
- How many pieces of data can list store in Java?