在变量中创建for循环

时间:2016-01-03 14:03:00

标签: python python-2.7

我的代码需要一些帮助。我正在使用for循环创建一个变量,以便我获取元素列表。

当我尝试这个时:

Size

我会得到这样的长元素列表:

channels = map(str, channels)
channels = str(channels)
channel = [channels for channel in channels]
print channel

以下是我使用13:54:18 T:4308 NOTICE: ["['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']", "['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']"]

时的输出结果
print channels

以下是完整代码:

14:01:19 T:1616  NOTICE: ['101 BBC One S East ', '102 BBC Two ', '103 ITV ', '104 Channel 4 ', '105 Channel 5 ', '106 Sky One ', '107 Sky Living ']

我想在变量里面使用for循环的变量。

请告诉我如何在列表中打印每个元素而不重复在变量中创建for循环?

1 个答案:

答案 0 :(得分:2)

答案

此代码完全错误:

channels = map(str, channels)
channels = str(channels)
channel = [channels for channel in channels]
print channel

除此之外,要打印每个频道,您可以这样做:

for channel in channels:
    print channel

详细

术语

变量“中没有”for循环。你拥有的是一个有价值的变量。该值可以是任何类型的对象,例如字符串或列表。

您的代码

让我们逐行完成你的代码......

第一行将channels的每个成员转换为字符串:

channels = map(str, channels)

例如,如果列表中有整数([1, 2, 3]),则会将它们转换为字符串(['1', '2', '3'])。

下一行将整个channels对象转换为一个字符串:

channels = str(channels)

因此, 1,2,3 示例现在变为"['1', '2', '3']"

下一行是列表理解。它遍历channelsfor channel in channels)的所有元素,并为每个元素添加channels到列表中。然后,它将所有内容存储到名为channel的变量中:

channel = [channels for channel in channels]

我不知道目的是什么。无论如何,最终结果与您使用以下代码得到的结果相当:

channel = []
for character in channels:
    # Goes through all 15 characters of "['1', '2', '3']"
    # Ignores the character, appends the whole `characters` string to the list
    channel.append(channels)

请注意,在上一行channels被转换为字符串,因此迭代channels实际上会遍历字符。

因此,在该行之后,对于 1,2,3 输入,channel将是"['1', '2', '3']"字符串的15倍。

简单指南

如果您不知道代码行是什么,请尝试打印结果。这很简单有效。例如:

print channels
channels = map(str, channels)
print channels
channels = str(channels)
print channels
channel = [channels for channel in channels]
print channel

然后看看你得到的是你想要的每条线。