Tuples in Python

Source: Internet
Author: User

To create a simple data structure you can use tuples to store data to create tuples using parentheses

Address = (' Beijing ', ' Shanghai ', ' Tianjin ', ' Guangzhou ', ' Chongqing ')

Even without parentheses, Python can often recognize tuples

Number = 1,2,34,4,5,6,7,8

If you want to create a tuple of 1 values you need to add one later ,(comma)

num = 1, # type = Tuplenum = 1 # type = Intnum = (1) # type = Intnum = (1,) # type = tuple

As with lists, you can use numeric indexes to extract values from elements however, the most common approach is to unpack tuples into a set of variables :

Number = (' Nanxiao ', 23, ' Hebei Zhangjiakou ') name,age,addr = number #元组解包为一组数据rint (name);p rint (age);p rint (addr);

Tuple operations are much the same as lists (indexed tiles, connections) But you cannot modify content after you create a tuple (you cannot replace an element in an existing tuple or insert a new Element) Description: It's a good idea to think of a tuple as a multipart object rather than a different collection in which you can insert or delete items

Tuple modifications must use the slice and join operators :

Name = (' Double gun will bohong ', ' no plume Arrow Zhang Qing ', ' green face Beast Yang Zhiwang ') #不能用insert或者append方法 Add new element name=name[1:]+ (' Vanguard Sosu ',) +name[:1];

Output Result:

(' No feather arrow Zhang Qing ', ' green face Beast Yang Zhiwang ', ' Vanguard Sosu ', ' double gun will Bohong ')

Use the * repeating operator in tuples such as:

8* (4,)

Output Result:

(4,4,4,4,4,4,4,4)

Tuples and lists are typically used when representing data:

filename = ' E:/work.txt ';d atas = [];for line in open (filename): Fileds = Line.split (', '); #将每行划分为一个列表 name = Fileds[0]; #提取并转换每一个字段 tokens = Int (fileds[1]); Price = float (fileds[2]); Stock = (Name,tokens,price); Datas.append (stock);p rint (datas)

Note: Extracts the data in Work.txt and makes up a tuple into the list

Output Result:

[("' Tom '", 132.0), ("' Jon '", 234, 255.0), ("' Jeck '", 123, 678.0)]

If you want to access data items in a tuple:

Print (datas[1]); #索引print (data[2][1]); #切片

Output results

("' Jon '", 234, 255.0)
123

if you want to loop all the records and operate the fields Then:

For name, tokens, price in datas:total + = tokens * Price;print (total);

Output results: 158904.0

Summarize:

1, Create a tuple to use ()

2, Create a tuple of 1 values to use:tuple1 = str, or tuple1 = (str,)

3, commonly used to extract the value of the way slice or index extraction or the tuple is unpacked into a set of variables name, Age,add = date;

5, tuples cannot be modified or added using the general method , but can be done using the slice operator and the join operators

6,* can be used as a repeating operator in tuples

7, using tuples and list usage at the same time

This article is from the "Hong Dachun Technical column" blog, please be sure to keep this source http://hongdachun.blog.51cto.com/9586598/1760241

Tuples in Python

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.