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 ...
随机推荐
- jsp 文件上传操作
文件上传 1:完成一个文件上传的功能 index.jsp 注意更换form表单的enctype enctype就是encodetype就是编码类型的意思. multipart/form-data是指表 ...
- JavaScript for impatient programmers
参考 作者发布的在线HTML版本(包含大部分主要章节,只缺少四个额外章节)——https://exploringjs.com/impatient-js/toc.html 作者的博客——http://2 ...
- nodejs安装及使用步骤详解
就一段小小的时间不用,就忘得差不多了,果然好记性不如乱笔头. 1.必须要安装node环境(建议装在C盘,这是一个系统盘)+安装mongoose数据库 +Robot 3T之于mongodb就相当于so ...
- vue中一些常见错误
一:在抽取路由模块时路径没有更改过来 二:跨域的问题
- 判断当前用户是否在某个SharePoint组内
/// <summary> /// 判断当前登录人是否在sharepoint组中 /// </summary> /// <param name="current ...
- Migration-添加表
public partial class _111111 : DbMigration { public override void Up() { CreateTable( "dbo.Asse ...
- BZOJ 1924 && Luogu P2403 [SDOI2010]所驼门王的宝藏 恶心建图+缩点DP
记住:map一定要这么用: if(mp[x[i]+dx[j]].find(y[i]+dy[j])!=mp[x[i]+dx[j]].end()) add(i,mp[x[i]+dx[j]][y[i]+dy ...
- JPA规范基础 ppt教程
https://wenku.baidu.com/view/5ca6ce6a1eb91a37f1115cee.html
- vue——做了一个幼稚的小页面
我的小花花没有转起来,不开心  ̄へ ̄
- arch安装问题总结
安装 archLinux 的时候遇到的一些问题,记录下来方便以后安装. 1.fcitx 在设置/etc/locale.conf文件时,中文不能写成zh_CN.utf-8,而是要写成zh_CN.utf8 ...