Python(二)列表、元组操作

2017-07-25  本文已影响50人  iYeso

一: 列表

#######1.1: 列表的格式
比C语言的数组强大的地方在列表的元素可以为不同的类型的
变量A的类型为列表

>>> nameList = ["123", "abc", "333", 44, 3.3]
>>> nameList
['123', 'abc', '333', 44, 3.3]

#######1.2: 列表的循环遍历

########1.2.1: 使用for循环

fileNames = ["1.py", "2.txt", "3.oop", "4.xxx", "5.xxxx"]
for value in fileNames:
    print(value)

1.py
2.txt
3.oop
4.xxx
5.xxxx

########1.2.2: 使用while循环

fileNames = ["1.py", "2.txt", "3.oop", "4.xxx", "5.xxxx"]
count = 0
while count < len(fileNames):
    print(fileNames[count])
    count+=1


1.py
2.txt
3.oop
4.xxx
5.xxxx

#######1.3: 添加元素
########1.3.1:append: 追加的形式增加一个元素

fileNames = ["1.py", "2.txt", "3.oop", "4.xxx", "5.xxxx"]
fileNames.append("xxxx")
for value in fileNames:
    print(value)

########1.3.2: extend:讲另外一个集合的元素添加到列表中

testLists = ["111", "3"]
fileNames = ["1.py", "2.txt", "3.oop", "4.xxx", "5.xxxx"]
testLists.extend(fileNames)
for value in testLists:
    print(value)

########1.3.3: insert在某些位置增加数据
insert(index, object)在指定位置index插入元素object

>>> a = [0, 1, 2]
>>> a.insert(0, "m")
>>> a

['m', 0, 1, 2]

#######1.4: 修改数据
修改元素的时候, 通过下标来确定要修改的元素, 才能进行修改

>>> a = ["m", 0, 1, 2]
>>> a[0] = "xxxx"
>>> a
['xxxx', 0, 1, 2]

#######1.5: 查找数据(in, not in, 'index', 'count')
#########1.5.1: int, not in

nameList = ["abc", "123", "ccc"]
if "123" in nameList:
    print("列表中包含")
else:
    print("列表不包含")

#########1.5.2: index, count: 获取列表中某个元素的位置和出现的次数

<1:>
a  = ["a", "b", "c", "d", "a", "c", "v", "a"]
print(a.index("a", 1, 6))
print(a.count("a"))

4
3

<2:>
a  = ["a", "b", "c", "d", "a", "c", "v", "a"]
print(a.index("a", 1, 3))

如果找不到就会崩溃崩溃!!!!!!!
Traceback (most recent call last):
  File "3.py", line 11, in <module>
    print(a.index("a", 1, 3))
ValueError: 'a' is not in list

#######1.6: 删除元素("del", "pop", "remove")
列表元素的常用删除方法有

<1: del>
languageLists = ["swift", "oc", "c", "c++", "python", "java"]
print("删除之前----------------")
for value in languageLists:
    print(value)
del languageLists[1]
print("删除之后----------------")
for value in languageLists:
    print(value)

删除之前----------------
swift
oc
c
c++
python
java
删除之后----------------
swift
c
c++
python
java

<2: pop>

languageLists = ["swift", "oc", "c", "c++", "python", "java"]
print("删除之前----------------")
for value in languageLists:
    print(value)
languageLists.pop()
print("删除之后----------------")
for value in languageLists:
    print(value)


删除之前----------------
swift
oc
c
c++
python
java
删除之后----------------
swift
oc
c
c++
python


<3: remove>

languageLists = ["swift", "oc", "c", "c++", "python", "java"]
print("删除之前----------------")
for value in languageLists:
    print(value)
languageLists.remove("swift")
print("删除之后----------------")
for value in languageLists:
    print(value)

删除之前----------------
swift
oc
c
c++
python
java
删除之后----------------
oc
c
c++
python
java

#######1.7: 排序("sort", "reverse")

list = [1, 20, 3, 4]
list.sort()
print(list)
list.reverse()
print(list)

[1, 3, 4, 20]
[20, 4, 3, 1]

二: 元组

Python的元组和列表类似, 不同之处在于元组的元素不能修改, 元组使用小括号, 列表使用方括号

>>> tuple = ("ac", 11, 22,33)
>>> tuple
('ac', 11, 22, 33)
上一篇 下一篇

猜你喜欢

热点阅读