Python数据的处理

一.字符串拼接的几种方式

  1. 使用str.join()方法进行拼接字符串
  2. 直接拼接
  3. 使用格式化字符串进行拼接
python 复制代码
​
s1='hello'
s2='world'
#(1)使用➕进行拼接
print(s1+s2)
#(2)使用字符串的join()方式
print(''.join([s1,s2]))
print('*'.join([s1,s2]))
print('你好'.join([s1,s2]))
#(3)直接拼接
print('hello''world')
#(4)使用格式化字符串拼接
print('%s%s' % (s1,s2))
print(f'{s1}{s2}')
print('{0}{1}'.format(s1,s2))


​

二.字符串的去重操作

python 复制代码
s='helloworldhelloworldhelloworld'
#(1)字符串的拼接及not in
new_s=''
for item in s:
    if item not in new_s:
        new_s+=item
print(new_s)
#(2)使用索引+not in
new_s2=''
for i in range(len(s)):
    if s[i] not in new_s2:
        new_s2+=s[i]
print(new_s2)
#(3)通过集合去重+列表排序
new_s3=set(s)
lst=list(new_s3)
lst.sort(key=s.index)
print(''.join(lst))
相关推荐
牛奔4 小时前
Go 如何打印调试深层或嵌套的结构体
开发语言·后端·golang
geovindu5 小时前
go: Iterative Algorithms
开发语言·后端·算法·golang·迭代算法
学逆向的5 小时前
汇编——JCC指令
开发语言·汇编·网络安全
mayaairi5 小时前
JS循环语句深度解析:嵌套for、while与do...while
开发语言·前端·javascript
郭老二6 小时前
【Python】基本语法:装饰器语法糖@
python
kite01216 小时前
Go语言Map深度解析与最佳实践
开发语言·后端·golang
_Jimmy_7 小时前
Agent常用检索器的详细介绍
python·langchain
小柯南敲键盘7 小时前
图片翻译API接入与自动化实现指南
运维·python·自动化
l156469488 小时前
突围!图文混合知识库难以解析,Kimi-K3 原生视觉架构深耕知识工作,DMXAPI 统一接口,缩短项目开发周期
java·大数据·开发语言
旅僧8 小时前
Q-learning(自用)
python·机器学习