Python unlimited element list instance tutorial, python unlimited instance tutorial
The implementation of the 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 using Yield.
The following two sample codes use the Python Yield generator to implement a simple list of infinite elements. (Www.jbxue.com)
1. incrementing infinite list
Code:
Def increment ():
I = 0
While True:
Yield I
I + = 1
For j in increment ():
Print I
If (j> 10): break
2. Fibonacci unlimited list
Code:
Def maid ():
I = j = 1
While True:
Result, I, j = I, j, I + j
Yield result
For k in maid ():
Print k
If (k> 100): break
Adding elements to the list by python
Input is the data input command in Python3; In Python2, the data input from the console is raw_input;
As follows:
Python 3.2.3 (default, Feb 20 2013, 17:02:41)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> A = []
>>> For I in range (3 ):
... A. append (input ("Enter :"))
...
Enter: 1 2 3
Enter: 3 4 5
Enter: 5 6 7
>>>
['1 2 3', '3 4 5', '5 6 7']
>>>
Python 2.7.3 (default, Jan 2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> A = []
>>> For I in range (3 ):
... A. append (raw_input ("Enter :"))
...
Enter: 1 2 3
Enter: 4 5 6
Enter: 7 8 9
>>>
>>>
['1 2 3', '4 5 6', '7 8 9']
>>>
Five Elements in python List a are obtained by user input.
# Coding = UTF-8
MyList = []
For I in range (5)
MyList [I] = input () # each element is a string and needs to be converted to another type.
Print myList