简体   繁体   English

在字符串中查找字母相邻字母(2 种类型:aABClg = ABC,aAbcd = aA)

[英]Find alphabet neighbour letters in string (2 types: aABClg = ABC, aAbcd = aA)

EDIT: The problem, and the answer lies in using the enumerate function to access the index of iterables, but I'm still working on applying it properly.编辑:问题和答案在于使用枚举 function 来访问可迭代的索引,但我仍在努力正确应用它。

I was asked to generate a random word with N length, and to print uppercase alphabet neighbours and lower - upper neighbours.我被要求生成一个长度为 N 的随机单词,并打印大写字母邻居和小写 - 大写邻居。 I really don't know how to put this better.我真的不知道如何更好地表达这个。 The example is in the title, here is my code so far, and I think it works, I just need to fix the error made by the index search in the ascii_uppercase list variable.示例在标题中,这是我到目前为止的代码,我认为它有效,我只需要修复 ascii_uppercase 列表变量中的索引搜索所产生的错误。

Also, please excuse the messy do - while loop at the beginning.另外,请原谅开头的混乱 do - while 循环。

import string
import random
 
letters = list(string.ascii_uppercase + string.ascii_lowercase)
 
enter = int(input('N: '))
def randomised(signs = string.ascii_uppercase + string.ascii_lowercase, X = enter):
        return( ''.join(random.choice(signs) for _ in range(X)))
 
if enter == 1:
    print('END')
   
while enter > 1:
    enter = int(input('N: '))
    word1 = randomised()
    word2 = list(word1)
    neighbour = ''
    same = ''
    for j in word2:
        if word2[j] and word2[j+1] in string.ascii_uppercase and letters.index(j) == word2.index(j) and letters.index(j+1) == word2.index(j+1):
            same += j
            same += j+1
           
    for i in word2:
        if word2[i] == i.upper and word2[i+1] == (i+1).upper:
            neighbour += i
            neighbour += i+1
    print('Created: {}, Neighbour uppercase letters: {}, Neighbour same letters: {}' .format(word1,same,neighbour))

expected behaviour:预期行为:

N: 7
Created: aaBCDdD, Neighbour uppercase letters: BCD, Neighbour same letters: dD
N: 1
N: END

I am not so sure, but i think your problem might stem from the use of "i+1" and "j+1" without limiting the iterations stop before the last one.我不太确定,但我认为您的问题可能源于使用“i+1”和“j+1”而不限制迭代在最后一个之前停止。

The next thing is that you need to put this code in english for people to be able to provide better answers, i am not native english, but the core of my code is in english so it will be understandable worldwide, there are other general improvements that can be done, but those are learn while coding.接下来的事情是你需要把这段代码写成英文,以便人们能够提供更好的答案,我不是英语母语,但我的代码的核心是英文的,所以全世界都可以理解,还有其他一般性的改进可以做到,但那些是在编码时学习的。

I hope your assignment goes great.我希望你的任务进展顺利。

Another recommendation you can use "a".islower() to see if the character is lowercase, isupper() to se if it is uppercase, you don't need to import the whole alphabet.另一个建议您可以使用“a”.islower() 来查看字符是否为小写,isupper() 来查看是否为大写,您不需要导入整个字母表。 There are many builtin functions to deal with common scenarios and those tend to be more efficient than what most people would do without them.有许多内置函数可以处理常见场景,并且这些函数往往比大多数人没有它们时所做的更有效。

Here is a simple working code (without formatting of the output).这是一个简单的工作代码(没有输出格式)。 I used a simple pairwise iteration to compare each character with the previous one.我使用了一个简单的成对迭代来将每个字符与前一个字符进行比较。

# generation of random word
N = 50
word = ''.join(random.choices(ascii_letters, k=N))

# forcing word for testing
word = 'aaBCDdD'

# test of conditions
cond1 = ''
cond2 = ''
flag1 = False  # flag to add last character of stretch

for a,b in zip('_'+word, word+'_'):
    if a.isupper() and b.isupper() and ord(a) == ord(b)-1:
        cond1 += a
        flag1 = True
    elif flag1:
        flag1 = False
        cond1 += a
    if a.islower() and b.isupper() and a.lower() == b.lower():
        cond2 += a+b

print(cond1, cond2, sep='\n')
# BCD
# dD

NB.注意。 In case the conditions are met several times in the word, the identified patterns will just be concatenated如果单词中多次满足条件,则将识别出的模式连接起来

Example on random word of 500 characters: 500 个字符的随机单词示例:

IJRSOPHILMMNVW
kKdDyY

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM