如何将多列表数据集中的每个元素分配给一个变量?

2024-04-25 09:24:16 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的列表中,我尝试在数据库中插入每个姓名和电话号码。所以我尝试迭代并将它赋给一个变量,然后在insert语句中使用它。你知道吗

contactlist = [
  ['Siemens, Harper',  '323-4149'],
  ['Smith, Patti',  '239-1212'],
  ['Jackson, Janet',   '313-1352'],
  ['Manfredi, Ralph','872-2221'],
  ['Thompson, Bobby',   '365-2622'],
  ['James, Lebron',  '457-6223'],
  ['Ziegler, Zig',   '667-1101'],
  ['Robbins, Tony', '329-2310']
]

这就是我一直在尝试的:

a = []
for data in contactlist:
    #print (data)
    print (data[0])
    for d1 in data:
        #print (d1)
        a.append(d1)
print (a)

Tags: in数据库列表fordata电话号码语句d1
3条回答

看看这个:Inserting multiple rows in a single SQL query?,一种方法是创建一个插入到命令中的字符串

contactlist = [
  ['Siemens, Harper',  '323-4149'],
  ['Smith, Patti',  '239-1212'],
  ['Jackson, Janet',   '313-1352'],
  ['Manfredi, Ralph','872-2221'],
  ['Thompson, Bobby',   '365-2622'],
  ['James, Lebron',  '457-6223'],
  ['Ziegler, Zig',   '667-1101'],
  ['Robbins, Tony', '329-2310']
]

str([tuple(i) for i in contactlist])[1:-1]

印刷品

"('Siemens, Harper', '323-4149'), ('Smith, Patti', '239-1212'), ('Jackson, Janet', '313-1352'), ('Manfredi, Ralph', '872-2221'), ('Thompson, Bobby', '365-2622'), ('James, Lebron', '457-6223'), ('Ziegler, Zig', '667-1101'), ('Robbins, Tony', '329-2310')"

但是看看Python文档,这是不必要的。想想这个 完整示例:https://docs.python.org/2/library/sqlite3.html):

import sqlite3

contactlist = [
      ['Siemens, Harper',  '323-4149'],
      ['Smith, Patti',  '239-1212'],
      ['Jackson, Janet',   '313-1352'],
      ['Manfredi, Ralph','872-2221'],
      ['Thompson, Bobby',   '365-2622'],
      ['James, Lebron',  '457-6223'],
      ['Ziegler, Zig',   '667-1101'],
      ['Robbins, Tony', '329-2310']
    ]

conn = sqlite3.connect('example.db')

c = conn.cursor()

# Delete table
c.execute('drop table if exists employees')

# Create table
c.execute('''CREATE TABLE employees
             (name, number)''')

# Insert a row of data
c.executemany('INSERT INTO employees VALUES (?,?)', contactlist)
# or
# c.execute("INSERT INTO employees VALUES {}".format(str([tuple(i) for i in contactlist])[1:-1]))

conn.commit() # Commit the changes
conn.close()

数据库现在包含一个名为employees的表,其中包含以下行:

('Siemens, Harper', '323-4149')
('Smith, Patti', '239-1212')
('Jackson, Janet', '313-1352')
('Manfredi, Ralph', '872-2221')
('Thompson, Bobby', '365-2622')
('James, Lebron', '457-6223')
('Ziegler, Zig', '667-1101')
('Robbins, Tony', '329-2310')

据我所知,你打错电话了。你的contactlist是一个列表列表。所以你可以像以前那样访问每个条目。如果您需要数字,请使用该数据的第二个条目,如下所示:

for data in contactlist:
    name = data[0]
    number = data[1]
    print 'Name: %s, Number %s' %(name, number)

不用打印条目,您可以对变量执行任何操作,将它们添加到数据库中。你知道吗

您可以使用functools中的^{}函数

from functools import reduce    
reduce(lambda m,n: m+n,contanctlist)

在你的代码里

>>> contactlist = [
...   ['Siemens, Harper',  '323-4149'],
...   ['Smith, Patti',  '239-1212'],
...   ['Jackson, Janet',   '313-1352'],
...   ['Manfredi, Ralph','872-2221'],
...   ['Thompson, Bobby',   '365-2622'],
...   ['James, Lebron',  '457-6223'],
...   ['Ziegler, Zig',   '667-1101'],
...   ['Robbins, Tony', '329-2310']
... ]
>>> a = reduce(lambda m,n: m+n,contactlist)
>>> a
['Siemens, Harper', '323-4149', 'Smith, Patti', '239-1212', 'Jackson, Janet', '313-1352', 'Manfredi, Ralph', '872-2221', 'Thompson, Bobby', '365-2622', 'James, Lebron', '457-6223', 'Ziegler, Zig', '667-1101', 'Robbins, Tony', '329-2310']

根据瓦伦你的评论

contactlist = [
  ['Siemens, Harper',  '323-4149'],
  ['Smith, Patti',  '239-1212'],
  ['Jackson, Janet',   '313-1352'],
  ['Manfredi, Ralph','872-2221'],
  ['Thompson, Bobby',   '365-2622'],
  ['James, Lebron',  '457-6223'],
  ['Ziegler, Zig',   '667-1101'],
  ['Robbins, Tony', '329-2310']
 ]

phone_list = []
person  = []

for contact in contactlist:
    phone_list.append(contact[1])
    person.append(contact[0])

print phone_list
print person   

相关问题 更多 >