如何使用 itertools 将一个函数的返回值传递给另一个函数?
How to use itertools to pass the returned values from one function to another?
我有一个返回列表
[('10.12.250.29', 'pdx02-he-trial-ansible01', 'us-west-2a', 'vol-e7775a10'), ('10.12.32.22', 'pdx02-cloud-prod-ansible01 Clone ', 'us-west-2b', 'vol-b0607d70'), ('10.12.0.20', 'pdx02-cloud-trial/dev-ansible01', 'us-west-2a', 'vol-b32e5c46'), ('10.12.250.7', 'pdx02-he-prod-ansible01', 'us-west-2a', 'vol-fd94400b'), ('10.12.250.4', 'pdx02-he-dev-ansible01', 'us-west-2a', 'vol-ee6abf18'), ('10.12.32.16', 'pdx02-cloud-prod-ansible', 'us-west-2b', 'vol-ae49adbb'), ('10.121.15.22', 'ansible-classic', 'us-west-2a', 'vol-f893c20d'), ('10.17.15.145', 'pdx01-ms-dev-ansible', 'us-west-2a', 'vol-e2d45515'), ('10.21.32.27', 'fra01-cloud-prod-ansible', 'eu-central-1b', 'vol-5f86f5bd'), ('10.21.250.13', 'fra01-he-trial-ansible01', 'eu-central-1a', 'vol-f9e7d220'), ('10.21.250.27', 'fra01-he-dev-ansible01', 'eu-central-1a', 'vol-f6e3fa2f'), ('10.21.0.9', 'fra01-cloud-dev-ansible01', 'eu-central-1a', 'vol-98104671'), ('10.21.250.5', 'fra01-he-prod-ansible01', 'eu-central-1a', 'vol-809b8259'), ('10.31.250.26', 'sin01-he-dev-ansible01', 'ap-southeast-1a', 'vol-86443940'), ('10.31.250.19', 'sin01-he-prod-ansible01', 'ap-southeast-1a', 'vol-bebcc178'), ('10.31.32.12', 'sin01-cloud-prod-ansible01', 'ap-southeast-1b', 'vol-01409de9'), ('10.31.250.27', 'sin01-he-trial-ansible01', 'ap-southeast-1a', 'vol-f6cdc631'), ('10.31.0.18', 'sin01-cloud-dev-ansible01', 'ap-southeast-1a', 'vol-3c0aac28')]
这基本上是:
<IP_ADDRESS> , <AWS_TAG_NAME> , <REGION> , <VOLUME>
现在我将它传递给另一个方法,在该方法中我需要提取每个值并单独存储它所以我使用了 __main__
中的 itertools
:
data = list(itertools.chain(*ansible_box_info))
print "-----------------"
print data
#mapping = {i[0]: [i[1], i[2]] for i in data}
print "Now Calling the Snapshot Creater!"
call_snapshot_creater(data)
def call_snapshot_creater(passed_data):
ip_address = ','.join(list(itertools.chain(*[[j[0] for j in i] for i in data])))
tags_descrip = list(itertools.chain(*[[j[1] for j in i] for i in data]))
regions_az = list(itertools.chain(*[[j[2] for j in i] for i in data]))
volume_id = list(itertools.chain(*[[j[3] for j in i] for i in data]))
这打破了上面的列表,只选择所有内容的第一个字母,即 ip_address 打印:
1,p,u,v,1,p,u,v,1,p,u,v,1,p,u,v,1,p,u,v,1,p,u,v,1,a,u,v,1,p,u,v,1,f,e,v,1,f,e,v,1,f,e,v,1,f,e,v,1,f,e,v,1,s,a,v,1,s,a,v,1,s,a,v,1,s,a,v,1,s,a,v
而不是 10.12.250.29 , 10.12.32.22 .....................
其他人也一样
如何使用迭代器正确地打破它?
我正在尝试通过将这些值传递给我的快照创建者来创建快照:
def call_snapshot_creater(passed_data):
ip_address = ','.join(list(itertools.chain(*[[j[0] for j in i] for i in data])))
tags_descrip = list(itertools.chain(*[[j[1] for j in i] for i in data]))
regions_az = list(itertools.chain(*[[j[2] for j in i] for i in data]))
volume_id = list(itertools.chain(*[[j[3] for j in i] for i in data]))
regions = ['us-west-2', 'eu-central-1', 'ap-southeast-1']
for region in regions:
ec2 = boto3.resource('ec2', region, aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY, )
print "Snapshot Creation For Ansible -> ",ip_address," initiated , tag = ", tags_descrip ,"region : ", regions_az
print "Snapshot will be created with -> Name : ",tags_descrip
snapshot = ec2.create_snapshot(VolumeId=volume_id, Description=tags_descrip)
print snapshot.id
print "Snapshot is being created for Ansible box ", tags_descrip ,"with snapshot id :",snapshot.id
#slack.chat.post_message(slack_channel,"Creating Snapshot for The volume"+ str(snapshot.id),username='Ansible_box_snapshot_bot')
snapshot.load()
while snapshot.state != 'completed':
print "The Snapshot :", snapshot.id , "for Ansible box named : ", tags_descrip ,"is currently in :",snapshot.state," state"
time.sleep(30)
snapshot.load()
print snapshot.progress
else:
print "Snapshot ",snapshot.id, "for Ansible box ", tags_descrip , "is now Ready!! Final state ->",snapshot.state
您没有正确使用chain
和解包。所有你想做的,都可以用 zip
和 unpacking 来完成,即。 zip(*...)
:
>>> l = [('10.12.250.29', 'pdx02-he-trial-ansible01', 'us-west-2a', 'vol-e7775a10'), ('10.12.32.22', 'pdx02-cloud-prod-ansible01 Clone ', 'us-west-2b', 'vol-b0607d70'), ('10.12.0.20', 'pdx02-cloud-trial/dev-ansible01', 'us-west-2a', 'vol-b32e5c46'), ('10.12.250.7', 'pdx02-he-prod-ansible01', 'us-west-2a', 'vol-fd94400b'), ('10.12.250.4', 'pdx02-he-dev-ansible01', 'us-west-2a', 'vol-ee6abf18'), ('10.12.32.16', 'pdx02-cloud-prod-ansible', 'us-west-2b', 'vol-ae49adbb'), ('10.121.15.22', 'ansible-classic', 'us-west-2a', 'vol-f893c20d'), ('10.17.15.145', 'pdx01-ms-dev-ansible', 'us-west-2a', 'vol-e2d45515')]
>>>
>>> data = zip(*l)
>>>
>>> ip_address = ','.join(next(data))
>>> ip_address
'10.12.250.29,10.12.32.22,10.12.0.20,10.12.250.7,10.12.250.4,10.12.32.16,10.121.15.22,10.17.15.145'
>>>
>>> tags_descrip = ','.join(next(data))
>>> tags_descrip
'pdx02-he-trial-ansible01,pdx02-cloud-prod-ansible01 Clone ,pdx02-cloud-trial/dev-ansible01,pdx02-he-prod-ansible01,pdx02-he-dev-ansible01,pdx02-cloud-prod-ansible,ansible-classic,pdx01-ms-dev-ansible'
>>>
>>> regions_az = ','.join(next(data))
>>> regions_az
'us-west-2a,us-west-2b,us-west-2a,us-west-2a,us-west-2a,us-west-2b,us-west-2a,us-west-2a'
>>>
>>> volume_id = ','.join(next(data))
>>> volume_id
'vol-e7775a10,vol-b0607d70,vol-b32e5c46,vol-fd94400b,vol-ee6abf18,vol-ae49adbb,vol-f893c20d,vol-e2d45515'
和 [[j[0] for j in i] for i in data]
你正在遍历列表,并计算 [j[0] for j in i]
,其中 i
是当前元组。所以你遍历元组,其中 j
是当前字符串,你得到该字符串的第一个字符。
然而,您想要的只是元组的第一项。这意味着您甚至不需要 itertools.chain
:使用 [i[0] for i in data]
即可。这将获取每个元组的第一个元素,并产生预期的输出。
所以这是更改后的代码:
ip_address = ','.join(i[0] for i in data) # you can use an iterator here
tags_descrip = ','.join(i[1] for i in data)
regions_az = ','.join(i[2] for i in data)
volume_id = ','.join(i[3] for i in data)
然而,这可以在一行中完成:
ip_address, tags_descrip, regions_az, volume_id = (','.join(j[i] for j in data) for i in range(4))
另一种可能性如下:(这里你只迭代 data
一次,但它不是那么可读)
ip_address, tags_descrip, regions_az, volume_id = map(','.join, zip(*data))
这是第二个的工作原理(我认为第一个是不言自明的):
您将以下元组传递给 zip
:
IP, ... from the first tuple
IP, ... from the second tuple
.
.
.
所以所有不同的字段都是对齐的。 zip
给你一个迭代器,returns 包含每个列表第一项的元组,然后是包含第二项的元组,依此类推。因此,如果您调用 list(zip(*data))
,您将得到一个包含以下条目的列表:所有 IP,等等。
我会选择第一个选项而不是第二个选项,因为第一个选项的可读性更高,但如果你真的关心性能(但在这种情况下你不会使用 Python,我想),第二个是要走的路。
希望能帮到你,
代号Lambda
我有一个返回列表
[('10.12.250.29', 'pdx02-he-trial-ansible01', 'us-west-2a', 'vol-e7775a10'), ('10.12.32.22', 'pdx02-cloud-prod-ansible01 Clone ', 'us-west-2b', 'vol-b0607d70'), ('10.12.0.20', 'pdx02-cloud-trial/dev-ansible01', 'us-west-2a', 'vol-b32e5c46'), ('10.12.250.7', 'pdx02-he-prod-ansible01', 'us-west-2a', 'vol-fd94400b'), ('10.12.250.4', 'pdx02-he-dev-ansible01', 'us-west-2a', 'vol-ee6abf18'), ('10.12.32.16', 'pdx02-cloud-prod-ansible', 'us-west-2b', 'vol-ae49adbb'), ('10.121.15.22', 'ansible-classic', 'us-west-2a', 'vol-f893c20d'), ('10.17.15.145', 'pdx01-ms-dev-ansible', 'us-west-2a', 'vol-e2d45515'), ('10.21.32.27', 'fra01-cloud-prod-ansible', 'eu-central-1b', 'vol-5f86f5bd'), ('10.21.250.13', 'fra01-he-trial-ansible01', 'eu-central-1a', 'vol-f9e7d220'), ('10.21.250.27', 'fra01-he-dev-ansible01', 'eu-central-1a', 'vol-f6e3fa2f'), ('10.21.0.9', 'fra01-cloud-dev-ansible01', 'eu-central-1a', 'vol-98104671'), ('10.21.250.5', 'fra01-he-prod-ansible01', 'eu-central-1a', 'vol-809b8259'), ('10.31.250.26', 'sin01-he-dev-ansible01', 'ap-southeast-1a', 'vol-86443940'), ('10.31.250.19', 'sin01-he-prod-ansible01', 'ap-southeast-1a', 'vol-bebcc178'), ('10.31.32.12', 'sin01-cloud-prod-ansible01', 'ap-southeast-1b', 'vol-01409de9'), ('10.31.250.27', 'sin01-he-trial-ansible01', 'ap-southeast-1a', 'vol-f6cdc631'), ('10.31.0.18', 'sin01-cloud-dev-ansible01', 'ap-southeast-1a', 'vol-3c0aac28')]
这基本上是:
<IP_ADDRESS> , <AWS_TAG_NAME> , <REGION> , <VOLUME>
现在我将它传递给另一个方法,在该方法中我需要提取每个值并单独存储它所以我使用了 __main__
中的 itertools
:
data = list(itertools.chain(*ansible_box_info))
print "-----------------"
print data
#mapping = {i[0]: [i[1], i[2]] for i in data}
print "Now Calling the Snapshot Creater!"
call_snapshot_creater(data)
def call_snapshot_creater(passed_data):
ip_address = ','.join(list(itertools.chain(*[[j[0] for j in i] for i in data])))
tags_descrip = list(itertools.chain(*[[j[1] for j in i] for i in data]))
regions_az = list(itertools.chain(*[[j[2] for j in i] for i in data]))
volume_id = list(itertools.chain(*[[j[3] for j in i] for i in data]))
这打破了上面的列表,只选择所有内容的第一个字母,即 ip_address 打印:
1,p,u,v,1,p,u,v,1,p,u,v,1,p,u,v,1,p,u,v,1,p,u,v,1,a,u,v,1,p,u,v,1,f,e,v,1,f,e,v,1,f,e,v,1,f,e,v,1,f,e,v,1,s,a,v,1,s,a,v,1,s,a,v,1,s,a,v,1,s,a,v
而不是 10.12.250.29 , 10.12.32.22 ..................... 其他人也一样
如何使用迭代器正确地打破它?
我正在尝试通过将这些值传递给我的快照创建者来创建快照:
def call_snapshot_creater(passed_data):
ip_address = ','.join(list(itertools.chain(*[[j[0] for j in i] for i in data])))
tags_descrip = list(itertools.chain(*[[j[1] for j in i] for i in data]))
regions_az = list(itertools.chain(*[[j[2] for j in i] for i in data]))
volume_id = list(itertools.chain(*[[j[3] for j in i] for i in data]))
regions = ['us-west-2', 'eu-central-1', 'ap-southeast-1']
for region in regions:
ec2 = boto3.resource('ec2', region, aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY, )
print "Snapshot Creation For Ansible -> ",ip_address," initiated , tag = ", tags_descrip ,"region : ", regions_az
print "Snapshot will be created with -> Name : ",tags_descrip
snapshot = ec2.create_snapshot(VolumeId=volume_id, Description=tags_descrip)
print snapshot.id
print "Snapshot is being created for Ansible box ", tags_descrip ,"with snapshot id :",snapshot.id
#slack.chat.post_message(slack_channel,"Creating Snapshot for The volume"+ str(snapshot.id),username='Ansible_box_snapshot_bot')
snapshot.load()
while snapshot.state != 'completed':
print "The Snapshot :", snapshot.id , "for Ansible box named : ", tags_descrip ,"is currently in :",snapshot.state," state"
time.sleep(30)
snapshot.load()
print snapshot.progress
else:
print "Snapshot ",snapshot.id, "for Ansible box ", tags_descrip , "is now Ready!! Final state ->",snapshot.state
您没有正确使用chain
和解包。所有你想做的,都可以用 zip
和 unpacking 来完成,即。 zip(*...)
:
>>> l = [('10.12.250.29', 'pdx02-he-trial-ansible01', 'us-west-2a', 'vol-e7775a10'), ('10.12.32.22', 'pdx02-cloud-prod-ansible01 Clone ', 'us-west-2b', 'vol-b0607d70'), ('10.12.0.20', 'pdx02-cloud-trial/dev-ansible01', 'us-west-2a', 'vol-b32e5c46'), ('10.12.250.7', 'pdx02-he-prod-ansible01', 'us-west-2a', 'vol-fd94400b'), ('10.12.250.4', 'pdx02-he-dev-ansible01', 'us-west-2a', 'vol-ee6abf18'), ('10.12.32.16', 'pdx02-cloud-prod-ansible', 'us-west-2b', 'vol-ae49adbb'), ('10.121.15.22', 'ansible-classic', 'us-west-2a', 'vol-f893c20d'), ('10.17.15.145', 'pdx01-ms-dev-ansible', 'us-west-2a', 'vol-e2d45515')]
>>>
>>> data = zip(*l)
>>>
>>> ip_address = ','.join(next(data))
>>> ip_address
'10.12.250.29,10.12.32.22,10.12.0.20,10.12.250.7,10.12.250.4,10.12.32.16,10.121.15.22,10.17.15.145'
>>>
>>> tags_descrip = ','.join(next(data))
>>> tags_descrip
'pdx02-he-trial-ansible01,pdx02-cloud-prod-ansible01 Clone ,pdx02-cloud-trial/dev-ansible01,pdx02-he-prod-ansible01,pdx02-he-dev-ansible01,pdx02-cloud-prod-ansible,ansible-classic,pdx01-ms-dev-ansible'
>>>
>>> regions_az = ','.join(next(data))
>>> regions_az
'us-west-2a,us-west-2b,us-west-2a,us-west-2a,us-west-2a,us-west-2b,us-west-2a,us-west-2a'
>>>
>>> volume_id = ','.join(next(data))
>>> volume_id
'vol-e7775a10,vol-b0607d70,vol-b32e5c46,vol-fd94400b,vol-ee6abf18,vol-ae49adbb,vol-f893c20d,vol-e2d45515'
和 [[j[0] for j in i] for i in data]
你正在遍历列表,并计算 [j[0] for j in i]
,其中 i
是当前元组。所以你遍历元组,其中 j
是当前字符串,你得到该字符串的第一个字符。
然而,您想要的只是元组的第一项。这意味着您甚至不需要 itertools.chain
:使用 [i[0] for i in data]
即可。这将获取每个元组的第一个元素,并产生预期的输出。
所以这是更改后的代码:
ip_address = ','.join(i[0] for i in data) # you can use an iterator here
tags_descrip = ','.join(i[1] for i in data)
regions_az = ','.join(i[2] for i in data)
volume_id = ','.join(i[3] for i in data)
然而,这可以在一行中完成:
ip_address, tags_descrip, regions_az, volume_id = (','.join(j[i] for j in data) for i in range(4))
另一种可能性如下:(这里你只迭代 data
一次,但它不是那么可读)
ip_address, tags_descrip, regions_az, volume_id = map(','.join, zip(*data))
这是第二个的工作原理(我认为第一个是不言自明的):
您将以下元组传递给 zip
:
IP, ... from the first tuple
IP, ... from the second tuple
.
.
.
所以所有不同的字段都是对齐的。 zip
给你一个迭代器,returns 包含每个列表第一项的元组,然后是包含第二项的元组,依此类推。因此,如果您调用 list(zip(*data))
,您将得到一个包含以下条目的列表:所有 IP,等等。
我会选择第一个选项而不是第二个选项,因为第一个选项的可读性更高,但如果你真的关心性能(但在这种情况下你不会使用 Python,我想),第二个是要走的路。
希望能帮到你,
代号Lambda