Ich habe eine Liste alsWie man itertools verwendet, um die zurückgegebenen Werte von einer Funktion zu einer anderen zu übergeben?
[('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')]
zurückgegeben wird dies ist im Grunde:
<IP_ADDRESS> , <AWS_TAG_NAME> , <REGION> , <VOLUME>
vergangen ich es an eine andere Methode, wo ich jeden Wert extrahieren müssen und speichern sie einzeln so habe ich itertools
aus __main__
:
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]))
Dies bricht die obige Liste und nimmt nur die ersten Buchstaben von allem, das heißt th e ip_adresse druckt:
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
statt 10.12.250.29, 10.12.32.22 .................. und gleiche gilt für andere
, wie zu bedienen die Iteratoren, um das richtig zu brechen?
Ich versuche, Snapshots zu erstellen, indem diese Werte zu meinem Schnappschuss Schöpfer vorbei:
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
Sobald Sie 'chain', Ihre Liste von Tupeln abgeflacht wird. Ist das das Verhalten, das du beabsichtigst? Und was genau versuchst du zu tun? –
Ich denke, Sie wollen '[i [0] für i in Daten]', '[i [1] für i in Daten]', ... und das ist es. – CodenameLambda
Sie gehen gerade durch alle Tupel, und wählen Sie das erste Zeichen jeder Zeichenfolge dort, und übergeben Sie das an "itertools.chain", das ist der Grund für das unerwartete Verhalten. – CodenameLambda