-
dictionary changed size during iteration The size of the dictionary cannot be changed during iteration.
-
Open the D drive of the computer, and according to the error report directory, find the file that is sorted at the end one by one. Open the file by opening it in Notepad mode, and look for errors on the fifteenth line of the file. As one of the most commonly used data structures in Python, list has similar characteristics to arrays in other programming languages.
Note (Note: tuple tuples are of a similar type to list, but tuple elements cannot be modified; The set set is similar to the list, but the elements in the set are unordered and the duplicate elements are automatically removed). Simply index its subscript with the middle parenthesis [ ], or iterate through all the values with a for loop.
When the subscript is negative, it means from back to front (or understood as length + negative subscript), i.e. -1 represents the last element and -2 represents the second element from the bottom. Dictionary dict. keys().
values() traverses the dict's key and value dict, respectively. The get() function can return the value of the specified key, and if there is no specified key in the dictionary, it will return none, and no error will be reported.
-
To delete a key, use the pop(key) method and the corresponding value will also be removed from the dict.
d =>>>'bob')
75>>>d
-
What can be removed? If you want to iterate through the deletion of the dictionary itself, the answer is yes.
-
Today from the project to introduce this big pit!!
Due to the needs of the work, it is necessary to migrate the data of the old table to the new table, and due to the needs of the project, the table needs to be dynamically created and the data should be migrated (** is distinguished by the project number, and the static method of creating ** is encapsulated in the model class), This static method is actually splicing SQL statements. Because **reached 2000+, I had to migrate the data and ** before going online, and it took nearly a week to finally achieve initial results, but one of them took a day, that is, during the data migration, I encountered the migration of duplicate data from different old tables. I need to get rid of duplicate data, so I have a double-layer list traversal to delete the cnm sinkhole for the same data!
look the demo:
lists = [1,2,3,4,5,6]
for list in lists:
print(lists)
Returns: [2, 4, 6].
Analysis: 1-Problem: Loop lists, want to delete all elements of lists, but actually have data retained, 2-Reason: This is because when looping lists, and deleting the elements of lists, the position of the list has shifted, when deleting "1", the second element "2" fills the position, so that "2" has escaped the fate of being deleted, it is the turn of "3", after "3" is deleted, "4" comes to fill in the position, escapes a catastrophe, and so on!!
3-Expand: 1) The for loop is to determine whether the object is an iterable object first, if not, it will directly report an error and throw a typeerror exception, if so, call the iter or iter method to return an iterator.
2) Continuously call the next or next method of the iterator and return a value in the iterator in order each time.
3) At the end of the iteration, when there are no more elements, an exception stopiteration is thrown, which Python will handle by itself and will not be exposed to the developer.
4- So when looping list, you can't delete your own elements at the same time as looping, but use pop (subscript) deletion.
-
Arrays are marked with indexes, and every time you delete a 3, the array is filled with one digit, for example.
1,3,3], you delete the first 3 (index is 1), then the second 3 will replace the position of the first 3, but the index represented by your first 3 has been traversed, and a 3 will be missed
-
This ** is modified by an exception, and there is no problem with the program.
dict =
x = dict['a']
print(x) Print out the x and you'll be clear.
for i in range(5):
x[i] = 0 This statement assigns print(dict['a'])
-
Because x and dict[.]'a'] all point to the same memory space.
-
Press Specify the number of rows to read pagination to handle well:
Or: def getandremovehead(filename, headlines):
with open(filename, 'rt') as handle:
buffer =
with open(filename, 'wt') as handle:
) return buffer[:headlines] but when encountering a large text file, deleting n lines in it is not an ideal business solution.
-
Operate on this list.
a = [1, 2, 3, 4, 5, 6]
First, modify the element with a value of 3 to 4. We can write it like this.
for i in range(len(a)):
if a[i] == 3:
a[i] = 4
Or so. for i, v in enumerate(a):
if v == 3:
a[i] = 4
It turned out like this:
a = [1, 2, 4, 4, 5]
It seems that the for loop is done quite smoothly, but the modification of the list is not limited to the modification of the value, but also the insertion and deletion.
Moving from the previous step, we are now going to try to remove all the elements with a value of 4 in it.
for i, v in enumerate(a):
if v == 4:
del a[i]
However, to get it is.
a = [1, 2, 4, 5]
If it is written like this.
for i in range(len(a)):
if a[i] == 4:
del a[i]
python directly reported an error.
traceback (most recent call last):
file "", line 2, in
indexerror: list index out of range
Because one of the elements is deleted during the traversal process, the following elements are moved forward as a whole, resulting in one element becoming a fish that slips through the net. Similarly, using an insert operation during traversal will result in a similar error. This is where the question says that you can't "track" the element.
If you use while, you can be flexible in the face of such a situation. The same is the same example above.
i = 0while i < len(a):
if a[i] == 4:
del a[i]
else:i += 1
-
Because the list is a bai mutable object, every time you iterate for loop, if you change the list at the same time, then the data will be changed, and it will also be affected by the genus when iterating on iteration, for example:
As a simple example like this, an element is removed at the same time for each iteration, and the list itself has changed after the iteration.
If in actual development, the data is very important, make a for loop and change the original data, which is a big taboo.
-
Related questions8 answers2024-04-11
Removed the long type, there is now only one integer, int, but it behaves like a version of long >>>More
7 answers2024-04-11First of all, the courseinfo method of the school class above is used to display information. >>>More
3 answers2024-04-11The size of the installation package is different, the function is different, and the nature is different. >>>More
10 answers2024-04-11python install
Add environment variables (the path should be filled in the scripts folder in your own python directory). >>>More
14 answers2024-04-11No, you can solve it yourself through third-party software.