在python

时间:2016-08-03 13:39:45

标签: python string list

这就是我所拥有的。我想将这两个列表连接在一起

first_name = ['Homer', 'Marge', 'Bart', 'Lisa', 'Maggie',
'Carl', 'Ned', 'Barney', 'Lenny', 'Otto', 'Seymour']

last_name = ['Simpson', 'Simpson', 'Simpson', 'Simpson', 'Simpson',
'Carlson', 'Flanders', 'Gumble', 'Leonard', 'Mann', 'Skinner']

for (i, j) in zip(first_name, last_name):
    print (first_name[i] + " " + last_name[j])

但是有一条错误消息说

TypeError: list indices must be integers, not str 

在“打印”声明的行上

3 个答案:

答案 0 :(得分:6)

你的循环实际上遍历了元素,而不是索引

for (f, l) in zip(first_name, last_name):
    print (f + " " + l)

答案 1 :(得分:0)

编辑:如果使用索引,但迭代通过elemenet更好

两个列表的长度相同:

DECLARE @table as NVARCHAR(25) = 'proposals'
DECLARE @column as NVARCHAR(25) = 'daypart'
DECLARE @modulo as NVARCHAR(5) = '%'

DECLARE @COL_SELECT AS NVARCHAR(2000)
SET @COL_SELECT = '
SELECT DISTINCT column_name 
FROM information_schema.COLUMNS
WHERE table_name = ''' + @table + '''
    AND column_name like ''' + @modulo + @column + @modulo + ''''

PRINT @COL_SELECT
EXEC (@COL_SELECT)

答案 2 :(得分:0)

您可以通过以下方式使用Python map function来获得所需的输出:

print(map(lambda f,l: f + " " + l, first_name, last_name))