“Urutkan 2 daftar Python bersama” Kode Jawaban

Urutkan dua daftar menjadi satu ular python

list1 = [3,2,4,1,1]
list2 = ['three', 'two', 'four', 'one', 'one2']
list1, list2 = zip(*sorted(zip(list1, list2)))
Comfortable Cockroach

Urutkan dua daftar yang saling berlomba

>>> list1 = [3,2,4,1, 1]
>>> list2 = ['three', 'two', 'four', 'one', 'one2']
>>> list1, list2 = zip(*sorted(zip(list1, list2)))
>>> list1
(1, 1, 2, 3, 4)
>>> list2 
('one', 'one2', 'two', 'three', 'four')
Tame Tamarin

Urutkan 2 daftar Python bersama

D=[5,4,2,1,3]
C=["five","four","two","one","three"]
#Combine lists in tuples (5,"five"),(4,"four")...
zipped_lists=zip(D,C)
#Sorts by both values
sorted_pairs=sorted(zipped_lists)
#Unpacks and combines tuples like this (1,2,...) and ("one","two",...)
tuples=zip(*sorted_pairs)
#Lists now sorted
D,C=[list(tuple) for tuple in tuples]
Panicky Platypus

Urutkan 2 daftar Python bersama

D=[5,4,2,1,3]
C=["five","four","two","one","three"]
#Combine lists in tuples (5,"five"),(4,"four")...
zipped_lists=zip(D,C)
#Sorts by both values
sorted_pairs=sorted(zipped_lists)
#Unpacks and combines tuples like this (1,2,...) and ("one","two",...)
tuples=zip(*sorted_pairs)
#Lists now sorted
D,C=[list(tuple) for tuple in tuples]
Panicky Platypus

Urutkan 2 daftar Python bersama

D=[5,4,2,1,3]
C=["five","four","two","one","three"]
#Combine lists in tuples (5,"five"),(4,"four")...
zipped_lists=zip(D,C)
#Sorts by both values
sorted_pairs=sorted(zipped_lists)
#Unpacks and combines tuples like this (1,2,...) and ("one","two",...)
tuples=zip(*sorted_pairs)
#Lists now sorted
D,C=[list(tuple) for tuple in tuples]
Panicky Platypus

Jawaban yang mirip dengan “Urutkan 2 daftar Python bersama”

Pertanyaan yang mirip dengan “Urutkan 2 daftar Python bersama”

Lebih banyak jawaban terkait untuk “Urutkan 2 daftar Python bersama” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya