【问题标题】:how to make 1111 into 1 in pythonhow to make 1111 into 1 in python
【发布时间】:2022-12-01 15:17:18
【问题描述】:

i want to make function to make from 1111 into 1 and 0000 into 0.

for the example:

Input:
['1111', '1111', '0000', '1111', '1111', '0000', '1111', '0000', '0000', '1111', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '1111', '0000', '0000', '1111', '1111', '0000', '0000', '0000', '0000', '1111', '0000', '1111', '1111', '0000', '0000']

Desired output:
11011010010000000100110000101100

But i don't know how to make it or the algorithm. Can you help me?

My attempt so far:

def bagiskalar(biner):
    print(biner)
    biner = str(biner)
    n = 4
    hasil = []
    potong = [biner[i:i+n] for i in range(0, len(biner), n)]
    for a in potong:
        hasil = potong.append(a)
    
    return hasil

【问题讨论】:

    标签: python algorithm binary scalar


    【解决方案1】:

    You can easily use list comprehension and the join() method:

    lst = ['1111', '1111', '0000', '1111', '1111', '0000', '1111', '0000', '0000', '1111', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '1111', '0000', '0000', '1111', '1111', '0000', '0000', '0000', '0000', '1111', '0000', '1111', '1111', '0000', '0000']
    s = ''.join(['1' if x == '1111' else '0' for x in lst ])
    print(s)
    # prints 11011010010000000100110000101100
    

    【讨论】:

    • ah yeah! it's work. thankyou!
    猜你喜欢
    • 2022-12-26
    • 2022-12-02
    • 2022-12-02
    • 2022-12-26
    • 2022-12-27
    • 1970-01-01
    • 2022-12-16
    • 1970-01-01
    • 2022-12-02
    相关资源
    最近更新 更多