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))
相关推荐
小成202303202655 小时前
Linux高级02
linux·开发语言
知行合一。。。5 小时前
Python--04--数据容器(总结)
开发语言·python
架构师老Y5 小时前
008、容器化部署:Docker与Python应用打包
python·容器·架构
咸鱼2.05 小时前
【java入门到放弃】需要背诵
java·开发语言
ZK_H5 小时前
嵌入式c语言——关键字其6
c语言·开发语言·计算机网络·面试·职场和发展
A.A呐5 小时前
【C++第二十九章】IO流
开发语言·c++
椰猫子5 小时前
Java:异常(exception)
java·开发语言
lifewange5 小时前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest
pluvium276 小时前
记对 xonsh shell 的使用, 脚本编写, 迁移及调优
linux·python·shell·xonsh
cmpxr_6 小时前
【C】原码和补码以及环形坐标取模算法
c语言·开发语言·算法