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

时间:2017-07-31 05:30:54

标签: python

在下面的列表中,我尝试在数据库中插入每个姓名和电话号码。所以我尝试迭代并将其分配给变量并在insert语句中使用它。

if(realmCities!=null && realmCities.size()>0){
    Log.e("realmCities",""+realmCities.size());
}else{
    callJSON();
}

这就是我一直在尝试的:

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']
]

4 个答案:

答案 0 :(得分:1)

您可以使用functools

中的reduce功能
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']

根据Varun你的评论

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   

答案 1 :(得分:0)

据我所知,你错误地访问了你的号码。您的contactlist是列表清单。因此,您可以像访问方式一样访问每个条目。如果您需要该号码,请按以下方式获取该数据的第二个条目:

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

不是打印条目,而是可以对变量进行任何操作,将它们添加到数据库中。

答案 2 :(得分:0)

看看这个: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')

答案 3 :(得分:0)

只是一个建议,但在你对Kallz的帖子的评论中你说" 同样的输出我在上面发布的代码中也有,但我的问题我想提取个人姓名和电话号码&# 34。

为此你可能想要使用字典。

Contacts={}
Contacts['Barry']="184788174"
Contacts['Stewart']="1515174"
Contacts['Sarah']="1252358174"

print(Contacts['Barry'])

返回:

184788174

或显示全部:

Contacts={}
Contacts['Barry']="184788174"
Contacts['Stewart']="1515174"
Contacts['Sarah']="1252358174"


for data in Contacts:
    print(data, ":")
    print(Contacts[data], """
""")

返回:

Barry :
184788174 

Stewart :
1515174 

Sarah :
1252358174