☆☆ 新着記事 ☆☆

2019年6月2日日曜日

Zip関数の使い方とソート(lambda x: x[0])


import random
list1 = [random.randint(0,99) for i in range(10)]
list2 = [random.randint(100,199) for i in range(10)]
list3 = [random.randint(200,299) for i in range(10)]

print(list1)
print(list2)
print(list3)
[21, 87, 82, 63, 97, 79, 39, 17, 53, 67]                                        
[159, 181, 105, 146, 144, 192, 174, 152, 124, 169]                                
[219, 215, 275, 273, 287, 234, 233, 229, 209, 263] 

data =zip(list1,list2,list3)

for two_digits, One_hundreds, Two_hundreds in zip(list1,list2,list3):
    print(two_digits, One_hundreds, Two_hundreds)

                                   
21 159 219                                                                              
87 181 215                                                                              
82 105 275                                                                              
63 146 273                                                                              
97 144 287                                                                              
79 192 234                                                                              
39 174 233                                                                              
17 152 229                                                                              
53 124 209                                                                              
67 169 263                             


data_listed = list(data) #List化する
print(data_listed)
[(21, 159, 219), (87, 181, 215), (82, 105, 275), (63, 146, 273), (97, 144, 287), (79, 1  
92, 234), (39, 174, 233), (17, 152, 229), (53, 124, 209), (67, 169, 263)]  
    

res0 = sorted(data_listed, key = lambda x: x[0]#lambdaでソートする
print(res0)
#
[(17, 152, 229), (21, 159, 219), (39, 174, 233), (53, 124, 209), (63, 146, 273), (67, 1  
69, 263), (79, 192, 234), (82, 105, 275), (87, 181, 215), (97, 144, 287)]                

res1 = sorted(data_listed, key = lambda x: x[1])
print(res1)
[(82, 105, 275), (53, 124, 209), (97, 144, 287), (63, 146, 273), (17, 152, 229), (21, 1  
59, 219), (67, 169, 263), (39, 174, 233), (87, 181, 215), (79, 192, 234)]                

res2 = sorted(data_listed, key = lambda x: x[2])
print(res2)
[(53, 124, 209), (87, 181, 215), (21, 159, 219), (17, 152, 229), (39, 174, 233), (79, 1  
92, 234), (67, 169, 263), (63, 146, 273), (82, 105, 275), (97, 144, 287)]                

*降順にするのは、
(data_listed, key = lambda x: x[1], reverse=True) 

#
# Pyhton code to demonstrate
# sort zipped list by values
# using operator and sorted
 
import operator
# Declaring initial lists
list1 = ['akshat', 'Manjeet', 'nikhil']
list2 = [3, 2, 1]
zipped = zip(list1, list2)
 
# Converting to list
zipped = list(zipped)
 
# Printing zipped list
print("Initial zipped list - ", str(zipped))
 
# Using sorted and operator
res = sorted(zipped, key = operator.itemgetter(1))
     
# printing result
print("final list - ", str(res))

0 件のコメント:

コメントを投稿