1. Definition list :

names = ['Mo',"Tenglan",'Eric']

The elements in the list are accessed by subscripts , Subscript from 0 Start counting

 >>> names[0]
'Mo'
>>> names[2]
'Eric'
>>> names[-1]
'Eric'
>>> names[-2] # You can also take it upside down
'Tenglan'

2. section : Take multiple elements

 >>> names = ["Alex","Tenglan","Eric","Rain","Tom","Amy"]
>>> names[1:4] # Take the subscript 1 To subscript 4 Number between , Include 1, barring 4
['Tenglan', 'Eric', 'Rain']
>>> names[1:-1] # Take the subscript 1 to -1 Value , barring -1
['Tenglan', 'Eric', 'Rain', 'Tom']
>>> names[0:3]
['Alex', 'Tenglan', 'Eric']
>>> names[:3] # If it's from the beginning ,0 You can ignore , The effect is the same as the sentence
['Alex', 'Tenglan', 'Eric']
>>> names[3:] # If you want to take the last one , Must not write -1, That's the only way to write it
['Rain', 'Tom', 'Amy']
>>> names[3:-1] # such -1 It won't be included
['Rain', 'Tom']
>>> names[0::2] # hinder 2 Represent , Every other element , Just take one
['Alex', 'Eric', 'Tom']
>>> names[::2] # It's the same as the last sentence
['Alex', 'Eric', 'Tom']

3. Additional

