python基础学习05

python基础学习05

1. 列表的嵌套使用

1.1 保存5个学生3门课的成绩
  • 如下:
语文数学英语
张飞676645
关羽908933
诸葛457867
赵云346723
王亮235678
  • 代码实现
import random

names = ['关羽', '诸葛', '王亮', '赵云', '张飞']
courses = ['语文', '数学', '英语']
scores = [[random.randint(50, 100) for _ in range(3)] for _ in range(5)]

for i, name in enumerate(names):
    for j, course in enumerate(courses):
        print(f'{name}{course}成绩:\t{scores[i][j]}')

    print(f'{name}的平均成绩{sum(scores[i]) / len(courses):.1f}')

for j, course in enumerate(courses):
    temp = [scores[i][j] for i in range(len(names))]
    # print(temp)
    print(f'{course}的最高分:{max(temp)}')
    print(f'{course}的最低分:{min(temp)}')
1.2 给列表元素去重
  • 方法一,先进行排序,再相邻两个比较
nums = [1, 2,2,2 ,3, 5, 3, 2, 3,5,23,23,2]
nums.sort()
temp=[]
for i in range(len(nums)-1):
    if nums[i]!=nums[i+1] :
        temp.append(nums[i])

temp.append(nums[-1])
nums=temp
print(nums)
  • 方法二,放在集合里面直接去重,但无法保存元素是否有序,但可以利用sort(key=L.index)方法不改变元素的顺序
nums = [1, 2,2,2 ,3, 5, 3, 2, 3,5,23,23,2]

temp=[]
for num in nums:
    if num not in temp:
        temp.append(num)
print(temp)
  • 方法三,建立一个临时列表,判断元素是否再临时列表中,如果不在就放入临时列表中最后将临时列表付给需要去重列表
nums = [1, 2,2,2 ,3, 5, 3, 2, 3,5,23,23,2]

nums2=list(set(nums))
nums2.sort(key=nums.index)
print(nums2)

2. 元组

2.1 元组的定义
  • 元组是一个不可变的数据类型
# 定义一个一元组
tuples = ('hell',)
fruits1 = ('apple', 'banana', 'pitaya')
print(type(fruits1))  # <class 'tuple'>

2.2 元组的运算
# 重复运算
print(fruits1 * 3)

# 合并运算
fruits2 = ('litchi', 'grape')
print(fruits2 + fruits1)

# 切片和索引
print(fruits1[::-1])
print(fruits1[:3])
print(fruits1[1:2])  # ('banana',)


# 成员运算
print('apple' in fruits1)

# 不支持修改
# 'tuple' object doesn't support item deletion
# del  fruits1[0]
2.3 元组的解unpack/pack
# ValueError: too many values to unpack (expected 3)
# a,b,c=1,23,42,534

# TypeError: cannot unpack non-iterable int object
# a,b=1

a, *b, c = 1, 3, 5, 4, 5   # 对于a,c以unpake,*b以pake,并以列表返回
print(a)
print(b, type(b))  # [3, 5, 4] <class 'list'>
print(c)

3. 字符串

3.1 初步感知字符串
a='hello,world'
# ''''''可以实现折行,'' 不可以折行
b='''
明天你是否想起
曾经最爱哭的你!
'''
print(a)
print(b)
# '\'转义字符
a='\'add\''
b='"ddff"'
c="'ddff'"
d="\"sidhi\""
print(b)
print(a)
print(c)
print(d)
3.2 字符串的常用方法
  • 字符串和元组一样时不可改变的,只能读写不能改变元素
s1 = 'hello,world!'
# 遍历字符串中每一个元素
for i in s1:
    print(i,end='')
print()


# s1[0] = 'H'  # 'str' object does not support item assignment

s2='tian,yu,ping!'
# 同上面一样
for i in range(len(s2)):
    print(s2[i],end='')

print()
# 重复运算
a = 'I'*50
print(a)
# 成员运算
print('i' in a)
print('i' not in a)

# 字符串的拼接
s1='holle'
s2=',world!'
s3=s1+s2
print(s3) # hello,world!
  • 判断性质、查找
s = 'hello,world'
# 转换
print(s.lower())
print(s.upper())

# 判断性质
print(s.isdigit())
print(s.isalnum())
print(s.isascii())
print(s.islower())
print(s.startswith('h'))
print(s.endswith('d'))

# 查找
s = 'tianyuping'
print(s.find('yu'))
print(s.rfind('u'))
3.3 字符串的编程练习
  • 实习字符串滚动打印,该程序不能直接运行需要在终端上运行
import time 
import os
context='   好好学习,天天向上!   '
while True:
    os.system('cls') #windowns系统下清屏指令
    print(context)
    time.sleep(1) # 实现停顿
    context = context[1:]+context[0] # 前后拼接

总结

通过对字符串的学习过后,这方便后面我们对文本数据的处理带来一点的便利。在以后的数据中我们大多拿到的数据都是文本类型,需要通过字符串的方法来处理数据。

语录

学习的过程是可能需要付出很多心血, 但坚持是成为一个优秀数据分析师必然要经历的过程。
数据分析的学习之路, 并不是学的越多越好, 而是要将学习的方向明确好, 再使正确的力, 这样才可以在正确的方向 做正确的事。
码字不易, 求个赞哈,

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值