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, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 2, 3, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 2, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 3, 2, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 2, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, 2, 4, 2, 1, 3, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2]

Yang ingin saya lakukan adalah menyusun semua nilai yang sama mengikuti satu sama lain menjadi string. Contohnya

[1,1,1,1,1,1,1,2,3,1,1,1,2,2,2,2,3,3,4,5,1,1]

harus disusun sebagai berikut:

["1x7",2,3,"1x3",,"2x4",,"3x2",4,5,"1x2"]

Saya rasa mengalikan masing-masing nilai berikut dan memeriksa apakah produk mereka berbeda dari 1,4,9,16,25 dapat bekerja sampai titik tertentu tetapi ketika datang ke daftar yang lebih besar, itu tidak akan cukup.

Apakah ada jalan pintas yang disediakan python?

0
Resul Bulbul 12 Mei 2021, 15:07

2 jawaban

Jawaban Terbaik

Anda dapat menggunakan itertools.groupby untuk mengumpulkan run berturut-turut dengan nilai yang sama.

from itertools import groupby
def condense(group):
    values = list(group)
    if len(values) > 1:
        return '{}x{}'.format(values[0], len(values))
    else:
        return values[0]

Anda dapat menggunakan fungsi ini dalam daftar comprehensino

>>> data = [1, 1, 1, 1, 2, 1, 3, 2, 1, 2, 2, 1, 2, 2, 1, 1, 1,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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 2, 3, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 2, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 3, 2, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 2, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, 2, 4, 2, 1, 3, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2]
>>> [condense(g) for k,g in groupby(data)]
['1x4', 2, 1, 3, 2, 1, '2x2', 1, '2x2', '1x4', 3, '1x2', '2x2', 1, 2, '1x2', 3, '1x4', '2x2', 1, 2, 1, 2, 1, 2, '1x2', 3, '1x7', 2, 1, 2, '1x8', 2, '1x4', '2x2', 3, '1x2', 2, 1, 3, 2, '1x2', 2, 1, '2x2', '1x13', 2, '1x3', '2x2', '1x2', '2x2', '1x3', '2x2', '1x4', 2, '1x3', 2, '1x2', 2, 1, 3, 2, '1x6', 2, '1x2', 3, '1x5', 3, '1x2', 3, '1x2', 2, '1x2', 2, 5, 4, 3, 1, '2x2', '1x4', 2, 3, '1x2', '2x6', 1, 2, '1x6', 2, 3, '1x2', 2, 1, 2, '1x2', 2, 3, '1x8', 4, '1x2', 2, 3, '1x2', 3, 2, '1x2', 2, 3, 1, '2x3', 1, '2x2', '1x3', 2, 3, '1x6', 4, 2, '1x3', 2, 1, '2x4', 1, 2, '1x2', '3x2', '1x2', 2, '1x3', '2x2', '1x2', 2, 3, '1x3', 4, '1x5', 3, 1, 2, 1, 2, 3, '1x2', '2x2', 1, 4, 1, 2, '1x3', 3, '1x2', 3, 2, 1, '2x2', 1, 2, 1, 2, 3, '1x3', 3, 2, 1, 2, '1x2', '2x2', '1x10', '2x2', '1x3', '2x2', '1x2', 2, '1x2', 2, '1x3', 3, '1x3', 2, '1x2', 2, '1x2', 2, '1x2', 3, '1x2', 3, '1x2', 4, 1, 2, '1x2', '3x2', 4, '2x2', 5, '1x8', 3, 4, '1x6', 2, 1, 2, '1x2', 4, 3, '1x3', 3, '1x3', 2, 1, 3, 4, '2x3', '1x2', '2x2', '1x6', 2, 3, '1x2', 2, 3, '1x2', '2x2', '1x2', 2, '1x6', 2, '1x2', 4, '1x2', 3, 4, '1x20', '2x2', 1, '2x2', 1, 2, 1, '2x3', 1, '2x2', '1x3', 5, '1x2', 3, '1x7', '2x2', 3, 4, 2, '1x5', 3, '1x4', 3, '2x2', 1, 3, 4, 2, '1x2', 2, 1, 2, 1, 3, 1, 2, '1x5', 2, '1x3', 2, '1x5', 2, '1x3', 3, 2, '1x2', 4, '1x3', 2, '1x8', 2, 1, 3, '1x2', 3, '1x2', '2x2', '1x2', 2, '1x4', 2, '1x2', '2x2', '1x4', 2, 1, 2, '1x2', 2, '1x4', '2x2', 3, 1, 4, '1x2', 2, '1x6', 2, '1x2', 2, 5, '1x3', 2, '1x3', 2, 1, 4, 1, '3x2', '1x3', 3, '1x6', 2, 1, 2, '1x15', 2, '1x4', '2x2', '1x9', 2, '1x8', 2, '1x3', 2, '1x3', 2, 1, '2x2', 1, '2x2', '1x2', '2x2', 3, 1, 2, 1, '2x2', '1x4', 2, 4, '1x4', '2x2', '1x3', 2, '1x2', 2, '1x10', 2, '1x4', 2, 1, 3, 2, '1x11', 2, '1x5', 2, '1x2', '2x2', '1x3', 2, '1x2', 2, 1, 3, 1, 2, '1x2', 4, 2, '1x2', '3x2', '2x2', '1x3', 4, 3, 2, '1x10', 2, '1x3', '2x2', '1x2', '2x2', '1x6', '2x2', '1x2', 2, '1x2', 2, '1x2', 3, '1x3', 2, '1x3', '2x2', '1x2', 2, '1x2', 2, '1x2', 2, '1x2', 3, '1x2', 2, '1x2', '2x2', '1x4', '2x2', '1x3', 4, 1, 3, '2x2', '1x2', 3, 1, '2x2', 1, '2x2', 3, '1x16', '2x2', '1x7', 3, '1x5', 2, '1x5', 2, '1x5', '2x2', '1x4', 2, '1x2', 2, '1x2', 3, '1x3', 3, '1x4', '2x2', 4, 3, 4, 2, '1x5', '2x3', 3, '1x4', 2, '1x5', 2, '1x4', 2, '1x2', 2, 1, 2, 4, '1x4', '2x2', '1x2', 2, '1x4', 3, '1x3', 3, '1x4', 2, 1, '2x2', 3, 4, '1x2', '2x2', '1x8', 3, 2, '1x2', 3, '1x33', 2, '1x2', 2, '1x3', 2, 3, '1x5', 2, '1x6', 2, '1x5', 2, '1x2', 2, 1, '2x2', '1x5', 2, '1x2', 2, '1x8', 2, '1x3', '2x2', '1x2', 2, '1x9', 3, 2, '1x5', 2, '1x7', 3, '2x2', '1x5', 2, '1x3', 2, '1x10', 3, 1, 2, '1x3', '2x2', '1x2', 2, '1x2', 2, 4, 2, 1, 3, '1x2', 2, '1x6', 2]
1
Cory Kramer 12 Mei 2021, 12:13

