19道Python列表和元祖的练习题

19道Python列表和元祖的练习题

1.创建一个空列表,命名为names,往里面添加old_driver,rain,jack,shanshan,peiqi,black_girl元素

names = ["old_driver","rain","jack","shanshan","peiqi","black_girl"]
print(names)

2.往names列表里black_girl前面插入一个alex

names.insert(names.index("black_girl"),"alex")
print(names)

3.把shanshan的名字改成中文,张三

names[names.index("zhangsan")] = "张三"
print(names)

4.往names列表里rain的后面插入一个子列表,[“oldboy”,”oldgirl”]

names.insert(names.index("rain")+1,["oldboy","oldgirl"])
print(names)

5.返回peiqi的索引值

print(names.index("peiqi"))

6.创建新列表[1,2,3,4,2,5,6,2],合并入names列表

names.extend([1,2,3,4,2,5,6,2])
print(names)

7.取出names列表中索引4-7的元素

print(names[4:7])

8.取出names列表中索引2-10的元素,步长为2

print(names[2:10:2])

9.取出names列表中最后3个元素

print(names[-3:])

10.循环names列表,打印每个元素的索引值,和元素

枚举

for index,name in enumerate(names):
    print("%s. %s" % (index,name)) 

计数

index = 0
for name in names:
    print("%s. %s" % (index, name))
    index += 1

11.循环names列表,打印每个元素的索引值,和元素,当索引值为偶数时,把对应的元素改为-1

for index,name in enumerate(names):
    if index % 2 == 0:
        names[index] = -1
print(names)

12.names里有3个2,请返回第2个2的索引值。不要人肉数,要动态找(提示,找到第一个2的位置,在次基础上再找第2个)

print(names.index(2,names.index(2)+1))

13.查找列表(或元祖或字典)中元素,移除每个元素的空格,并查找以 a 或 A 开头 并且以 c 结尾的所有元素

li = ["alec", " aric", "Alex", "Tony", "rain"]
tu = ("alec", " aric", "Alex", "Tony", "rain")
dic = {"k1": "alex", "k2": " aric","k3": "Alex", "k4": "Tony"}
li = ["alec", " aric", "Alex", "Tony", "rain"]
 
for S in li:
    S = S.strip()
    prefix_a = S.startswith("a")
    prefix_A = S.startswith("A")
    suffix_c = S.endswith("c")
    if prefix_a or prefix_A and suffix_c:
        print("要查找的元素:%s" % S)
 
tu = ("alec", " aric", "Alex", "Tony", "rain")
 
for S in tu:
    S = S.strip()
    prefix_a = S.startswith("a")
    prefix_A = S.startswith("A")
    suffix_c = S.endswith("c")
    if prefix_a or prefix_A and suffix_c:
        print("要查找的元素:%s" % S)
 
 
dic = {"k1": "alex", "k2": " aric","k3": "Alex", "k4": "Tony"}
 
for S in dic.values():
    S = S.strip()
    prefix_a = S.startswith("a")
    prefix_A = S.startswith("A")
    suffix_c = S.endswith("c")
    if prefix_a or prefix_A and suffix_c:
        print("要查找的元素:%s" % S)

14.写代码,有如下列表,按照要求实现每一个功能

li = ["alex","eric","rain"]

计算列表长度并输出

li = ["alex","eric","rain"]
v = len(li)
print(v)

列表中追加元素 “seven”,并输出添加后的列表

li = ["alex","eric","rain"]
li.append("seven")
print(li)

请在列表的第 1 个位置插入元素 “Tony”,并输出添加后的列表

#Python学习交流群:531509025

li = ["alex","eric","rain"]
li.insert(0,"Tony")
print(li)

请修改列表第 2 个位置的元素为 “Kelly”,并输出修改后的列表

li = ["alex","eric","rain"]
li[1] = "Kelly"
print(li)

请删除列表中的元素 “eric”,并输出修改后的列表

li = ["alex","eric","rain"]
li.remove("eric")
print(li)

请删除列表中的第 2 个元素,并输出删除的元素的值和删除元素后的列表

li = ["alex","eric","rain"]
v = li.pop(1)
print(v,li)

请删除列表中的第 3 个元素,并输出删除元素后的列表

li = ["alex","eric","rain"]
li.pop(2)
print(li)

请删除列表中的第 2 至 4 个元素,并输出删除元素后的列表

li = ["alex","eric","rain","rain","rain"]
del li[1:4]
print(li)

请将列表所有的元素反转,并输出反转后的列表

li = ["alex","eric","rain"]
li.reverse()
print(li)

请使用 for、len、range 输出列表的索引

li = ["alex","eric","rain"]
for i in range(len(li)):
    print(i)

请使用 enumrate 输出列表元素和序号(序号从 100 开始)

