简体   繁体   中英

Creating a list of tuples from an appended list

i want to separate the 4 appended items from their lists into a new list of tuples

loan_amount = [1250.0, 500.0, 1450.0, 200.0, 700.0, 100.0, 250.0, 225.0, 1200.0, 150.0, 600.0, 300.0, 700.0, 125.0, 650.0, 175.0, 1800.0, 1525.0, 575.0, 700.0, 1450.0, 400.0, 200.0, 1000.0, 350.0]
country_name = ['Azerbaijan', 'El Salvador', 'Bolivia', 'Paraguay', 'El Salvador', 'Philippines', 'Philippines', 'Nicaragua', 'Guatemala', 'Philippines', 'Paraguay', 'Philippines', 'Bolivia', 'Philippines', 'Philippines', 'Madagascar', 'Georgia', 'Uganda', 'Kenya', 'Tajikistan', 'Jordan', 'Kenya', 'Philippines', 'Ecuador', 'Kenya']
    time_to_raise = [193075.0, 1157108.0, 1552939.0, 244945.0, 238797.0, 1248909.0, 773599.0, 116181.0, 2288095.0, 51668.0, 26717.0, 48030.0, 1839190.0, 71117.0, 580401.0, 800427.0, 1156218.0, 1166045.0, 2924705.0, 470622.0, 24078.0, 260044.0, 445938.0, 201408.0, 2370450.0]
    num_lenders_total = [38, 18, 51, 3, 21, 1, 10, 8, 42, 1, 18, 6, 28, 5, 16, 7, 54, 1, 18, 22, 36, 12, 8, 24, 8]

    first = []
    i = 0
    for i in range(len(loan_amount)):
        first.append(country_name[i])
        first.append(loan_amount[i])
        first.append(time_to_raise[i])
        first.append(num_lenders_total[i])
        i += 1

    print(first)

output:

['Azerbaijan', 1250.0, 193075.0, 38, 'El Salvador', 500.0, 1157108.0, 18, 'Bolivia', 1450.0, 1552939.0, 51, 'Paraguay', 200.0, 244945.0, 3, 'El Salvador', 700.0, 238797.0, 21, 'Philippines', 100.0, 1248909.0, 1, 'Philippines', 250.0, 773599.0, 10, 'Nicaragua', 225.0, 116181.0, 8, 'Guatemala', 1200.0, 2288095.0, 42, 'Philippines', 150.0, 51668.0, 1, 'Paraguay', 600.0, 26717.0, 18, 'Philippines', 300.0, 48030.0, 6, 'Bolivia', 700.0, 1839190.0, 28, 'Philippines', 125.0, 71117.0, 5, 'Philippines', 650.0, 580401.0, 16, 'Madagascar', 175.0, 800427.0, 7, 'Georgia', 1800.0, 1156218.0, 54, 'Uganda', 1525.0, 1166045.0, 1, 'Kenya', 575.0, 2924705.0, 18, 'Tajikistan', 700.0, 470622.0, 22, 'Jordan', 1450.0, 24078.0, 36, 'Kenya', 400.0, 260044.0, 12, 'Philippines', 200.0, 445938.0, 8, 'Ecuador', 1000.0, 201408.0, 24, 'Kenya', 350.0, 2370450.0, 8]

One way to do it with tuple and zip ,

loan_amount = [1250.0, 500.0, 1450.0, 200.0, 700.0, 100.0, 250.0, 225.0, 1200.0, 150.0, 600.0, 300.0, 700.0, 125.0, 650.0, 175.0, 1800.0, 1525.0, 575.0, 700.0, 1450.0, 400.0, 200.0, 1000.0, 350.0]
country_name = ['Azerbaijan', 'El Salvador', 'Bolivia', 'Paraguay', 'El Salvador', 'Philippines', 'Philippines', 'Nicaragua', 'Guatemala', 'Philippines', 'Paraguay', 'Philippines', 'Bolivia', 'Philippines', 'Philippines', 'Madagascar', 'Georgia', 'Uganda', 'Kenya', 'Tajikistan', 'Jordan', 'Kenya', 'Philippines', 'Ecuador', 'Kenya']
time_to_raise = [193075.0, 1157108.0, 1552939.0, 244945.0, 238797.0, 1248909.0, 773599.0, 116181.0, 2288095.0, 51668.0, 26717.0, 48030.0, 1839190.0, 71117.0, 580401.0, 800427.0, 1156218.0, 1166045.0, 2924705.0, 470622.0, 24078.0, 260044.0, 445938.0, 201408.0, 2370450.0]
num_lenders_total = [38, 18, 51, 3, 21, 1, 10, 8, 42, 1, 18, 6, 28, 5, 16, 7, 54, 1, 18, 22, 36, 12, 8, 24, 8]

def merge(list1, list2, list3, list4): 

    merged_list = tuple(zip(list1, list2, list3, list4))  
    return merged_list 

print(merge(country_name, loan_amount, time_to_raise, num_lenders_total)) 

WORKING DEMO: https://rextester.com/CDW41464

If you want a list of tuple from your lists:

loan_amount = [1250.0, 500.0, 1450.0, 200.0, 700.0, 100.0, 250.0, 225.0, 1200.0, 150.0, 600.0, 300.0, 700.0, 125.0, 650.0, 175.0, 1800.0, 1525.0, 575.0, 700.0, 1450.0, 400.0, 200.0, 1000.0, 350.0]
country_name = ['Azerbaijan', 'El Salvador', 'Bolivia', 'Paraguay', 'El Salvador', 'Philippines', 'Philippines', 'Nicaragua', 'Guatemala', 'Philippines', 'Paraguay', 'Philippines', 'Bolivia', 'Philippines', 'Philippines', 'Madagascar', 'Georgia', 'Uganda', 'Kenya', 'Tajikistan', 'Jordan', 'Kenya', 'Philippines', 'Ecuador', 'Kenya']
time_to_raise = [193075.0, 1157108.0, 1552939.0, 244945.0, 238797.0, 1248909.0, 773599.0, 116181.0, 2288095.0, 51668.0, 26717.0, 48030.0, 1839190.0, 71117.0, 580401.0, 800427.0, 1156218.0, 1166045.0, 2924705.0, 470622.0, 24078.0, 260044.0, 445938.0, 201408.0, 2370450.0]
num_lenders_total = [38, 18, 51, 3, 21, 1, 10, 8, 42, 1, 18, 6, 28, 5, 16, 7, 54, 1, 18, 22, 36, 12, 8, 24, 8]

listOfTuple= []
for i in range(len(loan_amount)):
    listOfTuple.append((loan_amount[i],country_name[i], time_to_raise[i], num_lenders_total[i]))
print(listOfTuple)

if you want a list of tuples from your initial lists country_name , time_to_raise , num_lenders_total you may use:

list(zip(country_name, loan_amount, time_to_raise , num_lenders_total))

output:

[('Azerbaijan', 1250.0, 193075.0, 38),
 ('El Salvador', 500.0, 1157108.0, 18),
 ('Bolivia', 1450.0, 1552939.0, 51),
...]

to get your expected output you can use itertools.chain with the built-in function zip :

from itertools import chain

list(chain(*zip(country_name, loan_amount, time_to_raise , num_lenders_total)))

output:

['Azerbaijan',
 1250.0,
 193075.0,
 38,
 'El Salvador',
 500.0,
 1157108.0,
 18,
....]

or, as suggested by @benvc you may use:

list(chain.from_iterable(zip(country_name, loan_amount, time_to_raise, num_lenders_total)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM