0

0

Python程序:输入逗号分隔的字符串

王林

王林

发布时间:2023-09-09 16:25:02

|

7915人浏览过

|

来源于tutorialspoint

转载

python程序:输入逗号分隔的字符串

当输入文本字符串或作为输入给出时,其间可能有逗号。有时,任务是分隔句子或文本字符串的所有逗号分隔部分。这些部分可以具有单个单词或多个单词。这些字符串部分可以作为列表项进一步输入或者可以进一步处理。同样,也需要输入整数形式或小数形式的数字,并用逗号分隔。在这种情况下,将它们理解为数字很重要。本文通过使用四个不同的示例,演示了给定逗号分隔的字符串或句子或数字,并通过 Python 程序理解其逗号分隔结构来处理它的过程。

示例 1 - 输入逗号分隔字符串并使用 split 函数查找逗号分隔部分的程序

算法

第 1 步 - 首先输入一个以逗号分隔的字符串。

步骤 2 - 使用 split 函数将逗号分隔的部分分隔成列表。

第 3 步 - 删除列表项左侧的空格。

立即学习Python免费学习笔记(深入)”;

第 4 步 - 删除列表项右侧的空格。

第 5 步 - 运行程序,然后检查结果。

Mistral AI
Mistral AI

Mistral AI被称为“欧洲版的OpenAI”,也是目前欧洲最强的 LLM 大模型平台

下载

Python 文件包含此

commaSepStr = input ("Enter a comma separated String:")
list1 = commaSepStr.split(",")

def removeLspace(list):
   return [item.lstrip() for item in list]
    
print(commaSepStr)
print(list1)

def removeRspace(list):
   return [item.rstrip() for item in list]

noextraleftspace_list = removeLspace(list1)
noextrarightspace_list = removeRspace(noextraleftspace_list)

print(noextrarightspace_list)
print(*noextrarightspace_list, sep = "\n")

查看结果 - 示例 1

要查看结果,请在 cmd 窗口中运行 Python 文件。

Enter a comma separated String :Our last night plate included two rotis,daal,mixveg, rice, paneer, salad and achaar
Our last night plate included two rotis,daal,mixveg, rice, paneer, salad and achaar
['Our last night plate included two rotis', 'daal', 'mixveg', ' rice', ' paneer', ' salad and achaar']
['Our last night plate included two rotis', 'daal', 'mixveg', 'rice', 'paneer', 'salad and achaar']
Our last night plate included two rotis
daal
mixveg
rice
paneer
salad and achaar

示例 2:输入逗号分隔字符串并使用“for”循环查找逗号分隔部分的程序。

算法

第 1 步 - 首先给出一个以逗号分隔的输入字符串。

第 2 步 - 逐个字符地遍历字符串并识别逗号分隔的部分并将它们附加到列表中。

第 3 步 - 删除列表项左侧的空格。

立即学习Python免费学习笔记(深入)”;

第 4 步 - 打印包含没有多余空格的项目的列表。

第 5 步 - 运行程序,然后检查结果。

Python 文件包含此

commaSepStr = input ("Enter a comma separated String :")

print("The Entered String is: " + commaSepStr)
 
startofItem = 0
list1=[]
for item in range(len(commaSepStr)):
   if commaSepStr[item] == ',':
      # characters from startofItem to comma
      nospaceitem=commaSepStr[startofItem:item].lstrip()
      list1.append(nospaceitem)
      startofItem = item+1
      print(nospaceitem)

# characters from startofItem to end
nospaceitem=commaSepStr[startofItem:].lstrip()        
print(nospaceitem)
list1.append(nospaceitem)
print(list1))

查看结果

打开cmd窗口并运行python文件查看结果。

Enter a comma separated String :Our last night plate included two rotis,daal,mixveg, rice, paneer, salad and achaar
The Entered String is: Our last night plate included two rotis,daal,mixveg, rice, paneer, salad and achaar
Our last night plate included two rotis
daal
mixveg
rice
paneer
salad and achaar
['Our last night plate included two rotis', 'daal', 'mixveg', 'rice', 'paneer', 'salad and achaar']

示例 3 - 输入逗号分隔的整数字符串的程序

算法

步骤 1 - 首先输入一个以逗号分隔且仅包含整数的字符串。

第 2 步 - 使用 split 函数将逗号分隔的整数分隔成字符串列表。

第 3 步 - 从此字符串列表中获取每个项目,并将它们转换为整数类型,并将它们作为整数附加到另一个列表。

第 4 步 - 运行程序,然后检查结果。

Python 文件包含此

# input comma-separated numbers as string 
strInput = input ("Enter comma separated integers: ")
print( "Input string: ", strInput)

# convert to the list
strlist = strInput.split(",")
print("list of string type numbers: ", strlist)

# convert each string element as integers
list1 = []
for item in strlist:
	list1.append(int(item))

