Python求包含数字或字母最长的字符串及长度
一、求包含数字或字母最长的字符串及长度
org = 'ss121*2222&sdfs2!aaabb'
result = [] #保存最终要输出的字符串
result_temp = [] #保存当前最长的字符串
max_len = 0 #保存最长字符串的长度
for c in org + ' ': #多加一次循环,为了最后多执行一次else; 否则若字符串末尾满足条件,将不会保存到result中
if c.isalnum(): #若c为数字或字母,则加入result_temp中
result_temp.append(c)
else: #直到遇到一个非数字和字母时,判断当前result_temp的长度
len_temp = len(result_temp)
if len_temp > max_len: #若大于当前最大长度,则清空result,把该字符串加入reseult中
max_len = len_temp
result.clear()
result.append(''.join(result_temp))
elif len_temp == max_len: #若等于当前最大长度,说明存在两个长度一样的字符串,直接把该字符串加入result中
result.append(''.join(result_temp))
result_temp = [] #遇到非数字和字母时,清空result_temp,继续下一次遍历
if len(result) == 0:
print('没有符合标准的字符串')
else:
print('符合要求的最长字符串的长度为: ', max_len)
print('符合要求的最长字符串有: ', result)
二、求包含数字和字母最长的字符串及长度
org = 'ss121*2222&sdfs2!aaabb'
result = [] #保存最终要输出的字符串
result_temp = [] #保存当前最长的字符串
max_len = 0 #保存最长字符串的长度
for c in org+' ':
if c.isalnum(): #若字符是字母或者数字,则保存
result_temp.append(c)
else:
len_temp = len(result_temp)
result_temp_str = ''.join(result_temp)
if not result_temp_str.isalpha() and not result_temp_str.isdigit(): #若字符串不全为字母或全为数字,则该字符串一定同时包含字母和数字
if len_temp > max_len:
max_len = len_temp
result.clear()
result.append(result_temp_str)
elif len_temp == max_len:
result.append(result_temp_str)
result_temp = []
if len(result):
print('最长的字符串为: ', result, ' 长度为: ', max_len)
else:
print('没有满足要求的字符串')
三、另一种思路
1、现将字符串中所有非数字和字母的特殊字符替换为统一的一个特殊字符
2、将字符串进行分割
3、用两个list,分别保存包含数字和字母的字符串及其长度
4、遍历保存长度的list,提取值最大的下标,从而从保存字符串的list中取出对应的字符串
import string
str1 = 'ss121*2222&sdfs2!aaabb'
t = string.punctuation #获取所有的特殊字符
for c in str1: #替换特殊字符
if c in t:
str1 = str1.replace(c, '.')
list1 = str1.split('.') #分割字符串
list2 = [] #保存所有包含字母和数字的字符串
len_list2 = [] #保存字符串对应的长度
for str2 in list1:
if not str2.isdigit() and not str2.isalpha() and len(str2.strip()) > 1:
list2.append(str2)
len_list2.append(len(str2.strip()))
max_len = max(len_list2)
max_len_count = len_list2.count(max_len)
result = []
if max_len_count > 1: #有多个长度相同的字符串
for length in range(len(len_list2)):
if len_list2[length] == max_len:
result.append(list2[length])
elif max_len_count == 1:
result = list2[len_list2.index(max_len)]
else:
print('没有满足要求的字符串')
exit(0)
print('最长的字符串为: ', result, ' 长度为: ', max_len)
Python求包含数字或字母最长的字符串及长度的更多相关文章
- python 查找字符串同时包含数字和字母的最长子字符串的几种实现方法
有个字符串$sd1#111$svda123!!!221&eSSDSDG,包含特殊字符.数字和字母,输出最长的子字符串和他的长度 例如上面的字符串同时包含数字和字母的字符串是svda123,长度 ...
- 随机生成N个字符(包含数字和字母)
'************************************************************* ' Name: GetRandomString ' Purpose: 随机 ...
- QTP_随机生成N个字符(包含数字和字母)
'************************************************************* ' Name: GetRandomString ' Purpose: 随机 ...
- JS 用正则表达式,验证密码包含数字和字母的方法
必须包含至少一位数字和一位字母,脚本方法如下: function CheckPassWord(password) {//密码必须包含数字和字母 var str = password; if (str ...
- C# 批量生成随机密码,必须包含数字和字母,并用加密算法加密
要求:密码必须包含数字和字母 思路:1.列出数字和字符. 组成字符串 :chars 2.利用randrom.Next(int i)返回一个小于所指定最大值的非负随机数. 3. 随机取不小于chars长 ...
- .net 反射访问私有变量和私有方法 如何创建C# Closure ? C# 批量生成随机密码,必须包含数字和字母,并用加密算法加密 C#中的foreach和yield 数组为什么可以使用linq查询 C#中的 具名参数 和 可选参数 显示实现接口 异步CTP(Async CTP)为什么那样工作? C#多线程基础,适合新手了解 C#加快Bitmap的访问速度 C#实现对图片文件的压
以下为本次实践代码: using System; using System.Collections.Generic; using System.ComponentModel; using System ...
- Python求一个数字列表的元素总和
Python求一个数字列表的元素总和.练手: 第一种方法,直接sum(list): 1 lst = list(range(1,11)) #创建一个1-10的数字列表 2 total = 0 #初始化总 ...
- 记一次拿webshell踩过的坑(如何用PHP编写一个不包含数字和字母的后门)
0x01 前言 最近在做代码审计的工作中遇到了一个难题,题目描述如下: <?php include 'flag.php'; if(isset($_GET['code'])){ $code = $ ...
- Java随机输出验证码包含数字、字母、汉字
//随机验证码,有数字.字符 //生成随机数,然后再截取,还要限定随机数的范围 String zimu = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn ...
随机推荐
- MCP|ZWT|Precision de novo peptide sequencing using mirror proteases of Ac-LysargiNase and trypsin for large-scale proteomics(基于Ac-LysargiNase和胰蛋白酶的蛋白组镜像de novo测序)
一.概述 由于难以获得100%的蛋白氨基酸序列覆盖率,蛋白组de novo测序成为了蛋白测序的难点,由Ac-LysargiNase(N端蛋白酶)和胰蛋白酶构成的镜像酶组合可以解决这个问题并具有稳定性, ...
- uuid安装 插件安装
yum -y install uuid uuid-devel 安装uuid包tar -zxvf uuid-1.6.1.tar.gzcd uuid-1.6.1./configuremakemake in ...
- Leetcode题解
前言 Leetcode现在弄了一个Weekly Contest,然后题目又会作为新题目:感觉如果现在还不及时刷题的话可能真的赶不上它题目增长的速度了.......题目会在博客和Github上同步更新的 ...
- ubuntu 的 python从2.* 升级到3.*
1.在ubuntu 的终端中用代码下载最新的Python sudo apt-get install python3 2.用命令删除usr/bin/目录下的默认python link文件root@sal ...
- Unittest组织用例的姿势
本文我们将会讲解Python Unittest 里组织用例的5种姿势. 环境准备: python 3.0以上 python requests库 小编的环境: python 3.6.4 一.TestLo ...
- js中大数据量form表单卡顿问题解决
转载大神: http://www.mamicode.com/info-detail-1773696.html
- jQuery Plugin Poshy Tip 使用 统一提示信息
项目到了后期,发现前端的提示信息不统一,解决思路如下: 1.回顾系统中tip出现的场景:表单验证提示信息.数据列表中随填随显 2.确定问题域:多条提示信息层叠.信息显示风格不统一 3.结论:找出一款合 ...
- JPA规范基础 ppt教程
https://wenku.baidu.com/view/5ca6ce6a1eb91a37f1115cee.html
- java——super关键字、final关键字、throws关键字、访问控制
super关键字: 当父类被重写之后,子类对象无法访问父类被重写的方法,super就是为了解决这个问题: 1.使用super关键字访问父类的成员变量和成员方法: super.成员变量 super.成员 ...
- 034 Search for a Range 搜索范围
给定一个已经升序排序的整形数组,找出给定目标值的开始位置和结束位置.你的算法时间复杂度必须是 O(log n) 级别.如果在数组中找不到目标,返回 [-1, -1].例如:给出 [5, 7, 7, 8 ...