【每日任务】2022.11.7(List方法+字符串方法)

目录

MySQL部分

Python数据结构算法部分

列表

列表元素操作

列表操作方法

字符串

字符串方法


MySQL部分

# 牛客SQL.21:多表查询
方法一:join多表查询
select qpd.device_id, qpd.question_id, qpd.result
from question_practice_detail as qpd
inner join user_profile as up
on up.device_id=qpd.device_id and up.university='浙江大学'
order by question_id;
方法二:子查询
select device_id,question_id,result
from question_practice_detail
where device_id=(select device_id from user_profile where university='浙江大学')
order by question_id;

# 牛客SQL.22:多表查询+函数使用
方法:join多表查询,distinct
select 
    up.university,  
    count(qpd.question_id)/count(distinct qpd.device_id) as avg_answer_cnt
from question_practice_detail as qpd
left join user_profile as up
on up.device_id = qpd.device_id
group by up.university;

# 牛客SQL.23:多表查询+计算
方法:join多表查询,round(),count(),distinct
select 
    university, 
    difficult_level, 
    round(count(qpd.question_id) / count(distinct qpd.device_id), 4)
from question_practice_detail as qpd
left join user_profile as up on qpd.device_id=up.device_id 
left join question_detail as pd on pd.question_id=qpd.question_id
group by university, difficult_level;

# 牛客SQL.24:多表查询+计算
方法一:join多表查询,round(),count(),distinct
select
    university,
    difficult_level,
    round(count(qpd.question_id)/count(distinct qpd.device_id),4)
from question_practice_detail as qpd
left join user_profile as up on up.device_id = qpd.device_id
left join question_detail as qd on qd.question_id=qpd.question_id
where university='山东大学'
group by difficult_level;
方法二:另一种格式!!!
SELECT 
    t1.university,
    t3.difficult_level,
    COUNT(t2.question_id) / COUNT(DISTINCT(t2.device_id)) as avg_answer_cnt
from 
    user_profile as t1,
    question_practice_detail as t2,
    question_detail as t3
WHERE 
    t1.university = '山东大学'
    and t1.device_id = t2.device_id
    and t2.question_id = t3.question_id
GROUP BY
    t3.difficult_level;

# 牛客SQL.25:重复显示
方法:union all
select device_id,gender,age,gpa
from user_profile
where university='山东大学'

union all

select
    device_id, gender, age, gpa
from user_profile
where gender='male'

Python数据结构算法部分

列表

列表元素操作

# 列表追加元素
# 方法一:append
a=[1,2,3,4]
a.append(5)
print(a)  # [1, 2, 3, 4, 5]
# 方法二:extend
b = [4,5,6]
a.extend(b) #None  extend函数返回值为空
print(a) # [1, 2, 3, 4, 4, 5, 6]
# 方法三:加号连接list,效率比extend低
c = ['a','b','c']
print(a+c)  # [1, 2, 3, 4, 4, 5, 6, 'a', 'b', 'c']

# 列表插入元素
a = [1,2,3]
a.insert(0, 'abcd')  # insert函数没有返回值
print(a)  # ['abcd', 1, 2, 3]

# 列表替换元素
a = [1,2,3]
a[0]='123'
print(a) # ['123', 2, 3]

# 列表删除元素
# 方法一:pop
a = [1,2,3]
a.pop()  # 删除最后一个元素,该方法有返回值,返回被删除的元素值
# 方法二:remove
words=['he','word','ok','yes']
word.remove('he')
print(word)  # ['word', 'ok', 'yes']

列表操作方法

# clear方法:删除列表内容
names=['Bill ','Mary','Jack']
print(names)  # ['Bill ', 'Mary', 'Jack']
names.clear()
print(names)  #[]

# copy方法:重新拷贝
a=[1,2,3]
b=a
b[1]=30
print(a)  # [1, 30, 3]

aa=[1,2,3]
bb=aa.copy
bb[1]=30
print(aa)  # [1, 2, 3]
#  需要注意单纯的赋值操作改变的只是指针,并没有,所以当b变化之后a列表也发生了变化。而赋值操作创建的是一个新的列表,所以改变b之后不会改变a

# count方法:查询列表中指定数据个数
serch=['as','cv','df','as','hg','df',[1,2,3],[1,2,1]]
print(search.count('as'))  # 2
print(search.count([1,2,3]))  # 1

# index方法:查询指定字段在list中的索引
s=['I','love','python']
print(s.index(python))  # 2

# reverse方法:反转list
num=[1,2,3,4,5,6]
num.reverse()
print(num)  # [6, 5, 4, 3, 2, 1]

# sort(reverse=False)方法:排序,默认升序
num=[4,3,5,7,2,8,9]
num.sort(reverse=False)
print(num)  # [2, 3, 4, 5, 7, 8, 9]
#待排序的元素必须是可比较的,字符串和数值类型不能直接比较,否则会抛出异常
#使用sort方法排序,会直接修改原列表,如果想对列表的副本进行排序,可以使用下面的代码
x=[5,3,6,8,9]
y=x[:]
y.sort()
print(x)  # [5, 3, 6, 8, 9]
print(y)  # [3, 5, 6, 8, 9]

