如何在python中使用多个for循环保存为列表

问题描述

我有两列:

ID 姓名
11 大卫
12 亚历克斯
13 爱丽丝
14 标记
15 玛丽亚

我想将 ID 和名称组合在一起并保存为列表。例如,['ID 11 属于大卫']。我该怎么做?

到目前为止我尝试了什么?

op=[]
for i,j in itertools.product(data['ID'],data['Name']):
        dt = str('The ID' + str(i)+ 'belongs to' +j)
        op.append(dt)
        print(dt)

输出多次保存为列表。我该如何纠正?

所需的输出

['The ID 11 belongs to David','The ID 12 belongs to Alex','The ID 13 belongs to Alice','The ID 14 belongs to Mark','The ID 15 belongs to Maria']

解决方法

您想要的输出不是列表 import { LoginPage } from './login.po'; import { browser,logging,element,by } from 'protractor'; describe('Login page test',() => { let page: LoginPage; beforeEach(() => { page = new LoginPage(); }); it('Login button should activate with login and pass',() => { page.navigateTo(); expect(page.getButtonDisabledStatus()).toEqual('true'); page.fillLoginPassWith('user','password') expect(page.getButtonDisabledStatus()).toBeNull(); }); }); int currV; int maxSoFar = 0; // if all integers are negetive than `maxSoFar` should be 0 while (cin >> currV) { if (currV < 0) break; maxSoFar = max(maxSoFar,currV); } cout << "Max: " << maxSoFar; 的叉积。

所以不要使用data['ID'],我想你想改用zip()data['Name'] 返回一个元组生成器,将列表 itertools.product 的第一个元素与列表 zip(a,b) 的第一个元素匹配,然后是两个列表的第二个元素,然后是第三个,依此类推.

您还可以将代码缩短为列表推导式并使用 f 字符串,使其更加 Python 化:

a

该代码的输出将是:

b

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...