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))
相关推荐
zhangjw343 小时前
Java基础语法:变量、数据类型与运算符,从原理到实战
java·开发语言
yaoxin5211236 小时前
384. Java IO API - Java 文件复制工具:Copy 示例完整解析
java·开发语言·python
Greyson16 小时前
Layui表格如何使用第三方插件实现树形展示.txt
jvm·数据库·python
2401_871696526 小时前
mysql行级锁失效的原因排查_检查查询条件与执行计划
jvm·数据库·python
NotFound4866 小时前
实战指南如何实现Java Web 拦截机制:Filter 与 Interceptor 深度分享
java·开发语言·前端
xzal127 小时前
python中,turtle基础知识笔记1
笔记·python·turtle
a9511416427 小时前
CSS如何实现元素隐藏不占位_使用display-none完全移除
jvm·数据库·python
Ava的硅谷新视界7 小时前
用了一天 Claude Opus 4.7,聊几点真实感受
开发语言·后端·编程
rabbit_pro7 小时前
Python调用onnx模型
开发语言·python