# sorted函数:
x=[7,6,4,8,5]
y=sorted(x)
print(y)  # [4, 5, 6, 7, 8]
print(x)  # [7, 6, 4, 8, 5]

#sorted函数可以对任何序列进行排序,例如对字符串排序
print(sorted('esdfgrt'))
#['d', 'e', 'f', 'g', 'r', 's', 't']

字符串

字符串方法

join:字段拼接

# join:字段拼接:str.join(iterable)
# iterable为可迭代对象,一般为list或tuple
list_val = ['www','baidu','com']
str_val = '.'.join(list_val)
print(str_val)  # www.baidu.com
 
tuple = ('User','Li','nb')
str_val = '/'.join(tuple)
print(str_val)  # User/Li/nb

大小写转换

# 大小写转换
str1 = 'Oh,it\'s too nb!'
str2 = str1.upper()  # 方法upper():将小写字母化为大写字母。
str3 = str1.lower()  # 方法lower():将大写字母化为小写字母。
str4 = str1.title()  # 方法title():所有单词的首字母是大写,其余均为小写
str5 = str1.capitalize()  # 方法capitalize():将字符串的第一个字符转换为大写。
str6 = str1.swapcase()  # 方法swapcase():将字符串中大写转换成小写,小写转换成大写。
print(str2,str3,str4,str5,str6,sep = '\n')
# OH,IT'S TOO NB!
# oh,it's too nb!
# Oh,It'S Too Nb!
# Oh,it's too nb!
# oH,IT'S TOO NB!

常用列表检索方法

# 常用检索方法
# 方法一:count():统计字符串里指定子字符出现的次数。(区分大小写)
#        str.count(“被检索的字符串”,检索的起始位置,检索的结束位置)
str = "LoveYouPython"
print(str.count("o"))  # 3
print(str.count("O"))  # 0
# 方法二:find():查找某字符串里是否包含被查询的子字符串
#        str.find(“被检索的字符串”,检索的起始位置,检索的结束位置)
# 注意:如果找到被查询的第一个子字符串,此方法返回该字符串的索引,否则返回-1,代表没有找到该字符串。
str = "LoveYouPython"
print(str.find("o"))  # 1
print(str.find("O"))  # -1
# 方法三:index():查找某字符串里是否包含被查询的子字符串
#        str.index(“被检索的字符串”,检索的起始位置,检索的结束位置)
# 注意:如果找到被查询的第一个子字符串,此方法返回该字符串的索引,否则返回异常,代表没有找到该字符串。
str = "LoveYouPython"
print(str.index("o"))  # 1
# 方法四:startswith():查询字符串是否以指定子字符串开头
str = "LoveYouPython"
print(str.startswith("L"))  # True
print(str.startswith("o"))  # False
# 方法五:endswith():查询字符串是否以指定子字符串结尾
#        str.endswith(“被检索的字符串”,检索的起始位置,检索的结束位置)
str = "LoveYouPython"
print(str.endswith("n"))  # True
print(str.endswith("o"))  # False

字符串分割

# 字符串分割
# 方法一:split():通过指定分割符对字符串进行切片,拆分字符串。(在没有指定分割符时默认空白字符串【空格,\n,\r,\t】切片,切割后是个列表)
#        str.split("分隔符",分割次数)
str = 'Oh,it\'s too nb!'
print(str.split(" ",1))  # ["Oh,it's", 'too nb!']
print(str.split(" ",2))  # ["Oh,it's", 'too', 'nb!']
# 方法二:splitliness():只能通过行界符对字符串进行切片,拆分字符串。(不能指定分割符对字符串进行切片,切割后是个列表)
str = "我 爱\r你\nPy\r\nthon"
print(str3.splitlines())  # ['我 爱', '你', 'Py', 'thon']
# 方法三:partition():从str出现的第一个位置起,把字符串string分成一个3元素的元组。(切割后是个元组)
#        str.partition(string_pre_str,str,string_post_str)
str = "LoveYouPython"
print(str4.partition("o"))  # ('L', 'o', 'veYouPython')

字符串修剪

# 字符串修剪
# 方法一:strip():移除字符串头尾指定的字符(默认为空白或换行符)或字符序列
#        str.strip([chars]) chars:移除字符串头尾指定的字符序列
注意:该方法只能删除开头和结尾的字符,不能删除中间部分的字符
str = " Love Python\n\t\r "
print(str.strip())  # Love Python
# 方法二:lstrip():移除字符串头部指定的字符(默认为空白或换行符)或字符序列
#        str.lstrip([chars]) chars:移除字符串头部指定的字符序列
# 注意:该方法只能删除开头的字符,不能删除中间和结尾部分的字符。
# 方法三:rstrip():移除字符串尾部指定的字符(默认为空白或换行符)或字符序列
#        str.rstrip([chars]) chars:移除字符串尾部指定的字符序列
# 注意:该方法只能删除结尾的字符,不能删除中间和开头部分的字符

参考:Python学习之字符串常用方法_Aaron-ywl的博客-优快云博客_python字符串常用方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Arvee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值