简体   繁体   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

So i am a noob in python and i don't usually ask a question unless i have searched exhaustively or tried several workarounds.I'm Creating an 8bit binary adder, by simulating the logic of various circuit gates..... 所以我是python的菜鸟,除非我进行了详尽的搜索或尝试了几种解决方法,否则我通常不会问一个问题。我正在通过模拟各种电路门的逻辑来创建一个8位二进制加法器.....

Basically i want to take each element in the two lists and feed them into a function that loops 8 times. 基本上,我想将两个列表中的每个元素都放入一个循环8次的函数中。 (8 bits) (8位)

Creating two strings. 创建两个字符串。 (int can't start with a 0) (int不能以0开头)

example1 = '00001001'
example2 = '11011100'

Assigning the slices to separate strings. 将切片分配给单独的字符串。 One character each. 每个字符一个。

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]

Adding those slices to lists and converting them into an int. 将这些切片添加到列表并将其转换为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)]

Main function. 主功能。 Takes two inputs. 接受两个输入。 One from each list... (aToInt and bToInt) (a,b)(a1,b1,a2,b2,a3,b3.....) 每个列表中的一个...(aToInt和bToInt)(a,b)(a1,b1,a2,b2,a3,b3 .....)

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

a and b need to change to a1,b1,a2,b2,a3,b3... ++ each interation of the loop.. 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()

Other functions that also take two inputs... 其他功能也需要两个输入...

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)

Any suggestions and recommendations much appreciated. 任何建议和建议,不胜感激。

=============================EDIT==================== SPECIFICS ============================编辑====================规格

So i have a list and i want to loop through that list 所以我有一个清单,我想遍历该清单

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


def main():

=code goes here for loop that changes a and b according to the list? = code转到此处以根据列表更改a和b的循环? (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

So i don't know what to call it but i want to be able to maybe map a,b onto a1,b1.... respectively. 所以我不知道该怎么称呼,但我希望能够将a,b分别映射到a1,b1....。

I think you are looking for zip() function 我认为您正在寻找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

If you have two lists of numbers: 如果您有两个数字列表:

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

and a function: 和一个功能:

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

you can use zip to iterate over both of them: 您可以使用zip遍历这两个方法:

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

prints: 印刷品:

11
22
33
44

I think your adder is implemented wrong, but you want something like this after you fix it. 我认为您的加法器实现有误,但是修复它后需要这样的东西。

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)

Call main(a_to_int, b_to_int) and see what it does. 调用main(a_to_int, b_to_int)并查看其作用。

Note the following salient points: 请注意以下要点:

  • Use list comprehensions; 使用列表理解; don't type things out 8 times. 不要将内容输入8次。
  • Python has native bitwise operations for AND, OR, XOR, and NOT. Python具有AND,OR,XOR和NOT的本地按位运算
  • zip pairs up two iterables, and so the for loop iterates over corresponding bits of a and b . zip将两个可迭代对象配对,因此for循环在ab相应位上a迭代。 But it does so from left to right, which is probably not what you want. 但是这样做是从左到右,这可能不是您想要的。
  • return is not a function; return不是函数; it is conventional not to enclose a returned value in parentheses. 通常,不将返回的值括在括号中。
  • a or b is 1 does not mean a is 1 or b is 1 , but rather a or (b is 1) . a or b is 1并不意味着a is 1 or b is 1 ,而是a or (b is 1)
  • Do not test for integer equality using is . 不要使用is测试整数相等is Use == . 使用==
  • a{i++} is not valid Python, and does not resemble any valid Python. a{i++}是无效的Python,并且与任何有效的Python都不相似。 I would tell you what to replace it with, but I don't even know what you want it to mean. 我会告诉你用什么代替它,但是我什至不知道你想要什么。
  • Python has native binary literals, written like so: 0b00001001 , which equals 9 . Python具有本机二进制文字,其写法如下: 0b00001001 ,等于9

In short, what you actually want (a working full-adder) is this: 简而言之,您真正想要的(有效的全加器)是这样的:

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]

Now, main(example_a, example_b) == '11100101' , as you would expect. 现在,就像您期望的那样, main(example_a, example_b) == '11100101'

I believe you're looking for something like this: 我相信您正在寻找这样的东西:

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))

where the lowest bits of a and b come first , as your code seemed to assume. 正如您的代码所假设的那样, ab最低在前 The output of the above is 11010011 , which is 00001001 + 11011100 (again, lowest bits are first). 上面的输出是11010011 ,即00001001 + 11011100 (同样,最低位在前)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM