() function, using Iterators with the next () functionS3 = Sentence (' Pig and Pepper ')it = ITER (S3)Print (IT) #Print (Next (IT)) #PigPrint (Next (IT)) #andPrint (Next (IT)) #Pepper#print (Next (IT)) #StopIterationAfter print (list (it)) #[] The end of the head, the iterator is uselessPrint (ITER (S3)) #[' Pig ', ' and ', ' Pepper '] to iterate again, to rebuild the iteratorThe #因为内置的 iter (...) function
It is often used in development to iterate through the data such as
dictionaries , lists, and so on, but the traversal of a dictionary in Python is unfamiliar to many beginners, and today it is two ways to iterate through a dictionary in Python.
Note: The following two methods are common in Python2 and Python3.
1.
Just learning Python, the individual understanding of "iterative objects" and "iterator objects" doesn't know right.1. Several concepts1. Iterative tools: Includes for loop, list resolution, in membership test ..... And so on, to access the actions (?) of the elements in an iterative object (container) in turn.2. Iterator object: An object with the __next__ () method. The method can automatically return the
position of the variable, forming a dictionaryDef func1 (): a = 2 b = 3 print (Globals ()) print (Locals ()) def inner (): c = 5 d = 6 Print (Globals ()) print (Locals ()) inner () func1 ()def func (): a = 3 b = 4 print (Locals ()) print (Globals ()) func () A = 1b = 2print (Globals ()) print (Locals ())5. Iterative objects and iterators (all objects in Python)The judgment of an iterative obje
In some cases, we need to do something about the list, such as multiplying each element in the list by 2, which is generally the traversal of each element multiplied by 2. Then it will be two lines to write it down. And this will modify the original list, if you want to not modify the original list, there are more than
DescribeThe Extend () function is used to append multiple values from another sequence at the end of the list (the original list is expanded with a new list).GrammarExtend () method syntax:List.extend (seq) Parameters
SEQ--List of elements.
return valueThe method does not return a value, but adds a new
List data types and common operations of python, and list data types of python
A list is the most common Python data type. It can appear as a comma-separated value in square brackets.
Each element in the
Python list traversal and deletion implementation code, python list Code
The list of python can be traversed using a for loop. An error is found during actual development, that is, an error occurs when deleting the
http://blog.csdn.net/lc_lc2000/article/details/53135839The intention is to make a = b,b a list, the result in the subsequent operation of a, resulting in the value of B also changed, only recall that Python is "reference" this word.To avoid this, there are two ways to avoid this post summary.1. Using the list slice A = b[:]2. Deep copy A = Copy.deepcopy (B)The
Python list deduplication method you should know, python list deduplication Method
Preface
List deduplication is a common problem when writing Python scripts, because no matter where the source data comes from, when we convert it
If an element exists in the list, the returned
Task:You have a list L, there is an index number I, if I is a valid index, return l[i], if not, return the default value VSolution:
The list supports bidirectional indexing, so I can be negative >>> def list_get (L,i,v=none): if -len (l) Len (l): return L[i] else : return v >>> list_get ([1,2
Python [2]-list and metadata, python listI. Sequence
Python contains six built-in sequences: List, tuples, strings, unicode strings, buffer objects, and xrange objects.
The list can be modified, and the tuples cannot be modified.I
How to implement an infinite element list in Python, and an infinite element list in python
The example in this article describes how to implement the infinite element list in Python. The specific implementation can be completed u
Python List Operation instance, python list instance
This article describes how to operate python lists. Share it with you for your reference.
The specific implementation method is as follows:
Copy codeThe Code is as follows: class Node:"Single node in a data structure """De
Python list usage, python list
#! Usr/bin/env python#-*-Coding: UTF-8 -*-# The following methods are all run in python2.7.x. Please execute them in the print (put in parentheses) by those above 3. x)# List methods:Li = [] # defin
[Python] -- list, python -- list
The most basic data structure in Python. Each element in the sequence is assigned a number-its location, or index. The first index is 0, the second index is 1, and so on.List 1. Define the list and
Python learning 3: List, tuples, and python learning list1. List: 1. How to define a list:
list1 = [1,2,3,4,"hello","world"]
As shown above, list1 is a list. The content of the list is
Python list deduplication, Python list
Boring statistics on how many methods are available to deduplicate the list.
1. Set
list(set(alist))
To maintain the order:
import randomif __name__=='__main__': a=[random.randint(0,10) fo
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.