The basic method of tuples PYTHON

Tuples, tuples are immutable, repeatable set, ordered list of elements, and a list of different, variable sequence list, and tuples are immutable, which means that the list of tuples ratio more space-saving.

'''初始化一个元组'''
a=tuple()
a=()
a=(1,)#如果初始化的时候元组内只有一个元素,在元素后需要添加逗号,否则不是元组类型。
b=list(a)#元组和列表转换
a=tuple(b)

Tuple which way to do that:
tuple.count (),
. Tuple index ()
shame ah only two, and very familiar, list and string types also have these two methods.

names=('alex','bob','bob','james','tode','str','int','float')
print(names.count('bob'))#计数‘bob’元素==》2
print(names.index('bob'))#获得下标==》1

If you think there is any way to sort it tuple:
First: after converting to sort the list in the conversion back.
Second: with the built-sorted () function, but also the function returns a list, who told the list is a collection of very casual yet.
Here Insert Picture Description

Published 23 original articles · won praise 5 · Views 374

Guess you like

Origin blog.csdn.net/weixin_43287121/article/details/105134030