# print list as integers
print("list of integers: ", list1)

查看结果 - 示例 3

要查看结果,请在 cmd 窗口中运行 Python 文件。

Enter comma separated integers: 101, 280, 98, 185, 934, 9684, 955, 20, 34
Input string:  101, 280, 98, 185, 934, 9684, 955, 20, 34
list of string type numbers:  ['101', ' 280', ' 98', ' 185', ' 934', ' 9684', ' 955', ' 20', ' 34']
list of integers:  [101, 280, 98, 185, 934, 9684, 955, 20, 34]

示例 4:输入具有十进制数字的逗号分隔字符串的程序

第 1 步 - 首先输入一个以逗号分隔的字符串,并且仅包含整数和小数。

步骤 2 - 使用 split 函数来识别逗号分隔的数字并将它们作为字符串附加到列表中。

第 3 步 - 从此字符串列表中取出每个数字,并将它们转换为浮点类型,并将它们作为小数附加到另一个列表。

第 4 步 - 运行程序,然后检查结果。

Python 文件包含此

# input comma separated numbers as string 
strInput = input ("Enter comma separated numbers: ")
print( "Input string: ", strInput)

# convert to the list
strlist = strInput.split (",")
print("list of string type numbers: ", strlist)

# convert each string element as integers
list1 = []
for item in strlist:
	list1.append(float(item))

# print list as integers
print("list of decimal numbers: ", list1)

查看结果 - 示例 4

打开cmd窗口并运行python文件查看结果。

Enter comma-separated numbers: 102.88, 6.5, 6767.907, 5555.3, 4545, 6677,56.009
Input string:  102.88, 6.5, 6767.907, 5555.3, 4545, 6677,56.009
list of string type numbers:  ['102.88', ' 6.5', ' 6767.907', ' 5555.3', ' 4545', ' 6677', '56.009']
list of decimal numbers:  [102.88, 6.5, 6767.907, 5555.3, 4545.0, 6677.0, 56.009]

图 4:显示具有十进制数字的输入字符串中以逗号分隔的部分的列表。

在这篇 Python 文章中,使用四个不同的示例,给出了如何输入逗号分隔字符串的方法。首先,在示例 1 中,使用 split 函数将字符串的各个部分用逗号分隔。在示例 2 中,通过检查所有字符来遍历字符串来识别逗号分隔的部分。在示例 3 中,整数作为字符串输入,在示例 4 中,十进制数作为输入字符串给出,然后分隔到列表中。

相关文章

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
python开发工具
python开发工具

php中文网为大家提供各种python开发工具,好的开发工具,可帮助开发者攻克编程学习中的基础障碍,理解每一行源代码在程序执行时在计算机中的过程。php中文网还为大家带来python相关课程以及相关文章等内容,供大家免费下载使用。

758

2023.06.15

python打包成可执行文件
python打包成可执行文件

本专题为大家带来python打包成可执行文件相关的文章,大家可以免费的下载体验。

639

2023.07.20

python能做什么
python能做什么

python能做的有:可用于开发基于控制台的应用程序、多媒体部分开发、用于开发基于Web的应用程序、使用python处理数据、系统编程等等。本专题为大家提供python相关的各种文章、以及下载和课程。

761

2023.07.25

format在python中的用法
format在python中的用法

Python中的format是一种字符串格式化方法,用于将变量或值插入到字符串中的占位符位置。通过format方法,我们可以动态地构建字符串,使其包含不同值。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

618

2023.07.31

python教程
python教程

Python已成为一门网红语言,即使是在非编程开发者当中,也掀起了一股学习的热潮。本专题为大家带来python教程的相关文章,大家可以免费体验学习。

1265

2023.08.03

python环境变量的配置
python环境变量的配置

Python是一种流行的编程语言,被广泛用于软件开发、数据分析和科学计算等领域。在安装Python之后,我们需要配置环境变量,以便在任何位置都能够访问Python的可执行文件。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

548

2023.08.04

python eval
python eval

eval函数是Python中一个非常强大的函数,它可以将字符串作为Python代码进行执行,实现动态编程的效果。然而,由于其潜在的安全风险和性能问题,需要谨慎使用。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

579

2023.08.04

scratch和python区别
scratch和python区别

scratch和python的区别:1、scratch是一种专为初学者设计的图形化编程语言,python是一种文本编程语言;2、scratch使用的是基于积木的编程语法,python采用更加传统的文本编程语法等等。本专题为大家提供scratch和python相关的文章、下载、课程内容,供大家免费下载体验。

708

2023.08.11

高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

43

2026.01.16

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Swoft2.x速学之http api篇课程
Swoft2.x速学之http api篇课程

共16课时 | 0.9万人学习

PHP基础入门课程
PHP基础入门课程

共33课时 | 1.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号