Home Backend Development Python Tutorial python实现从字符串中找出字符1的位置以及个数的方法

python实现从字符串中找出字符1的位置以及个数的方法

Jun 16, 2016 am 08:42 AM
python number Location string find out

本文实例主要实现给出任意字符串,获取字符串中某字符的位置以及出现的总次数。

实现该功能代码的时候可以使用函数enumerate来将字符串分离成位置和字符,然后进行比较即可。

具体实现代码如下:

#!/bin/env python
#-*- coding:utf-8 -*-
#
"""
  用enumerate将string中的1都找出来,
  用enumerate实现:
"""
def get_1_pos(string):
  onePos=[]
  try:
    onePos=list(((pos,int(val)) for pos,val in enumerate(string) if val == '1'))
  except:
    pass
  return onePos

def get_1_num(string):
  return len(list(get_1_pos(string)))

def get_char_pos(string,char):
  chPos=[]
  try:
    chPos=list(((pos,char) for pos,val in enumerate(string) if(val == char)))
  except:
    pass
  return chPos
def get_char_num(string,char):
  return len(list(get_char_pos(string,char)))

if(__name__ == "__main__"):
  str0="10101010101010101"
  str1="123abc123abc123abc"
  lt=get_1_pos(str0)
  print(lt)
  lt=get_1_pos(str1)
  print(lt)
  num=get_1_num(str0)
  print(num)
  lt=get_char_pos(str1,'1')
  print(lt)
  num=get_char_num(str1,'1')
  print(num)  
Copy after login

希望本文实例对大家Python程序设计中字符串操作的学习有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

What are the advantages and disadvantages of templating? What are the advantages and disadvantages of templating? May 08, 2024 pm 03:51 PM

What are the advantages and disadvantages of templating?

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step May 06, 2024 pm 03:52 PM

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

Share several .NET open source AI and LLM related project frameworks

A complete guide to golang function debugging and analysis A complete guide to golang function debugging and analysis May 06, 2024 pm 02:00 PM

A complete guide to golang function debugging and analysis

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

How to save the evaluate function How to save the evaluate function May 07, 2024 am 01:09 AM

How to save the evaluate function

See all articles