[英]How to print even and odd chars from a given string in Python
I was doing a problem in Python: Problem statement我在 Python 中遇到问题:问题陈述
However, my code is not getting the output I want.但是,我的代码没有得到我想要的 output。 I want to store the strings in an array (2xtestcases) (given by the size of 'testcases' and then for each word loop over the index of the strings of the arrays and print the even and uneven word.我想将字符串存储在一个数组(2xtestcases)中(由'testcases'的大小给出,然后为每个单词循环遍历 arrays 的字符串索引并打印偶数和不均匀单词。
import numpy as np
import array as arr
# testcases
testcases = int(input())
# list of empty strings
strings = [None] * testcases
# list of translated strings
strings_translated = np.zeros((2, testcases))
for i in range(0, testcases):
strings[i] = input()
for k in strings[i]:
if k % 2 == 0:
strings_translated[1, i] = strings_translated[1, i] + k
else:
strings_translated[2, i] = strings_translated[2, i] + k
for i in range(0, testcases):
print(strings_translated[1, i] + strings_translated[2, i])
I get the following error:我收到以下错误:
if k % 2 == 0:
TypeError: not all arguments converted during string formatting
Would you like to try this: (just a sample code to give you hints)你想试试这个吗:(只是一个示例代码给你提示)
N = len(s) # your string's len
evens = ''.join(s[i] for i in range(0, N,2)) # 'Hce'
# then do the odds character as well
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.