简体   繁体   English

如何检查一个字符串是否包含2个相同的字符

[英]How to check if a string contains 2 of the same character

Using for loops, while loops, and/or if statements.使用 for 循环、while 循环和/或 if 语句。 How can you find number pairs in a string where the numbers are even indexes如何在数字为偶数索引的字符串中找到数字对

I used if statements which works for a 6 character string but this would take forever if given a large string.我使用了适用于 6 个字符的字符串的 if 语句,但如果给定一个大字符串,这将需要很长时间。 So how can I make this code more efficient?那么我怎样才能让这段代码更有效率呢?

string = '8h8s9c'
if string[0] == string[2] or string[0] == string[4] :
   print('pair')
if string[2] == string[0] or string[2] == string[4] :
   print('pair')
else:
   print('This string contains no pair')

8h8s9c should print pair 8h8s9c 应该打印对

5s7a5k should print pair 5s7a5k 应该打印对

Since we're only worried about even indexes, we can right away cut out all the other characters.由于我们只关心偶数索引,我们可以立即删除所有其他字符。

string = string[::2]

Then check each character we care about (decimal digits) and see if it's in there twice.然后检查我们关心的每个字符(十进制数字),看看它是否在那里两次。

if any(string.count(x)>1 for x in '0123456789'):print('pair')
else:print('This string contains no pair')

Super simple answer in python: python 中的超级简单答案:

string = '8h8s9c'

string = string[::2]; #string = "889", this basically splices the string

string1 = list(string) #string1 = ['8','8','9'], separates into digits

string2 = set(string1) #string2 = {'9','8'} basically all duplicates get eliminated if any. therefore IF there are duplicates, number of elements WILL reduce.

print(len(string1) == len(string2)) # false as 3 != 2
 def func(s):
      l  = list(s)
      for i in s:
           if l.count(i)>1:
                return True
      return False

s = "8h8s9c"
if func(s):
     print("pair")
else:
     print("not pair")

output output

pair

this will give you a dictionary with all pairs and # of occurrences:这将为您提供包含所有对和出现次数的字典:

from collections import Counter
counts = Counter(string)
out = {key:counts[key] for key in counts.keys() if counts[key] > 1}
if out:
    print('Pair')

output: output:

{'8': 2} #output of out
Pair

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 python 中检查字符串是否包含特定字符 - How to check if a string contains a specific character or not in python 如何轻松检查字符串是否包含不应包含在其中的字符? - How do I easily check whether a string contains a character that should not be in there? Python:如何检查unicode字符串是否包含一个cased字符? - Python: How to check if a unicode string contains a cased character? Python:检查字符串是否包含中文字符? - Python: Check if a string contains chinese character? 检查字符串是否包含所需的字符 position python - check if the string contains the desired Character position python 检查字符串是否在python中包含字符 - check whether a string contains a character in python 检查字符串是否只包含python中的某个字符 - Check if string only contains a certain character in python 如何使用 Python 中的 range() 函数和布尔值检查字符串是否包含特定字符? - How to check check if a string contains a specific character(s) using range() functions and bools in Python? 如何检查python字符串列表在字符串的最后一个字母中是否包含特定字符? - How to check if a python string list contains a specific character in the last letter of a string? 检查该字符串包含两个相同字符的方法? - Way to check that string contains two of the same characters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM