Python string, list, tuple, index, slice

Source: Internet
Author: User

I. A brief overview

What is list?1, enclosed in [and], separated by commas each data item 2, data item but the same type of data can also be different types of data (number, string, float) 3, list can have a list as its data item 4, the location of the data item corresponding to index number (index). The default first is 05, an ordered set of data that is string?1, enclosed in single quotes, double quotes, and three quotation marks. 2, the string is a constant can not be modified, its main purpose is to read its elements. What are tuple?1, tuples, and strings are not modifiable. But the way you access them is the same. 2, the definition of a different format: the list is "[]", the string is "" ", the tuple is used" () "3, the use is not the same: tuples for multi-value return (multi-value brought in), the list for the storage of data (because it can be modified) is range?1, Range is a built-in function in Python whose return value is a list; the default step is 1. All the sequence elements in index?1, Python, are numbered, increment from 0, and the elements can be accessed by number. 2. The index of the first element of a sequence is always 0. What if the index is negative? Python starts counting from the right, the last element, and the last element's index is always-13, the string literal (as well as the other sequence literals) can use the index directly, without needing a variable to refer to them 4, if a function returns a sequence, you can also index the result of the return 5, The range of the index {-len (str) to Len (str)-1} is a closed interval

II script case test

#!/usr/bin/env python#coding:utf-8# defining string variables and finding print  "#定义字符串变量" str =  "www.hytyi.com" Print   "STR-&GT;POS-C-&GT;:", Str.index ("C") print  "str->", str, "\ n" #定义空列表的方法print   "#定义空列表的方法" list01  = []print  "List01->long:", Len (list01) print  "List01->type:", type (list01) list02  = list ("www.hytyi.com") print  "LIST02-&GT;:" list02# convert string to list print  "#将字符串转换成列表" list03 =  list (str) print  "LIST03-&GT;:",list03list04 = list03print  "LIST04-&GT;:", List04list04.reverse () print  ' list04<-> ',list04print  "LIST04&NBSP;LONG&NBSP;-&GT;:", Len (list04) #将列表list04以逆序的方式赋值给list05的方式有以下4种:print  "#将列表list04以倒叙的方式赋值给list05的方式有以下几种:" list05 = []i =  0while i < len (list04):         list05.insert (0 , List04[i])         i = i + 1else:         p rint  "LIST05-&GT;1-&GT;:", List05list05 = []i = len (list04) -1while i >=  0:        list05.append (List04[i])          i = i - 1else:        print   "LIST05-&GT;2-&GT;:",list05list05 = list04[::-1]print  "LIST05-&GT;2-&GT;:", List05list04.reverse () list05 = list04print  "LIST05-&GT;3-&GT;:", list05# the square slice in the list print  "# The square slice in the list, if you only want to hytyi. No other, how to do it? "' ' list = [' W ',  ' W ',  ' W ',  '. ',  ' h ',  ' y ',  ' t ',  ' y ',  ' i ',   '. ',  ' C ',  ' o ',  ' m ']index =  0    1     2    3    4    5    6     7    8    9    10   &nbSp;11   12 "print " list02-long-> ", Len (list02) List06 = list02[4:10:1]print   "LIST06-&GT;1-&GT;:",list06list06 = list02[-9:-3:1]print  "LIST06-&GT;2-&GT;:", list06list06 = list02[4:-3:1]print  "LIST06-&GT;3-&GT;:", list06list06 = list02[-3:-9:- 1]print  "List06->4->no->no:", list06# merges all tuples of a one-dimensional list into a single string print  "#将一维列表的所有元组合并为一个字符串" "list02  = [' W ',  ' W ',  ' W ',  '. ',  ' h ',  ' y ',  ' t ',  ' y ',  ' i ',  '. ',   ' C ',  ' o ',  ' m ']str2 =  "www.hytyi.com" ' print list02import itertoolsstr2  =  "". Join (Itertools.chain (list02)) print  "STR2-&GT;NOW-&GT;:", str2# if there are duplicate data items in the list, delete them, leaving only the first one. print  "#如果列表中有重复的数据项, delete it and keep only the first one. "#方法 [1]list08 = list02 * 2print  " "" count list08 ("Y")->: "", List08.count ("Y") print   "LIST08-&GT;:", List08s = list08.count ("Y") -1i = 0while i < s:        list08.remove (' y ')          i += 1print list08.count ("Y") print list08# method [2]list09 =  []list10 = list02 * 2col1 = len (list10) print  "COUNT&NBSP;LIST10-&GT;:", Col1col3 = list10.index ("Y") col2 = 0i = 0while i < col1:         if i == col3 or list10[i] !=  "Y":                 list09.append ( List10[i])         col2 = col2 + 1         i += 1else:        print   "COUNT&NBSP;LIST09-&GT;:", Len (list09)         print  "list09- > ", list09# method [3]list11 = list02 * 2list12 = []i = col4 = 0while i  < col1:        if list11[i] ==  "Y":                 if col4 <  1:                         list12.append (List11[i])                  col4 += 1         else:                 list12.append (List11[i])         i += 1print  " Count list012->: ", Len (list12) print " list12-> ", list12

