![](/img/trans.png)
[英]Taking two lists as input that contain words, to form a tuple with two words, one from each list that have the same starting letter of each word
[英]Printing a string which is formed by taking each successive letter in the list of words based on the position of the word?
例如:
list = ['如何','是','你','今天']
输出:hrua
打印第一个单词的 index0 和第二个单词的 index1 等等......
请帮忙!:(
使用列表理解和 lambda 函数:
result = lambda list: [item[i] for i, item in enumerate(list)]
或作为正常功能
def bar(list):
return [item[i] for i, item in enumerate(list)]
result = bar(....)
这将返回列表中的字母。 如果要打印它们:
l = ["how","are","you","today"]
for index, value in enumerate(l):
print(value[index])
您需要考虑这不会检查有效性。 如果我们查看数组:
["a", "b"]
这将返回一个错误,因为“b”只有索引 0,没有索引 1。
尝试这个:
l = ["how","are","you","today"]
"".join([x[i] for i, x in enumerate(l)])
# Output
# 'hrua'
enumerate
函数是您的朋友文档:
l = ["how","are","you","today"]
for index, value in enumerate(l):
print(value[index])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.