使用字典进行Python字符串操作

2024-09-24 00:24:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究教授给我们的一个问题,但我无法让它按照我希望的方式工作。我应该先说我是Python的初学者

本质上,我要编写一个函数,给定一个字符串返回与列表相同的字符串,列表的对象是给定字符串的不同版本

例如:Hello应该返回

Hello
hEllo
heLlo
helLo
hellO

但是,我们不允许使用python的内置函数来确定一个字符是大写还是小写,比如upper()还是lower(),在此之前,我们只在MatLab中进行过“编程”,我正在寻找一些入门帮助

到目前为止,我尝试的是这样的:

def randRetur(menBok,dicT):
    menBok = list(menBok)
    for i,j in enumerate(menBok):
        if menBok[i] in dicT.values():
            scrambledPws.append(menBok)
        else:
            scrambledPws.append(dicT[j])
    return scrambledPws

我的字典将小写字母映射为大写字母,而DPWS只是一个空列表

然而,这并没有给出我想要的东西,而是在列表的第一个对象中,所有东西都大写了,这就是我对pythons如何处理for循环的理解与MatLab如何处理它的理解非常不同的地方。因此,我更希望得到一些指导,而不是如何去做,因为我正在努力学习


Tags: 对象函数字符串inhello列表for方式
3条回答

这个问题有多个部分,我可以帮你回答其中的一部分。这部分,, 给定一些字符,您需要返回大写变量,而不使用 字符串方法upper()。我不确定CPython的实现,但是 Ruby one在这里:

https://github.com/ruby/ruby/blob/fb3c711d/regenc.c#L970-L977

下面是一个简单的Python实现:

def up(s_in):
   n_out = ord(s_in) + ord('A') - ord('a')
   return chr(n_out)

输入应为单个字符,输出为上限变量。 请注意,这仅适用于ASCII字符

解决方案非常简单,我将字符串转换为列表,并将包含该字符的列表的索引更改为大写版本。然后我将列表返回到一个字符串,并将其添加到combos中。然后我返回了组合列表。 对不起,我的解释不太好。但希望代码能更好地解释它

def scrambleString(string):
    letters = { # Create a dictionary containing all the letters and their uppercase
    "a": "A",
    "b": "B",
    "c": "C",
    "d": "D",
    "e": "E",
    "f": "F",
    "g": "G",
    "h": "H",
    "i": "I",
    "j": "J",
    "k": "K",
    "l": "L",
    "m": "M",
    "n": "N",
    "o": "O",
    "p": "P",
    "q": "Q",
    "r": "R",
    "s": "S",
    "t": "T",
    "u": "U",
    "v": "V",
    "w": "W",
    "x": "X",
    "y": "Y",
    "z": "Z"
    }
    combos = []
    for index, char in enumerate(string):
        newString = list(string) # I turned the string into a list for easier manipulation
        # The line below swaps the index of string to uppercase
        newString[index] = letters[char] 
        returnString = "" # This is just to the list to a string
        for i in newString: 
            returnString += i
        combos.append(returnString) # Appends that scrambledString to a combo
    return combos

见下文
我们的想法是使用2个DICT来支持从低到高&;从上到下

l2u = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H', 'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L', 'm': 'M', 'n': 'N', 'o': 'O', 'p': 'P', 'q': 'Q', 'r': 'R', 's': 'S', 't': 'T', 'u': 'U', 'v': 'V', 'w': 'W', 'x': 'X', 'y': 'Y', 'z': 'Z'}
u2l = {'A': 'a', 'B': 'b', 'C': 'c', 'D': 'd', 'E': 'e', 'F': 'f', 'G': 'g', 'H': 'h', 'I': 'i', 'J': 'j', 'K': 'k', 'L': 'l', 'M': 'm', 'N': 'n', 'O': 'o', 'P': 'p', 'Q': 'q', 'R': 'r', 'S': 's', 'T': 't', 'U': 'u', 'V': 'v', 'W': 'w', 'X': 'x', 'Y': 'y', 'Z': 'z'}

def get_str_versions(string):
  result = []
  for x in range(len(string)):
    if string[x] in l2u:
      result.append(string[0:x] + l2u[string[x]] + string[x+1:])
    else:
      result.append(string[0:x] + u2l[string[x]] + string[x+1:])
  return result

print(get_str_versions('Hello'))

输出

['hello', 'HEllo', 'HeLlo', 'HelLo', 'HellO']

相关问题 更多 >