>>> names
['Alex', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy']
>>> names.append(" I'm new here ")
>>> names
['Alex', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy', ' I'm new here ']

4. Insert

 >>> names
['Alex', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy', ' I'm new here ']
>>> names.insert(2," Force from Eric Insert in front ")
>>> names
['Alex', 'Tenglan', ' Force from Eric Insert in front ', 'Eric', 'Rain', 'Tom', 'Amy', ' I'm new here '] >>> names.insert(5," from eric Try the new position ")
>>> names
['Alex', 'Tenglan', ' Force from Eric Insert in front ', 'Eric', 'Rain', ' from eric Try the new position ', 'Tom', 'Amy', ' I'm new here ']

5. modify

 >>> names
['Alex', 'Tenglan', ' Force from Eric Insert in front ', 'Eric', 'Rain', ' from eric Try the new position ', 'Tom', 'Amy', ' I'm new here ']
>>> names[2] = " It's time to change "
>>> names
['Alex', 'Tenglan', ' It's time to change ', 'Eric', 'Rain', ' from eric Try the new position ', 'Tom', 'Amy', ' I'm new here ']

6. Delete

 >>> del names[2]
>>> names
['Alex', 'Tenglan', 'Eric', 'Rain', ' from eric Try the new position ', 'Tom', 'Amy', ' I'm new here ']
>>> del names[4]
>>> names
['Alex', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy', ' I'm new here ']
>>>
>>> names.remove("Eric") # Deletes the specified element
>>> names
['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', ' I'm new here ']
>>> names.pop() # Delete the last value in the list
' I'm new here '
>>> names
['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy']

7. Expand

 >>> names
['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy']
>>> b = [1,2,3]
>>> names.extend(b)
>>> names
['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', 1, 2, 3]

8. Copy

 >>> names
['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', 1, 2, 3] >>> name_copy = names.copy()
>>> name_copy
['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', 1, 2, 3]

9. Statistics

 >>> names
['Alex', 'Tenglan', 'Amy', 'Tom', 'Amy', 1, 2, 3]
>>> names.count("Amy")
2

10. Sort & Flip

 >>> names
['Alex', 'Tenglan', 'Amy', 'Tom', 'Amy', 1, 2, 3]
>>> names.sort() # Sort
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < str() #3.0 Different data types can't be sorted together , wipe
>>> names[-3] = ''
>>> names[-2] = ''
>>> names[-1] = ''
>>> names
['Alex', 'Amy', 'Amy', 'Tenglan', 'Tom', '', '', '']
>>> names.sort()
>>> names
['', '', '', 'Alex', 'Amy', 'Amy', 'Tenglan', 'Tom'] >>> names.reverse() # reverse
>>> names
['Tom', 'Tenglan', 'Amy', 'Amy', 'Alex', '', '', '']

11. Get subscript

 >>> names
['Tom', 'Tenglan', 'Amy', 'Amy', 'Alex', '', '', '']
>>> names.index("Amy")
2 # Returns only the first subscript found

Tuples : Tuples are like lists , Also save a set of numbers , It's not just that once it's created , We can't modify , So it's also called read-only list . It's just 2 A way , One is count, One is index

names = ("alex","jack","eric")

notes :python3 in , The list is either all numbers , Or it's all strings

python-- More articles on the use of lists

  1. python Sort the list

    from http://www.iplaypython.com/jinjie/jj114.html reverse() Method Reverse sort the elements in the list , Like the following >>> x = [1,5,2, ...

  2. python list 、 Yuan Zu 、 Dictionaries

    python list   ['a','1','vs2']       The value can be changed python Yuan Zu   ('a','1','css','sdf12')   The value in it can't be changed python Dictionaries   {'s ...

  3. Python list 、 Tuples 、 Common functions of dictionaries and strings

    Python list . Tuples . Common functions of dictionaries and strings One . Tabular method 1.ls.extend(object) To list ls Insert object Every element in ,object Can be a string , Tuples and lists ( character string “abc” in ...

  4. Python list

    python  list Features of lists 1. A list is a variable data type , This is different from tuples 2. The values in the list are ordered , And can store duplicate values , This is followed by set There is a difference 3.python Lists in are similar to arrays in other languages 4 ...

  5. python List builder

    python List builder List generation is List Comprehensions, yes Python Built in very simple but powerful can be used to create list Generative form of . A cycle stay C Language and other languages ,for It's like this ...

  6. [ Reprint ] Python list (list)、 Dictionaries (dict)、 character string (string) Summary of common basic operations

    Create a list of sample_list = ['a',1,('a','b')] Python The list of operations sample_list = ['a','b',0,1,3] Get a value in the list value_star ...

  7. Python How a list gets the subscript of a specified element

    In the use of Python3.4 Read txt Data to list , Due to coding problems , The data read usually appears '\ufeffX'(x For the data you want ). At this time, if you need to convert the data in the list from string to numerical data for data analysis, you will get ...

  8. python Common operation methods of list

    It mainly introduces Python Middle list (List) How to operate , Include creation . visit . to update . Delete . Other operations, etc , Friends in need can refer to . 1. Create a list of . Just enclose the different data items separated by commas in square brackets List = [ ...

  9. !! Yes python List learning sorting list and array detailed introduction

    1.Python There are three types of arrays :( Detailed see http://blog.sina.com.cn/s/blog_6b783cbd0100q2ba.html) (1) list Ordinary linked list , After initialization, you can use the special ...

  10. ( Reprint )Python list (list) operation

    ( Reprint )http://blog.csdn.net/facevoid/article/details/5338048 Create a list of sample_list = ['a',1,('a','b')] Python ...

Random recommendation

  1. iOS note

    1. Use assertion NSAssert() Debugger error NSAssert() It's just a macro , Used in debugging programs in the development phase Bug, By providing NSAssert() Pass a conditional expression to determine whether it belongs to Bug, If the condition is satisfied, return the true value , The program continues ...

  2. Django Environment building

    1. Install well Python 2.7.10 2. Download decompression Django Django-1.9.2.tar.gz cmd cd Go to the decompression Directory (***) python setup.py install 3. Check if ...

  3. zoj3882 game

    I misunderstood the title , Muddleheaded A 了 . In fact, it is the first to win . #include<stdio.h> int main() { int n; while(scanf("%d",&am ...

  4. close ehcache Update check for UpdateChecker

    ehcache Automatic update check is on by default , In an environment where you can't connect to the Internet , There will be anomalies . stay ehcache In the configuration file ehcache Tag with attributes updateCheck="false" that will do .

  5. Black horse programmer _JAVA Basic knowledge summary 2

    ------- android train .java train . Looking forward to communicating with you ! ---------- IDE ---> Itegrity Development Environment Java EE --- ...

  6. CSV File import to SQL Server In the table

    USE  Database name BULK Insert dbo. Table name From 'c:\CSV file name .csv'  WITH (   FIELDTERMINATOR = ',',   ROWTERMINATOR = '\ ...

  7. QEventLoop Two examples of the use of

    Familiar stranger Qt It's event driven , So when you use Qt When , Almost all the time and QEventLoop Dealing with ., It's just that you may not realize : QCoreApplicaton::exec() QApplication ...

  8. 【PHP piece 】 Output method

    php Add... At the beginning :error_reporting(E_ALL & ~E_NOTICE);  // Don't print attention echo: echo “ character string ”;   // It can also be single quotation marks echo $ Variable name ; ech ...

  9. Web Service FAQs

    1. Resources cannot be found , use 58225 When the port publishes the service , There's no way to find Then try another port .http://localhost:58272/ 2. could not load file or assembl ...

  10. SpringCloud Study Directory

    Spring Cloud Built directly on Spring Boot The enterprise Java In terms of innovative methods , It simplifies distribution by implementing proven patterns . Microservice style architecture , So as to bring flexibility to your micro service . Reliability and coordination . The above comes from spri ...