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))
相关推荐
Zane19941215 小时前
CAS 与原子类:Java 如何实现无锁编程
java·开发语言
固定资产管理系统软件16 小时前
商务局RFID固定资产管理系统的应用价值与落地要点解析
大数据·python
码农颜16 小时前
6.3 主函数流程剖析
开发语言·php
TheBestRucy16 小时前
Python 九阳神功:从零筑基到线程飞升
服务器·开发语言·网络·人工智能·python
研☆香16 小时前
聊一聊前端的常见字 关键字
开发语言·前端·javascript
lancyu17 小时前
关于多轮对话机器人的上下文Token优化和解决方案
人工智能·python·深度学习·机器学习·chatgpt·机器人·prompt
草莓橙子碗17 小时前
Claude 使用指南
开发语言·python
工具分享17 小时前
陪跑跟品爆单AI选品助手,高效跟品会赔本吗?
人工智能·python
COOLMO研究AI17 小时前
Python 如何实现 AI API 的提示词(Prompt)版本管理与热更新
人工智能·python·prompt
老郑聊AI业财智造18 小时前
Transformer 技术架构与源码分析
人工智能·python·深度学习·语言模型·架构·transformer·软件工程