Iii. display of test results

[email protected] 20141107]# python list.py  #定义字符串变量str->pos-c->: 10str- > www.hytyi.com  #定义空列表的方法list01->long: 0list01->type: <type  ' list ' > list02->: [' W ',  ' W ',  ' W ',  '. ',  ' h ',  ' y ',  ' t ',  ' y ',  ' i ',  ' . ',  ' C ',  ' o ',  ' m '] #将字符串转换成列表list03->: [' W ',  ' W ',  ' W ',  '. ',  ' h ',   ' y ',  ' t ',  ' y ',  ' i ',  '. ',  ' C ',  ' o ',  ' m ']list04->: [' W ',  ' W ',  ' W ',  '. ',  ' h ',  ' y ',  ' t ',  ' y ',  ' i ',  '. ',  ' C ',  ' O ',  ' M ']list04<-> [' m ',  ' o ',  ' C ',  '. ',  ' i ',  ' y ',  ' t ',  ' y ',  ' h ',   '. ',  ' W ',  ' W ',  ' W ']list04 long ->: 13# There are several ways to assign a list list04 to list05 in a flashback way: list05->1->: [' W ',  ' W ',  ' W ',  '. ',  ' h ',  ' Y ',  ' t ',  ' y ',  ' i ',  '. ',  ' C ',   ' O ',  ' m ']list05->2->: [' W ',  ' W ',  ' W ',  '. ',  ' h ',  ' y ',  ' t ',  ' Y ',  ' i ',  '. ',  ' C ',  ' o ',  ' m ']list05->2->: [' W ',  ' W ',  ' W ',  '. ' ,  ' h ',  ' y ',  ' t ',  ' y ',  ' i ',  '. ',  ' C ',  ' o ',  ' m ']list05->3->:  [' W ',  ' W ',  ' W ',  '. ',  ' h ',  ' y ',  ' t ',  ' y ',  ' i ',  '. ',  ' C ' ,  ' o ',  ' m '] #列表中的正方向切片, if only want to hytyi. list02-long-> 13list06->1->: [' h ',  ' y ',  ' t ',  ' y ',  ' i ',  '. ' list06->2->: [' h ',  ' y ',  ' t ',  ' y ',  ' i ',  '. ' list06->3->: [' h ',  ' y ',  ' t ',  ' y ',  ' i ',  '. ' list06->4->no->no: [' C ',  '. ',  ' i ',  ' y ',  ' t ',  ' y '] #将一维列表的所有元组合并为一个字符串 [' W ',  ' W ',  ' W ',  '. ',  ' h ',  ' y ',  ' t ',  ' y ',  ' i ',  '. ',  ' C ',  ' O ',  ' m ']str2-&Gt now->: www.hytyi.com# If there are duplicate data items in the list, delete them, leaving only the first one. count list08 ("y")->: 4list08->: [' W ',  ' W ',  ' W ',  '. ',  ' h ',  ' y ',   ' t ',  ' y ',  ' i ',  '. ',  ' C ',  ' o ',  ' m ',  ' W ',  ' W ',  ' W ',  '. ',   ' h ',  ' y ',  ' t ',  ' y ',  ' i ',  '. ',  ' C ',  ' o ',  ' m ']1[' W ',  ' W ',   ' W ',  '. ',  ' h ',  ' t ',  ' I ',  '. ',  ' C ',  ' o ',  ' m ',  ' W ',  ' W ',   ' W ',  '. ',  ' h ',  ' t ',  ' y ',  ' i ',  '. ',  ' C ',  ' o ',  ' m ']count  list10->: 26count list09->: 23list09-> [' W ',  ' W ',  ' W ',  '. ',   ' h ',  ' y ',  ' t ',  ' I ',  '. ',  ' C ',  ' o ',  ' m ',  ' W ',  ' W ',  ' W ',   '. ',  ' h ',  ' t ',  ' I ',  '. ',  ' C ',  ' o ',  ' m ']count list012->:  23list12-> [' W ',  ' W ',  ' W ',  '. ',  ' h ',  ' y ',  ' t ',  ' I ',  '. ',  ' C ',  ' o ',  ' m ',  ' W ',  ' W ',  ' W ',  '. ',  ' h ',   ' t ',  ' I ',  '. ',  ' C ',  ' o ',  ' m '


This article is from the "Zheng" blog, make sure to keep this source http://467754239.blog.51cto.com/4878013/1574116

Python string, list, tuple, index, slice

Related Article

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.