li = ["alex","eric","rain"]
for i,v in enumerate(li,100):
    print(i,v)

请使用 for 循环输出列表的所有元素

li = ["alex","eric","rain"]
for i in li:
    print(i)

15.写代码,有如下列表,请按照功能要求实现每一个功能

li = ["hello", "seven", ["mon", ["h", "kelly"], "all"], 123, 446]

请根据索引输出“Kelly”

li = ["hello", "seven", ["mon", ["h", "kelly"], "all"], 123, 446]
print(li[2][1][1])

请使用索引找到”all”元素并将其修改为“ALL”,如:li[0][1][9]...

li = ["hello", "seven", ["mon", ["h", "kelly"], "all"], 123, 446]
li[2][2] = "ALL"
print(li)

16.写代码,有如下元祖,请按照功能要求实现每一个功能

tu = ("alex","eric","rain")

计算元组长度并输出

#Python学习交流群:531509025
tu = ("alex","eric","rain")
v = len(tu)
print(v)

获取元组的第 2 个元素,并输出

tu = ("alex","eric","rain")
v = tu[1]
print(v)

获取元组的第 1­‐2 个元素,并输出

tu = ("alex","eric","rain")
v = tu[0:2]
print(v)

请使用 for 输出元组的元素

tu = ("alex","eric","rain")
for i in tu:
    print(i)

请使用 for、len、range 输出元组的索引

for i in range(len(tu)):
    print(i)

请使用 enumrate 输出元祖元素和序号(序号从 10 开始)

tu = ("alex","eric","rain")
for i,v in enumerate(tu,10):
    print(i,v)

17.有如下变量,请实现要求的功能

tu = ("alex", [11, 22, {"k1": "v1", "k2": ["age", "name"], "k3": (11,22,33)}, 44])
  • 讲述元祖的特性

元组的一级元素不可被修改增加删除,有序,可迭代,可切片,可索引,可转换为列表。

  • 请问 tu 变量中的第一个元素 “alex” 是否可被修改?

不可被修改

  • 请问 tu 变量中的”k2″对应的值是什么类型?是否可以被修改?如果可以,请在其中添加一个元素 “Seven”
tu = ("alex", [11, 22, {"k1": "v1", "k2": ["age", "name"], "k3": (11,22,33)}, 44])
v = tu[1][2]["k2"]
v.append("Seven")
print(tu)
  • 请问 tu 变量中的”k3″对应的值是什么类型?是否可以被修改?如果可以,请在其中添加一个元素 “Seven”
k3对应的是元组,不可修改

18.现有商品列表如下:

products = [ ["iphone8",6888], ["MacPro", 14800], ["小米6",2499], ["coffee",31],["book",80],["Nike shoes",799]]

请打印出这样的格式:

-----------商品信息 ------------
0. iphone8 6888
1. MacPro 14800
2. 小米6 2499
3. coffee 31
4. book 80
5. Nike shoes 799
products = [ ["iphone8",6888], ["MacPro", 14800], ["小米6",2499], ["coffee",31],["book",80],["Nike shoes",799]]
print("商品信息".center(28,"-"))
for index,product in enumerate(products):
    print("%s.%s %s" %(index,product[0],product[1]))
    # print("{0}.{1} {2}".format(index,product[0],product[1]))

19.利用上题中的列表,写一个循环,不断的问用户想买什么,用户选择一个商品标号,就把对应的商品添加到购物车里,最终用户输入q退出时,打印购物车里的商品列表。

products = [ ["iphone8",6888], ["MacPro", 14800], ["小米6",2499], ["coffee",31],["book",80],["Nike shoes",799]]
 
shopping_cart = []  # 定义一个空的购物车
 
exit_flag = False
 
# while True:
while not exit_flag:
    print("商品信息".center(28, "-"))
    for index,product in enumerate(products):
        print("%s. %s %d" % (index,product[0],product[1]))
 
    product_choice = input("
请输入商品标号:
")
    if product_choice.isdigit(): #判断输入的字符串是否只包含数字
        product_choice = int(product_choice)  # 字符串转成整型
        if product_choice >= 0 and product_choice < len(products):
            shopping_cart.append(products[product_choice][0]) #增加到购物车列表
            print("
商品 %s 已添加到购物车
" % (products[product_choice][0]))
        else:
            print("商品标号有误,请重新输入")
    elif product_choice == "q":
        if len(shopping_cart) > 0:
            print("
您添加到购物车的商品如下:
")
            for index,product_cart in enumerate(shopping_cart):
                print("%s. %s" % (index,product_cart))
        else:
            print("
您的购物车为空!
")
        # break
        exit_flag = True #为真时候结束循环
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » 19道Python列表和元祖的练习题