简体   繁体   English

如何索引python列表中的中间字符

[英]How to index a middle character in a list in python

Complete the get_mid_letter() function which is passed a list of strings as a parameter.完成 get_mid_letter() 函数,该函数将字符串列表作为参数传递。 The function returns a string made up of the concatenation of the middle letter of each word from the parameter list.该函数返回一个由参数列表中每个单词的中间字母串联而成的字符串。 The string returned by the function should be in lowercase characters.函数返回的字符串应该是小写字符。 If the parameter list is an empty list, the function should return an empty string.如果参数列表是一个空列表,函数应该返回一个空字符串。

def get_mid_letter(a_list):
    middle_list = []
    for item in a_list:
        middle_index = int(len(item) / 2)
        middle_letter = a_list.index(middle_index)
        middle_list = middle_list + [middle_letter] 
    return middle_list.lower()

def test_get_mid_letter():
    print("1.", get_mid_letter(["Jess", "Cain", "Amity", "Raeann"]))

In my case, it shows an error message like "2 is not in the list".就我而言,它显示一条错误消息,例如“2 不在列表中”。 What can I do to run my code successfully?我该怎么做才能成功运行我的代码? Thanks!谢谢!

array.index(element) returns an index not the character or element . array.index(element)返回一个索引,而不是字符或element So you use array[index_mid] to get the character and then append it to the middle_list所以你使用array[index_mid]来获取字符,然后将它附加到middle_list

As the other answer pointed out, the mistake is here:正如另一个答案所指出的,错误在这里:

middle_letter = a_list.index(middle_index)

The index() method is looking for an element equal to middle_index (in this case, 2) in the list, trying to return the index of that element, but since there's no element equal to 2 in the list you get that error. index()方法在列表中查找等于middle_index (在本例中为 2)的元素,尝试返回该元素的索引,但由于列表中没有等于 2 的元素,因此您会收到该错误。 To find the middle_letter , you should directly access the list item:要找到middle_letter ,您应该直接访问列表项:

middle_letter = item[middle_index]

Be also aware that you're using the lower() method on a list, which is gonna cause an error.另请注意,您正在列表上使用lower()方法,这会导致错误。 To get around this problem, you can use lower() on middle_letter for each loop iteration and simply return middle_list , like this:为了解决这个问题,您可以在每次循环迭代的middle_letter上使用lower()并简单地返回middle_list ,如下所示:

for item in a_list:
    middle_index = int(len(item) / 2)
    middle_letter = item[middle_index]
    middle_letter = middle_letter.lower()
    middle_list = middle_list + [middle_letter] 
return middle_list

There are a few problems with your code.您的代码存在一些问题。

  • The function is supposed to return a string but you declare middle_list as a list not a string.该函数应该返回一个字符串,但您将 middle_list 声明为列表而不是字符串。
  • The index method returns the position in the list of its argument. index 方法返回其参数在列表中的位置。 You want to use [] for access.您想使用 [] 进行访问。
  • You are not retrieving the character from the item, but the item from the list.您不是从项目中检索字符,而是从列表中检索项目。

The corrected code could look like below:更正后的代码可能如下所示:

def get_mid_letter(a_list):
    middle_list = ""                        # declare the variable as an empty string
    for item in a_list:
        middle_index = int(len(item) / 2)   # get the middle index
        middle_letter = item[middle_index]  # get the char from the string in the list
        middle_list += middle_letter        # concatenate the char and the string
    return middle_list.lower()

If you turn each word into a list ['J', 'e', 's', 's'] in your for loop then you can just add the letter of the middle index to your growing word of middle letters如果您在for循环中将每个单词转换为列表['J', 'e', 's', 's']那么您只需将中间索引的字母添加到您不断增长的中间字母单词中

def middles(listb):
    middle_word = ''
    for i in listb:
        letters = list(i)
        index = int(len(i)/2)
        letter = letters[index]
        middle_word += letter.lower()
    return middle_word

lista = ['Jess', 'Cain', 'Amity', 'Reann']

print(middles(lista))
 (xenial)vash@localhost:~/python/stack_overflow$ python3.7 middle.py siia

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM