Python Iteration and List Builder

Source: Internet
Author: User
Tags iterable

Using a For loop to traverse list and tuple, this traversal becomes an iterative

In the C language is the subscript to get the value, for...in this way is actually the same.

In the section of the function, this says---> ' Sum function sum (), sum (Iterable,start), the first parameter must be an object that can be iterated, listtuple. ' Iterative objects are list,tuple

So when using for traversal, the object is to be iterated.

How do I tell if an object can iterate?

The iterable of Python's collections module can be used to determine whether iterations can be iterated;isinstance()函数用来判断某种数据类型是否是已知的类型

 >>>isinstance (1,int) 
Ture
> >>isinstance (' abc ', str)
Ture
>>>isinstance (3.14,float)
Ture
>>>isinstance ( ' 3.14 ', float)
False
#####################################


from collections import iterable
>>>isinstance ([1,2,3,4,5 ],iterable)
ture
>>> Isinstance (12345,iterable)
false
>>>isinstance ((1,2,3,4,5), iterable)
ture
>>>isinstance ({' Name ': ' JKL ', ' age ': 15},iterable)
ture

So dict can use the for traversal key or values or key,values

Dict is an iterative object, depending on the use of the environment

How do I get a list or a tuple to display subscripts and elements at the same time?

Python provides the enumerate () function, which turns the list or tuple into: index-element pairs, like key-value pairs

t = [1,2,3,4,5] for in enumerate (t)        print(k, '-', V)
0-1
1-2
2-3
3-4
4-5
5-6

List-Generated

To generate an L = [2,4,6,8,10,12,14,...... 100] such a list;

Can be sliced out:

#这里使用list () Convert the number of sequences generated by the range () function to List,tuple also in the same way
>>>l = List (range (1,101= l[1::2]>>>l1[2,4,6,8,10,........]

You can also use functions:

DEF create ():
L = []
For x in range (1,51):
L.append (x*2)
Print (L)
Return

Create ()
[2,4,6,8,10,12,...... 100]

The above code is very troublesome, the use of a list generated by a row can be resolved

 for  in range (1,101) [2,4,6,8,10,..... 100]

The build can also be nested in two for loops, resulting in a full-array

 for inch  for  in range (1,5) [1,2,3,4,2,4,6,8]

Diego represents the up-type

t = (1,2,3,4,5,6 for in t][1,2,3,4,5,6]

Plus other qualifications: A list contains multiple data types, takes out a string and capitalizes the first letter

L = ['grant','Fitz', 15,3.14for in  If  instance (x,str) ['Grant','Fitz  ']

Python Iteration and List Builder

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.