这里也有类似的问题,但是如果一个单词更长,他们希望剩下的字母返回。我正在尝试为两个字符串返回相同数量的字符。
这是我的代码:
def one_each(st, dum):
total = ""
for i in (st, dm):
total += i
return total
x = one_each("bofa", "BOFAAAA")
print(x)
它不起作用,但我正在尝试获得所需的输出:
>>>bBoOfFaA
我将如何解决这个问题?谢谢!
答案 0 :(得分:0)
我可能会做这样的事情
s1 = "abc"
s2 = "123"
ret = "".join(a+b for a,b in zip(s1, s2))
print (ret)
答案 1 :(得分:0)
这是一种简短的方法。
export class MyErrorStateMatcher implements ErrorStateMatcher {
isErrorState(
control: FormControl | null,
form: FormGroupDirective | NgForm | null
): boolean {
const isSubmitted = form && form.submitted;
return !!(
control &&
control.invalid &&
(control.dirty || control.touched || isSubmitted)
);
}
}
name = new FormControl("", [Validators.required]);
dob = new FormControl("", [Validators.required]);
policyNo = new FormControl("", [
Validators.required,
Validators.minLength(6)
]);
matcher = new MyErrorStateMatcher();
当您输入def one_each(short, long):
if len(short) > len(long):
short, long = long, short # Swap if the input is in incorrect order
index = 0
new_string = ""
for character in short:
new_string += character + long[index]
index += 1
return new_string
x = one_each("bofa", "BOFAAAA") # returns bBoOfFaA
print(x)
时,即当小写字母长于大写字母时,它可能会显示错误的结果,但是如果您更改输出中每个字母的x = one_each("abcdefghij", "ABCD")
,则可以很容易地解决。 / p>
答案 2 :(得分:0)
str.join
与zip
一起使用,因为zip
仅成对地迭代直到最短可迭代。您可以与itertools.chain
结合使用以使可迭代的元组变平坦:
from itertools import chain
def one_each(st, dum):
return ''.join(chain.from_iterable(zip(st, dum)))
x = one_each("bofa", "BOFAAAA")
print(x)
bBoOfFaA