python--009 sequence

Source: Internet
Author: User

Sequence

• List, meta-ancestor, string

Common:

1. Each element can be indexed by index
2. The default index starts from 0
3, can be fragmented in the form of a set of elements within a certain range
4, many common operators, *,+, In/not in

Data with the above characteristics is a sequence (list, meta-ancestor, String)

• Built-in functions for sequences (BIF)

1 list ()/list (iterator)

Convert an object that can be iterated to a list

a=‘Hello‘print (list(a))#[‘H‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘]

2, tuple ()/tuple (iterator)
Convert an object that can iterate to Ganso

  a=‘Hello‘  print (tuple(a))#(‘H‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘)

3, str (object): Oject converted to string

print str(10)#字符串10

4. Len (...): Returns the length of a parameter

    a=‘Hello‘    print len(a) #5

5, Max (): Returns the maximum value in a sequence or parameter set

    l=[1,2,3,4,5]    print max(l)  #5

6, MIN (): Returns the sequence or set minimum value

    l=[1,2,3,4,5]  print min(l)  #1   l=[‘a‘,‘b‘,‘c‘]   print max(l)#c ASCII 对应数值大l=[1,2,3]l.append(‘a‘) print l  #[1, 2, 3, ‘a‘]print min(l) #1print max(l) #a的编码表为97  返回a  这个比较在python2 可以,如果python3不同类型比较报错

7. SUM (iterator[,start=0]): Returns the sum of the iterated objects and optional parameters

a=[1,2,3]print sum(a) #6print sum(a,10) #16

8, Sorted (): sort () similar to: sorting

a=[3,1,2]print sorted(l) #[1,2,3]

9, reversed (): reverse () Reverse Iteration Object

a=[4,5,3,8,1]print reversedlist(reversed(sorted(a)))print a  #[8,5,4,3,1]#如果直接reversed(sorted(a))不行 这是可迭代对象 必须加list

10. Enumerate (): Returns the progenitor sequence (subscript, Element) to iterate over the object

l=[‘a‘,‘b‘,‘c‘]print list(enumerate(l))#[(0, ‘a‘), (1, ‘b‘), (2, ‘c‘)]

11, Zip (): Return to Ganso after packing

a=[1,2,3,4,5,6,7,8]b=[4,5,6,7,8]print list(zip(a,b))#[(1, 4), (2, 5), (3, 6), (4, 7), (5, 8)]a=[1,2,3,4,5,6,7,8]b=[‘a‘,‘b‘,‘c‘,‘d‘]print list(zip(a,b))#[(1, ‘a‘), (2, ‘b‘), (3, ‘c‘), (4, ‘d‘)]

Complementary concepts:
Sequence: Has 4 properties
Iterations: Activities that repeat the feedback process, such as a For loop, are iterative
Iterative objects: Multiple elements have been used to iterate over objects containing sequences

可迭代对象>序列>元祖,字符串,列表

python--009 sequence

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.