How do you split a list of typles into two separate lists?

Viewed 36

i have a list of tuples that i am trying to convert to two different lists called 'list x' and 'list y'. Here is the list.

[('[(0.07, 6380.0), (0.14, 6653.0), (0.3, 6672.0), (0.45, 6699.0), (0.56, 6738.0), (1.01, 6783.0), (1.47, 6827.0), (1.93, 6867.0), (2.39, 6912.0), (3.08, 6942.0), (3.85, 6976.0), (4.61, 6975.0), (5.38, 6972.0), (6.33, 7012.0), (7.38, 7018.0), (8.43, 6990.0), (9.49, 7018.0), (10.67, 7050.0), (11.97, 7060.0), (13.27, 7027.0), (14.57, 7056.0), (15.96, 7104.0), (17.44, 7096.0), (18.93, 7120.0), (20.42, 
7156.0), (21.96, 7180.0), (23.57, 7205.0), (25.19, 7214.0), (26.81, 7303.0), (28.43, 7336.0), (30.11, 7311.0), (31.79, 7278.0), (33.46, 7284.0), (35.14, 7256.0), (36.82, 7194.0), (38.49, 7185.0), (40.16, 7279.0), (41.84, 7325.0), (43.48, 7361.0), (45.11, 7423.0), (46.75, 7532.0), (48.38, 7555.0), (49.94, 7530.0), (51.5, 7509.0), (53.06, 7500.0), (54.63, 7447.0), (56.09, 7420.0), (57.55, 7434.0), (59.02, 7503.0), (60.48, 7635.0), (61.87, 7764.0), (63.22, 7870.0), (64.57, 7916.0), (65.93, 7933.0), (67.22, 7878.0), (68.47, 7754.0), (69.71, 7646.0), (70.96, 7572.0), (72.16, 7531.0), (73.31, 7525.0), 
(74.44, 7602.0), (75.58, 7700.0), (76.69, 7771.0), (77.73, 7840.0), (78.76, 7907.0), (79.8, 7869.0), (80.82, 7776.0), (81.76, 7687.0), (82.69, 7583.0), (83.63, 7489.0), (84.56, 7481.0), (85.4, 7573.0), (86.24, 7709.0), (87.08, 7837.0), (87.91, 7967.0), (88.67, 8059.0), (89.41, 8059.0), (90.15, 7972.0), (90.89, 7837.0), (91.57, 7708.0), (92.21, 7615.0), (92.86, 7599.0), (93.5, 7660.0), (94.1, 7819.0), (94.65, 8009.0), (95.19, 8212.0), (95.74, 8351.0), (96.25, 8390.0), (96.69, 8322.0), (97.13, 8154.0), (97.57, 7961.0), (97.99, 7767.0), (98.33, 7648.0), (98.66, 7674.0), (98.99, 7878.0), (99.31, 8162.0), (99.52, 8426.0), (99.72, 8638.0), (99.93, 8740.0), (100.13, 8566.0), (100.21, 8263.0), (100.28, 7938.0), (100.34, 7636.0), (100.45, 7329.0), (100.35, 6998.0), (100.25, 6652.0), (100.16, 6285.0), (100.06, 6010.0), (99.85, 5813.0), (99.57, 5623.0), (99.29, 5456.0), (99.01, 5167.0), (98.63, 4852.0), (98.15, 4779.0), (97.68, 4722.0), (97.2, 4678.0), (96.64, 4612.0), (95.95, 4572.0), (95.27, 4614.0), (94.58, 4629.0), (93.85, 4643.0), (92.95, 4648.0), (92.06, 4599.0), (91.17, 4504.0), (90.26, 4424.0), (89.18, 4374.0), (88.09, 4303.0), (87.01, 4266.0), (85.93, 4300.0), (84.69, 4358.0), (83.44, 4392.0), (82.19, 4405.0), (80.94, 4429.0), (79.58, 4388.0), (78.19, 4274.0), (76.82, 4167.0), (75.44, 4073.0), (73.98, 3973.0), (72.5, 3915.0), (71.01, 3932.0), (69.54, 3998.0), (68.01, 4083.0), (66.47, 4192.0), (64.92, 4269.0), (63.36, 4242.0), (61.79, 4176.0), (60.2, 4055.0), (58.61, 3881.0), (57.02, 3711.0), (55.43, 3599.0), (53.83, 3571.0), (52.23, 3599.0), (50.63, 3739.0), (49.03, 3972.0), (47.44, 
4185.0), (45.84, 4272.0), (44.25, 4261.0), (42.65, 4187.0), (41.09, 4000.0), (39.53, 3767.0), (37.97, 3589.0), (36.41, 3530.0), (34.91, 3528.0), (33.41, 3646.0), (31.91, 3851.0), (30.41, 4073.0), (28.94, 4220.0), (27.52, 4295.0), (26.09, 4283.0), (24.66, 4053.0), (23.27, 3755.0), (21.95, 3512.0), (20.63, 3368.0), (19.32, 3288.0), (18.01, 3477.0), (16.81, 3780.0), (15.62, 4069.0), (14.42, 4338.0), (13.2, 4566.0), (12.15, 4643.0), (11.1, 4273.0), (10.06, 3955.0), (9.01, 3639.0), (8.09, 3341.0), (7.23, 3059.0), (6.37, 3156.0), (5.51, 3557.0), (4.73, 3935.0), (4.09, 4301.0), (3.43, 4673.0), (2.78, 5027.0), (2.19, 5338.0), (1.78, 5525.0), (1.36, 5754.0), (0.95, 5908.0), (0.57, 6007.0), (0.43, 6117.0), (0.28, 6227.0), (0.14, 6349.0), (0.07, 6380.0)]', )]

I am trying to use the split and reshape option but its throwing an error.

surface_data_card_list = surface_data_card.split(',')
surface_data_card_list = [float(i) for i in surface_data_card_list]
surface_data_card_list = np.reshape(surface_data_card_list,(-1,2))
print(surface_data_card_list)

the error message is

ValueError: could not convert string to float: '(0.07'

is there a simpler way of doing this? The results that i am looking for would look something like this so i can plot it.

list of x values            list of y values
(0.07, 0.14, 0.3, ...), (6380.0, 6653.0, 6672.0, ...)

2 Answers

You can do that with zip:

lst_x, lst_y = list(zip(*lst))

From your given data, you have a list of tuples of strings of lists of tuples (if I'm counting correctly). What you need to do first is to remove both the outmost list and tuple to get to the strings.

mystring = lst[0][0]

Next, you can use eval to turn the strings into list of tuples.

newlist = eval(mystring)

Finally, you can use zip to get your desired output.

a, b = zip(*newlist)
Related