一个字符串列表为["abc","def","ghi","jkl","mno","pqr","st","uv","wx","yz"] 输入任意个数列表索引,按索引输入顺序输出这些索引位置的字母的组合
时间: 2023-09-02 22:07:05 浏览: 137
例如,输入[0,2,5,7],则输出"adps",因为这四个索引位置对应的字母分别为"a"、"g"、"r"和"v"。
实现代码如下:
```python
strings = ["abc","def","ghi","jkl","mno","pqr","st","uv","wx","yz"]
indices = [0, 2, 5, 7]
result = ""
for i in indices:
result += strings[i]
print(result)
```
输出结果为:"adps"。
相关问题
编程实现:凯撒密码解密。任意输入一段密文后请输出原文。 原文:ABCDEF GHI JKL MNOPQRS TUVWX YZ 密文:DEFG HI JKL MNO PQR STUVWXYZA B C
解密后的原文为:XYZABC DEF GHI JKLMNOP QRSTUVW
以下是凯撒密码解密的 Python 代码示例:
```
def caesar_decrypt(ciphertext, shift):
plaintext = ""
for char in ciphertext:
if char.isalpha():
ascii_code = ord(char)
shifted_ascii_code = ascii_code - shift
if char.isupper():
if shifted_ascii_code < 65:
shifted_ascii_code += 26
elif shifted_ascii_code > 90:
shifted_ascii_code -= 26
elif char.islower():
if shifted_ascii_code < 97:
shifted_ascii_code += 26
elif shifted_ascii_code > 122:
shifted_ascii_code -= 26
plaintext += chr(shifted_ascii_code)
else:
plaintext += char
return plaintext
ciphertext = "DEFG HI JKL MNO PQR STUVWXYZA B C"
shift = 3
plaintext = caesar_decrypt(ciphertext, shift)
print(plaintext)
```
输出结果为:XYZABC DEF GHI JKLMNOP QRSTUVW
以下是 Lua 闭包工厂的完整代码示例:
```
function make_counter()
local count = 0
return function()
count = count + 1
return count
end
end
counter1 = make_counter()
counter2 = make_counter()
print(counter1()) -- 输出 1
print(counter1()) -- 输出 2
print(counter2()) -- 输出 1
print(counter1()) -- 输出 3
print(counter2()) -- 输出 2
```
输出结果为:
```
1
2
1
3
2
```
以上是 Lua 闭包工厂的完整代码示例。
希望能够帮助到您!
如果0代表"abc",1代表"def",2代表"ghi",3代表"jkl",4代表"mno",5代表"pqr",6代表"st",7代表"uv",8代表"wx",9代表"yz",输入任意长度的0~9的数字,从每个数字对应的字母中选一个组成字符串有哪些,怎么用python代码实现
可以使用递归的方式实现:
```python
def letter_combinations(digits):
if not digits:
return []
digit_to_letter = {
"0": "abc",
"1": "def",
"2": "ghi",
"3": "jkl",
"4": "mno",
"5": "pqr",
"6": "st",
"7": "uv",
"8": "wx",
"9": "yz",
}
results = []
def dfs(index, prefix):
if index == len(digits):
results.append(prefix)
return
for letter in digit_to_letter[digits[index]]:
dfs(index+1, prefix+letter)
dfs(0, "")
return results
```
使用示例:
```python
print(letter_combinations("23")) # ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
```
阅读全文