简体   繁体   中英

manipulate tuple into a list of tuples

I have the following variable for class label in my dataset:

y = np.array([3, 3, 3, 2, 3, 1, 3, 2, 3, 3, 3, 2, 2, 3, 2])

To determine the number of each class, I do:

np.unique(y, return_counts=True)
(array([1, 2, 3]), array([1, 5, 9]))

How then do I manipulate this into a list of tuples for (label, n_samples) ? So that I have:

[ (1,1), (2,5), (3,9) ]

If you want a simple list, use zip :

out = list(zip(*np.unique(y, return_counts=True)))

Output: [(1, 1), (2, 5), (3, 9)]

Alternatively, you can create an array with:

np.vstack(np.unique(y, return_counts=True)).T

Output:

array([[1, 1],
       [2, 5],
       [3, 9]])
list_1 = ['a', 'b', 'c']
list_2 = [1, 2, 3]

# option 1
list_of_tuples = list(
    map(
        lambda x, y: (x, y),
        list_1,
        list_2
    )
)

#option 2
list_of_tuples = [
    (list_1[index], list_2[index]) for index in range(len(list_1))
]

# option 3
list_of_tuples = list(zip(list_1, list_2))

print(list_of_tuples)
# output is [('a', 1), ('b', 2), ('c', 3)]

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