Python basic data type--list

Source: Internet
Author: User

First, create a list:
Li = []li = List () name_list = [' Alex ', ' Seven ', ' eric ']name_list = List ([' Alex ', ' Seven ', ' Eric '])
Second, the basic operation:
#append追加name_list = ["Zhangyanlin", "suoning", "Nick"]name_list.append (' Zhang ') print (name_list) #count制定字符出现几次name _list = ["Zhangyanlin", "suoning", "Nick"]name_list.append (' Zhang ') name_list.append (' Zhang ') name_list.append (' Zhang ') print (Name_list.count (' Zhang ')) #extend可扩展, bulk to riga data name_list = ["Zhangyanlin", "suoning", "nick"]name = ["Aylin "," Zhang "," Yan "," Lin "]name_list.extend (name) print (name_list) #index找到字符所在的位置name_list = [" Zhangyanlin "," suoning ", "Nick"]print (' Nick ') #insert插入, inserting values into the index name_list = ["Zhangyanlin", "suoning", "Nick"]name_ List.insert (1, "Zhang") print (name_list) #pop在原列表中移除掉最后一个元素 and assign to another variable name_list = ["Zhangyanlin", "suoning", "Nick"] Name = Name_list.pop () print (name) #remove移除, only remove the first name_list found from the left = ["Zhangyanlin", "suoning", "Nick"]name_ List.remove (' Nick ') print (name_list) #reverse反转name_list = ["Zhangyanlin", "suoning", "Nick"]name_list.reverse () Print (name_list) #del删除其中元素, remove name_list from 1 to 3 = ["Zhangyanlin", "suoning", "Nick"]del Name_list[1:3]print (name_list)

#join将列表元素用指定字符串连接name_list = ["You", "is", "good"]s = "". Join (name_list) print (name_list)
List source

Iii. data type conversion string to list
s = "Hello Morra" li = List (s) print (LI) output:[' You ', ' good ', ' m ', ' o ', ' R ', ' R ', ' a ']
Tuple goto List
Tu = ("Hello", "alex") Li = List (tu) print (li) output:[' hello ', ' Alex ']
Dictionary goto List
DiC = {' K1 ': ' Hello ', ' K2 ': ' Morra '}l3 = List (dic)              #字典在循环的时候默认只循环keyprint (L3) L4 = List (Dic.values ()) print (l4) L5 = List (Dic.items ()) print (L5) output:[' K2 ', ' K1 ' [' Morra ', ' Hello '] [(' K2 ', ' Morra '), (' K1 ', ' hello ')]
Iv. Possible iterative
L = [' I ', ' am ', ' spark ']# can be iterated by for loop for I in L:    print (i) # I am spark
Five, can be nested
Li = [' String ', (' tuple ', ' hh '), {"Key1": "Value1", "Key2": "value2"}]print (li[2]["Key1"]) #输出  value1

Python basic data type--list

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.