簡體   English   中英

如何將字符串連接/修改為變量名,以便使用循環將其識別為另一個變量? python的麻煩

[英]How to join/amend a string to the name of a variable so that it is recognized as another variable using a loop? python troubles

所以我是python的菜鳥,除非我進行了詳盡的搜索或嘗試了幾種解決方法,否則我通常不會問一個問題。我正在通過模擬各種電路門的邏輯來創建一個8位二進制加法器.....

基本上,我想將兩個列表中的每個元素都放入一個循環8次的函數中。 (8位)

創建兩個字符串。 (int不能以0開頭)

example1 = '00001001'
example2 = '11011100'

將切片分配給單獨的字符串。 每個字符一個。

a1 = example1[7:8]
a2 = example1[6:7]
a3 = example1[5:6]
a4 = example1[4:5]
a5 = example1[3:4]
a6 = example1[2:3]
a7 = example1[1:2]
a8 = example1[0:1]

b1 = example2[7:8]
b2 = example2[6:7]
b3 = example2[5:6]
b4 = example2[4:5]
b5 = example2[3:4]
b6 = example2[2:3]
b7 = example2[1:2]
b8 = example2[0:1]

將這些切片添加到列表並將其轉換為int。

aToInt = [int(a1),int(a2),int(a3),int(a4),int(a5),int(a6),int(a7),int(a8)]
bToInt = [int(b1),int(b2),int(b3),int(b4),int(b5),int(b6),int(b7),int(b8)]

主功能。 接受兩個輸入。 每個列表中的一個...(aToInt和bToInt)(a,b)(a1,b1,a2,b2,a3,b3 .....)

def main(a,b): 
    for onebit in range(len(aToInt)):
        a = a{i++} ???

a和b需要更改為a1,b1,a2,b2,a3,b3 ... ++循環的每個交互。

       ##something like this maybe ("{a++}")?
       XOR1OUT = XOR(a{++},b{++})  
       print(" 1 XOR: ", XOR1OUT)

       AND1OUT = AND(a,b)
       print(" 1 AND: ",AND1OUT)

       AND2OUT = AND(XOR1OUT,c0)
       print(" 2 ANDL ", AND2OUT)

       CARRYOROUT = OR(AND1OUT,AND2OUT)
       print(" CARRY: ", CARRYOROUT)

       XOR2OUT = XOR(XOR1OUT,c0)
       print("final value: ", XOR2OUT)

main()

其他功能也需要兩個輸入...

def OR(a,b):
    if a or b is 1:
        OR11 = 1
        return(OR11)
    else:
        OR10 = 0
        return(OR10)

def XOR(a,b):
    if a == b:
        XOR10 = 0
        return(XOR10)
    else:
        XOR11 = 1
        return(XOR11)

def AND(a,b):
    if a == 1 and b == 1:
        AND11 = 1
        return(AND11)
    elif a and b == 0:
        return(0)
    else:
        return(0)

任何建議和建議,不勝感激。

============================編輯====================規格

所以我有一個清單,我想遍歷該清單

list = [a1,b1,a2,b2,a3,b3....a8,b8]


def main():

= code轉到此處以根據列表更改a和b的循環? (a ...,b .....)?

   1st interation...
   dough = cookie(a1,b1)

   2nd interation...
   bread = bagel(a2,b2)


   2nd interation...
   oil = eggs(a3,a3)

   for 8 interations....


def cookie(a,b):
   if some code
   else some code


def bagel(a,b):
   if some code
   else some code


def eggs(a,b):
   if some code
   else some code

所以我不知道該怎么稱呼,但我希望能夠將a,b分別映射到a1,b1....。

我認為您正在尋找zip()函數

example1 = '00001001'
example2 = '11011100'

for a,b in zip(example1, example2):
    print a, b
    # some_function(int(a), int(b))

output:
0 1
0 1
0 0
0 1
1 1
0 1
0 0
1 0

如果您有兩個數字列表:

a_values = [1, 2, 3, 4]
b_values = [10, 20, 30, 40]

和一個功能:

def func(a, b):
    return a + b

您可以使用zip遍歷這兩個方法:

for a, b in zip(a_values, b_values):
    print(func(a, b))

印刷品:

11
22
33
44

我認為您的加法器實現有誤,但是修復它后需要這樣的東西。

example_a = '00001001'
example_b = '11011100'
a_to_int = [int(char) for char in example_a] # == [0, 0, 0, 0, 1, 0, 0, 1]
b_to_int = [int(char) for char in example_b]

def OR(a, b):
    return a | b

def XOR(a, b):
    return a ^ b

def AND(a, b):
    return a & b

def main(a, b):
    c0 = 0
    result = []
    for bit_a, bit_b in zip(a, b):
        xor1out = XOR(bit_a, bit_b)
        print("a XOR b:", xor1out)

        and1out = AND(bit_a, bit_b)
        print("a AND b:", and1out)

        and2out = AND(xor1out, c0)
        print("(a XOR b) AND c:", and2out)

        carryorout = OR(and1out, and2out)
        print("(a AND b) AND ((a XOR b) AND c):", carryorout)

        xor2out = XOR(xor1out, c0)
        print("(a XOR b) XOR ((a AND b) AND ((a XOR b) AND c))):", xor2out)

        c0 = carryorout
        result.append(xor2out)

    return ''.join(str(bit) for bit in result)

調用main(a_to_int, b_to_int)並查看其作用。

請注意以下要點:

  • 使用列表理解; 不要將內容輸入8次。
  • Python具有AND,OR,XOR和NOT的本地按位運算
  • zip將兩個可迭代對象配對,因此for循環在ab相應位上a迭代。 但是這樣做是從左到右,這可能不是您想要的。
  • return不是函數; 通常,不將返回的值括在括號中。
  • a or b is 1並不意味着a is 1 or b is 1 ,而是a or (b is 1)
  • 不要使用is測試整數相等is 使用==
  • a{i++}是無效的Python,並且與任何有效的Python都不相似。 我會告訴你用什么代替它,但是我什至不知道你想要什么。
  • Python具有本機二進制文字,其寫法如下: 0b00001001 ,等於9

簡而言之,您真正想要的(有效的全加器)是這樣的:

example_a = '00001001'
example_b = '11011100'

to_ints = lambda s: [int(char) for char in s]

def main(a: str, b: str) -> str:
    c = 0
    result = []
    for bit_a, bit_b in reversed(list(zip(to_ints(a), to_ints(b)))):

        s = (bit_a ^ bit_b) ^ c
        c_out = ((bit_a ^ bit_b) & c) | (bit_a & bit_b)

        result.append(s)
        c = c_out

    return ''.join(str(bit) for bit in result)[::-1]

現在,就像您期望的那樣, main(example_a, example_b) == '11100101'

我相信您正在尋找這樣的東西:

a = map(int, '00001001')
b = map(int, '11011100')
#             11010011

def xor(x, y):
    return (x or y) and not (x and y)

carry = 0
bits = []
for a_i, b_i in zip(a, b):
    bit = xor(a_i, b_i)
    new_carry = (bit and carry) or (a_i and b_i)
    bit = xor(bit, carry)
    carry = new_carry
    bits.append(bit)

print(''.join('{:d}'.format(b) for b in bits))

正如您的代碼所假設的那樣, ab最低在前 上面的輸出是11010011 ,即00001001 + 11011100 (同樣,最低位在前)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM