如何在保持一定顺序的同时合并列表和多维列表

时间:2019-06-10 22:14:22

标签: python list

我有1个姓名列表:

[VendorName1, VendorName2, VendorName3, VendorName4]

我有一个大小不一的电子邮件的多维列表:

[[email1@mail.com],[email2@mail.com,email3@mail.com],[email4@mail.com],[email5@mail.com,email6@mail.com,email7@mail.com]

这是我到目前为止尝试过的方法,但是我一直遇到错误。

final=[]
  count=1
  for sub1 in vendor_names:
    for sub2 in vendor_emails:
      vendor_names[count].append(final)
      vendor_emails[count].append(final[1])
  print(final)

我想合并两个列表,以便最终列表像这样:

[[[Vendor1,[email1@mail.com],[Vendor2,[email2@mail.com,email3@mail.com]]...]

2 个答案:

答案 0 :(得分:1)

您可以使用zip(),但是它将列表合并到元组的列表中。

OR

用于将列表合并到列表列表:

# This section does nothing except cause errors
1 = service
2 = process
3 = pinging
4 = multiplying a number

# What is SNumber? Variables need a $ in front of them
$num1 = Read-Host " Please enter a number for service "
Snumber = 1
# What are you doing with the number they give you? You are retrieving all services
Get-Service

$num2 = Read-Host " Please enter a number for process"
$Number = 2
# What are you doing with the number they give you? You are retrieving all Processed
Get-Process

$num3 = Read-Host " Please enter a number to ping"
# What is this variable used for?
$Number = 3
$ComputerName = Read-Host "enter the FQDN of the target computer"
Test-Connection $ComputerName FQDN

$num4 = Read-Host " Please enter a number for double the number"
# What is this variable used for?
$Number = 4
$num4 = Read-Host "Pleas enter number 5" 
# What if I chose the number 200, it will still say I chose 5
# Well it would if it didn't error out. The command is Write-Host
Write_Host "Your original number was 5, now it's 10"`enter code here`

输出:

x = ['VendorName1', 'VendorName2', 'VendorName3', 'VendorName4']
y = [['email1@mail.com'], ['email2@mail.com', 'email3@mail.com'], [
    'email4@mail.com'], ['email5@mail.com', 'email6@mail.com', 'email7@mail.com']]
z = []
for i in range(len(x)):
    z.append([])
    z[i].append(x[i])
    z[i].append(y[i])
print(z)

答案 1 :(得分:1)

Zip可以做到这一点:

l1 = ['VendorName1', 'VendorName2', 'VendorName3', 'VendorName4']
l2 = [['email1@mail.com'],['email2@mail.com','email3@mail.com'],['email4@mail.com'],['email5@mail.com,email6@mail.com','email7@mail.com']]

print(list(zip(l1, l2)))

输出:

[('VendorName1', ['email1@mail.com']), ('VendorName2', ['email2@mail.com', 'email3@mail.com']), ('VendorName3', ['email4@mail.com']), ('VendorName4', ['email5@mail.com,email6@mail.com', 'email7@mail.com'])]

如果您真的不想要tuples(由于它们是不可变的),则可以进行列表理解,但这要求您的供应商不要超过电子邮件:

l1 = ['VendorName1', 'VendorName2', 'VendorName3', 'VendorName4']
l2 = [['email1@mail.com'],['email2@mail.com','email3@mail.com'],['email4@mail.com'],['email5@mail.com,email6@mail.com','email7@mail.com']]

l3 = [[l1[x]] + [l2[x]] for x in range(len(l1))]
print(l3)

输出:

[['VendorName1', ['email1@mail.com']], ['VendorName2', ['email2@mail.com', 'email3@mail.com']], ['VendorName3', ['email4@mail.com']], ['VendorName4', ['email5@mail.com,email6@mail.com', 'email7@mail.com']]]