Jika Anda hanya ingin data ini mengambil lebih sedikit ruang, Anda dapat menggunakan kamus dengan item sebagai kunci dan jumlah kemunculannya dalam daftar sebagai nilai. Saya menggunakan set untuk menghapus duplikat dan kemudian membuat kamus.

a = [1,1,1,1,1,1,1,2,3,1,1,1,2,2,2,2,3,3,4,5,1,1]
d = {i:a.count(i) for i in set(a)}
print(d)

Keluaran:

{1: 12, 2: 5, 3: 3, 4: 1, 5: 1}

Membaca dari dan menulis ke kamus jauh lebih mudah daripada string dalam bentuk itu. Anda juga dapat dengan mudah menyimpan kamus sebagai file JSON.

PERBARUI:

Jadi jika urutan elemen itu penting. Anda dapat melakukan seperti ini:

a = [1,1,1,1,1,1,1,2,3,1,1,1,2,2,2,2,3,3,4,5,1,1]
b = []
count = 1
for i in range(1,len(a)):
    if a[i-1] != a[i]:
        b.append((a[i-1],count))
        count = 0
    count += 1
b.append((a[-1],count))
print(b)

Keluaran:

[(1, 7), (2, 1), (3, 1), (1, 3), (2, 4), (3, 2), (4, 1), (5, 1), (1, 2)]

Ini akan mengulang daftar dan membuat Tuple dengan item sebagai elemen pertama dan dihitung sebagai elemen kedua.

0
aaronn 12 Mei 2021, 13:08