Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在列表上迭代-生成列表_Python_Loops_Dictionary_Variables_Nested Lists - Fatal编程技术网

Python 在列表上迭代-生成列表

Python 在列表上迭代-生成列表,python,loops,dictionary,variables,nested-lists,Python,Loops,Dictionary,Variables,Nested Lists,下面的代码有一个名为“list_a”的列表,它循环了4次 list_a=['red','green','blue', 'yellow'] list_1=[] for i in list_a[0]: list_1.append(i) print(list_1) list_2=[] for i in list_a[1]: list_2.append(i) print(list_2) list_3=[] for i in list_a[2]: list_3.append(

下面的代码有一个名为“list_a”的列表,它循环了4次

list_a=['red','green','blue', 'yellow']

list_1=[]
for i in list_a[0]:
    list_1.append(i)
print(list_1)

list_2=[]
for i in list_a[1]:
    list_2.append(i)
print(list_2)

list_3=[]
for i in list_a[2]:
    list_3.append(i)
print(list_3)

list_4=[]
for i in list_a[3]:
    list_4.append(i)
print(list_4)
如果要迭代“list_a”中的每个元素并将每个字母拆分为一个字符串,则可以按照上面的代码执行。但是,有没有一种方法可以编写更短的代码,而不是每次循环“list_a”元素时都编写相同的代码

例如,我的尝试是:

list_a=['red','green','blue', 'yellow']

number=0
while number<len(list_a):
    list_(int(number+1))=[]
    for i in list_a[number]:
        list_(int(number+1)).append(i)
    print(list_(int(number+1))
number+=1
list_a=[“红色”、“绿色”、“蓝色”、“黄色”]
数字=0

而数字这就是你想要的吗

list_a = ['red', 'green', 'blue', 'yellow']
list_b = [[ch for ch in word] for word in list_a]
print(list_b)
输出:

[['r', 'e', 'd'], ['g', 'r', 'e', 'e', 'n'], ['b', 'l', 'u', 'e'], ['y', 'e', 'l', 'l', 'o', 'w']]
[['r', 'e', 'd'], ['g', 'r', 'e', 'e', 'n'], ['b', 'l', 'u', 'e'], ['y', 'e', 'l', 'l', 'o', 'w']]
或许:

list_a = ['red', 'green', 'blue', 'yellow']
for word in list_a:
    print([ch for ch in word])
结果:

['r', 'e', 'd']
['g', 'r', 'e', 'e', 'n']
['b', 'l', 'u', 'e']
['y', 'e', 'l', 'l', 'o', 'w']
尝试使用以下代码:

list_a=['red','green','blue', 'yellow']
output_lst = []
for word in list_a:
    temp = []
    for char in word:
        temp.append(char)
    output_lst.append(temp)
print(output_lst) 

在第二个代码(尝试)中,您不能像在代码中那样定义列表名称(Python不是这样工作的),相反,您必须创建列表列表(嵌套列表)来解决此问题。我附上示例代码以解决此问题及其输出。

示例代码

让列表成为一个

其产出

[r',e',d',[g',r',e',e',e',n']]


如您所见,它是包含每个元素的嵌套列表

这是获得相同输出的捷径:

代码:

list_a=['red','green','blue', 'yellow']

for word in list_a:
    print(list(word))
['r', 'e', 'd']
['g', 'r', 'e', 'e', 'n']
['b', 'l', 'u', 'e']
['y', 'e', 'l', 'l', 'o', 'w']
输出:

list_a=['red','green','blue', 'yellow']

for word in list_a:
    print(list(word))
['r', 'e', 'd']
['g', 'r', 'e', 'e', 'n']
['b', 'l', 'u', 'e']
['y', 'e', 'l', 'l', 'o', 'w']

在遍历
列表中的每个元素时,使用列表理解将每个字母作为字符串存储在另一个列表中。我尝试了以下代码:

list_a=['red','green','blue', 'yellow']
list_b=[]
for i in list_a:
    list_b.append([j for j in i])
for i in list_b:
    print(i)
此处
list\u b
包含
list\u a
中每个字符串的子列表。每个子列表包含一个字符串的所有字符作为一个单独的字符串

这产生了以下输出:

C:\User\Python>python script.py
['r', 'e', 'd']
['g', 'r', 'e', 'e', 'n']
['b', 'l', 'u', 'e']
['y', 'e', 'l', 'l', 'o', 'w']
最短代码:

[[*a] for a in list_a]
输出:

[['r', 'e', 'd'], ['g', 'r', 'e', 'e', 'n'], ['b', 'l', 'u', 'e'], ['y', 'e', 'l', 'l', 'o', 'w']]
[['r', 'e', 'd'], ['g', 'r', 'e', 'e', 'n'], ['b', 'l', 'u', 'e'], ['y', 'e', 'l', 'l', 'o', 'w']]

说明:
*a
称为解包,当应用于字符串时,将其解包为单个字符<代码>[*a]
表示我们正在将其解包到列表中。仅适用于Python 3。

您正在寻找列表理解我的朋友,在类似这样的问题中,明确告诉我们您希望输出的内容会很有帮助。要获得列表,您只需:
[列表a中l的列表(l)]
,但不清楚这是否是您想要的。我没想到会出现这种情况。回答得好:)