3. Acquaintance list of Python

list

This day Ergou has been stopped several small fry

Small fry mouth and asked, what is the python list

Ergou rejoicing the heart:

列表由一系列按特定顺序排列的元素组成

在Python中,用方括号([])来表示列表,并用逗号来分隔其中的元素

就比如你们几个的名字用列表就可以表示成:

names = ['小混混1', '小混混2', '小混混3', '小混混4', '小混混5'] 
print(names)

['小混混1', '小混混2', '小混混3', '小混混4', '小混混5']

image.png

image.png

Small fry suddenly said: "Well, I am going to let you join us."

Ergou still immersed in the world list, mouth and said:

这就涉及到列表中添加元素了

names = ['小混混1', '小混混2', '小混混3', '小混混4', '小混混5'] 
print(names)

# 二狗加入小混混组
names.append('二狗')
print(names)

这样就是
['小混混1', '小混混2', '小混混3', '小混混4', '小混混5']
['小混混1', '小混混2', '小混混3', '小混混4', '小混混5', '二狗']

这样我就是最小的一个,但是如果你想让我当个大哥,那也不是没办法,你要是想让我当老三

names.insert(2, '二狗')
print(names)

您也别疑惑,这不是说我想当老二,因为您是大哥您是0啊,没有您谁都开不了头,您始终是最前那一个

Ergou think this is a good ass shot, complacent.

image.png

image.png

image.png

image.png

Small fry a moment, that two dogs are playing himself, said: "roll"

Ergou is ungrateful: continued:

这样的话那就涉及到了列表的删除了

如果按照之前的我是最后一个加入的,所以肯定排在最后面,那么你就可以直接把我踢出去

print(names.pop())
print(names)

这样您还能知道您把我踢出去了
二狗
['小混混1', '小混混2', '小混混3', '小混混4', '小混混5']

但是现在不一样啊,我是老三,那么你就需要像按照插入那样踢我了

print(names.pop(2))
print(names)

二狗
['小混混1', '小混混2', '小混混3', '小混混4', '小混混5']

当然了一般黑社会那都是直接不留名的

del names[2]
print(names)

当然啦,咱们可不是什么黑社会,

哦,对啦,大哥您日理万机,有时候忘了我排第几了

没问题,你这样

names.remove('二狗')
print(names)

image.png

image.png

image.png

另外还有一些办法,比如你想看我们现在有多少人

print(len(names))

你想给我们的辈分重新拍一次,这个就是以后永远的长幼顺序

# 字母顺序排列的(永久改变)
names.sort()
print(names)
# 字母倒叙排列的(永久改变)
names.sort(reverse=True)
print(names)

或者你只想临时按照身高排一次

# 字母顺序排列的(临时改变,原长幼顺序不变)
print(sorted(names))
print(names)

# 字母倒叙排列的(临时改变,原长幼顺序不变)
print(sorted(names, reverse=True))
print(names)

或者你想让我们倒着顺序
names.reverse() 
print(names)

Note, Reverse () does not refer to the order in which the reverse alphabetical list of elements, but simply reversing the order of the list of elements:


image.png

tuple

tuple can not be called the list, he has a name is occupied by the tuple
he can not be automatically extended length, Once initialized, the length has been determined

Tuples use parentheses, square brackets list.

Tuple create very simple, only need to add elements in parentheses and separated by commas can be.

tup1 = ('a', 'b', ['c', 'd'])
tup2 = (1, 2, [3, 4, 5] )
print(tup1)
print(tup2)
print(tup1[0])
print(tup1[1])
print(tup1[2])
print(tup1[2][0])

输出:
('a', 'b', ['c', 'd'])
(1, 2, [3, 4, 5] )
a
b
['c', 'd']
c

image.png

When the time is only one element in the tuple is not tup = (222) but tup = (222,) need to add a comma,
because only a (222) It is difficult to distinguish between a bracket or a tuple

Of course, tuple can not not extended length, it can be spliced

tup1 = ('a', 'b', ['c', 'd'])
tup2 = (1, 2, [3, 4, 5] )

tup3 = tup1 + tup2

print(tup3)

What's the answer? You need to run the program look

Some commonly used operational tuples

Python expression result description
as ((1, 2, 3)) 3 Calculating the number of elements
(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) connection
('Hi!',) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!') copy
3 in (1, 2, 3) True Element exists
for x in (1, 2, 3): print x 1 2 3 Iteration

Of course, there are many commonly used method, after learning of the presentation will be 11 out of

Guess you like

Origin www.cnblogs.com/hcf-fcl/p/11199538.html