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 小时前
五分钟入门 强化学习---Q-Learning算法与实现
人工智能·python·深度学习·算法·机器学习·计算机视觉
weixin_468466854 小时前
Prometheus监控服务部署与实战指南
服务器·后端·python·docker·自动化·prometheus
花酒锄作田4 小时前
[Python]标准库argparse解析命令行参数使用介绍
python
卡次卡次14 小时前
vibecoding起步之注意点:如何做一个聊天机器人
python·ai
Hanniel4 小时前
Python 元类(下):进阶与实战建议
开发语言·python
会编程的土豆5 小时前
Go interface 底层的 itab 到底是什么
开发语言·后端·golang
千纸鹤の脉搏5 小时前
多线程的初步了解---进程与线程
java·开发语言·学习·线程
mONESY5 小时前
Python 字典(dict):从原理到实战,彻底搞懂哈希表核心
python
卡次卡次15 小时前
vibecoding起步之注意点:从零到一:Claude Code 接入飞书文档的完整链路
python
Mikowoo0075 小时前
机器学习_梯度计算
人工智能·python·机器学习