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))
相关推荐
Eiceblue1 小时前
将 Python 列表导出为 Excel 文件:一维、二维、字典列表
开发语言·python·excel·visual studio code
Swizard8 小时前
别再让你的 Python 傻等了:三分钟带你通过 asyncio 实现性能起飞
python
代码or搬砖8 小时前
String字符串
android·java·开发语言
leo__5209 小时前
基于两步成像算法的聚束模式SAR MATLAB实现
开发语言·算法·matlab
Darkershadow9 小时前
python学习之串口通信
python·学习
Macbethad10 小时前
自动化测试技术报告
开发语言·lua
38242782710 小时前
python:输出JSON
前端·python·json
不会画画的画师10 小时前
Go开发指南:io/ioutil包应用和迁移指南
开发语言·后端·golang
2503_9284115610 小时前
12.22 wxml语法
开发语言·前端·javascript
59803541510 小时前
【java工具类】小数、整数转中文大写
android·java·开发语言