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))
相关推荐
Java后端的Ai之路4 小时前
【Python 教程15】-Python和Web
python
冬奇Lab5 小时前
一天一个开源项目(第15篇):MapToPoster - 用代码将城市地图转换为精美的海报设计
python·开源
灰子学技术7 小时前
go response.Body.close()导致连接异常处理
开发语言·后端·golang
二十雨辰7 小时前
[python]-AI大模型
开发语言·人工智能·python
Yvonne爱编码8 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
Re.不晚8 小时前
JAVA进阶之路——无奖问答挑战1
java·开发语言
你这个代码我看不懂8 小时前
@ConditionalOnProperty不直接使用松绑定规则
java·开发语言
pas1368 小时前
41-parse的实现原理&有限状态机
开发语言·前端·javascript
琹箐8 小时前
最大堆和最小堆 实现思路
java·开发语言·算法
前端摸鱼匠9 小时前
YOLOv8 环境配置全攻略:Python、PyTorch 与 CUDA 的和谐共生
人工智能·pytorch·python·yolo·目标检测