python - 如何访问列表列表中的每个字符串

标签 python list

我的任务是输入多行,每行由多个单词组成。任务是将长度为奇数的单词大写,将长度为奇数的单词小写 甚至长度。 我的代码现在是这样的,你能帮我正确解决吗?

first = []
while True:

    line = input().split()
    first.append(line)
    if len(line) < 1:
        break
for i in first:
    for j in i:
        if len(line[i][j]) % 2 == 0:
            line[i][j] = line[i][j].lower()
        elif len(line[i][j]) % 2 != 0:
            line[i][j] = line[i][j].upper()
         print(first[i])

it should look like this

最佳答案

ij 不是索引,它们是子列表和单词本身。你可以这样做:

for i in first:  # i is a list of strings
    for j in range(len(i)):  # you do need the index to mutate the list
        if len(i[j]) % 2 == 0:
            i[j] = i[j].lower()
        else:
            i[j] = i[j].upper()
        print(' '.join(i))

关于python - 如何访问列表列表中的每个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52539861/

相关文章:

python - 人脸识别Python opencv

Python 3 : Removing list item with for loop, 这是正确的方法吗?

list - Haskell - 一个非常简单的函数

python - 比较两个集合列表

python - JSON Python MBTA API 请求

python - 重新索引和堆叠数据框

python - 子类化 Python 列表以验证新项目

list - 如何找出列表类型所属的类?

java - 在查询生成器中使用子字符串操作字符串

python - for循环在 Pandas 中进行逐行比较