为什么在映射后我的字符串顺序会混淆?
Why is the order of my string getting mixed up after the mapping?
我在下面有一些代码,其中我们有一个字母映射,其中 z=1,y=2,...,a=26。我们得到一个字符串格式的编号列表作为输入。而我们需要return使用映射对应的词。
def switcher(arr):
int_arr= []
final_arr = []
for i in arr:
int_arr.append(int(i))
print(int_arr)
ascii_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
ascii_letters.reverse()
for i,l in enumerate(ascii_letters,1):
for j in range(len(int_arr)):
if int_arr[j] == i:
final_arr.append(l)
return final_arr
print(switcher(['24', '12', '23', '22', '4', '26', '9', '8']))
我的问题是关于我上面的代码 - 它可以工作,但由于某种原因,输出字母的打印顺序不正确。例如,上面的数字列表应该对应于 'codewars',但我得到的是 'wsroedca'。我可以看到它的 SPELLS 代码字,但由于某种原因,顺序变得混乱,知道这是为什么吗?我知道集合不保留顺序,但在这种情况下我没有使用集合!
改成下面,你只需要调用ascii_letters[int_asci-1]
,这将return相应的字母。
def switcher(arr):
int_arr= []
final_arr = []
for i in arr:
int_arr.append(int(i))
print(int_arr)
ascii_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
ascii_letters.reverse()
for int_asci in int_arr:
final_arr.append(ascii_letters[int_asci-1])
return final_arr
您不必将其复杂化。
只需反转字母并找到给定索引的字母。
int_arr = [int(x)-1 for x in index]
word = []
letters.reverse()
for ind in int_arr:word.append(letters[ind])
print(''.join(word))
输出
codewars
试试这个:
def switcher(arr):
int_arr= []
final_arr = []
for i in arr:
int_arr.append(int(i)-1)
print(int_arr)
ascii_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
ascii_letters.reverse()
return [ascii_letters[i] for i in int_arr]
print(switcher(['24', '12', '23', '22', '4', '26', '9', '8']))
我在下面有一些代码,其中我们有一个字母映射,其中 z=1,y=2,...,a=26。我们得到一个字符串格式的编号列表作为输入。而我们需要return使用映射对应的词。
def switcher(arr):
int_arr= []
final_arr = []
for i in arr:
int_arr.append(int(i))
print(int_arr)
ascii_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
ascii_letters.reverse()
for i,l in enumerate(ascii_letters,1):
for j in range(len(int_arr)):
if int_arr[j] == i:
final_arr.append(l)
return final_arr
print(switcher(['24', '12', '23', '22', '4', '26', '9', '8']))
我的问题是关于我上面的代码 - 它可以工作,但由于某种原因,输出字母的打印顺序不正确。例如,上面的数字列表应该对应于 'codewars',但我得到的是 'wsroedca'。我可以看到它的 SPELLS 代码字,但由于某种原因,顺序变得混乱,知道这是为什么吗?我知道集合不保留顺序,但在这种情况下我没有使用集合!
改成下面,你只需要调用ascii_letters[int_asci-1]
,这将return相应的字母。
def switcher(arr):
int_arr= []
final_arr = []
for i in arr:
int_arr.append(int(i))
print(int_arr)
ascii_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
ascii_letters.reverse()
for int_asci in int_arr:
final_arr.append(ascii_letters[int_asci-1])
return final_arr
您不必将其复杂化。 只需反转字母并找到给定索引的字母。
int_arr = [int(x)-1 for x in index]
word = []
letters.reverse()
for ind in int_arr:word.append(letters[ind])
print(''.join(word))
输出
codewars
试试这个:
def switcher(arr):
int_arr= []
final_arr = []
for i in arr:
int_arr.append(int(i)-1)
print(int_arr)
ascii_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
ascii_letters.reverse()
return [ascii_letters[i] for i in int_arr]
print(switcher(['24', '12', '23', '22', '4', '26', '9', '8']))