简体   繁体   English

Python 字符串列表和条件语句

[英]Python list of strings and conditional statement

I have this bit of code that I am stuck with and I am not sure why it won't execute the else statement.我有这段代码,但我不确定为什么它不执行 else 语句。 If x is "100" I would like to to convert decimal value to 6 bits otherwise for x = "115", "130", "145" I would like convert decimal value to 8 bits.如果 x 是“100”,我想将十进制值转换为 6 位,否则对于 x =“115”、“130”、“145”,我想将十进制值转换为 8 位。

string_list = ["100", "115", "130", "145"]

if (x == "100" for x in string_list):
    dec_to_bin = "{0:06b}".format()
    
else:
    dec_to_bin = "{0:08b}".format()

However, it doesn't carry on the else statements and print everything in 6 bits.但是,它不执行 else 语句并以 6 位打印所有内容。

I want the answer to be something like this我希望答案是这样的

100 6bits 100个6位

115 8bits 115个8位

130 8bits 130个8位

145 8bits 145个8位

As @Sayse mentioned in comment, the usage of for loop is incorrect:正如@Sayse 在评论中提到的, for循环的用法是不正确的:

Try this:尝试这个:

string_list = ["100", "115", "130", "145"]
res = []

for x in string_list:
    if x == "100":
        dec_to_bin = "{0:06b}".format(int(x))
    else:
        dec_to_bin = "{0:08b}".format(int(x))
    res.append(dec_to_bin)

print(res) #Will print ['1100100', '01110011', '10000010', '10010001']

You're putting the condition which returns a True/False response after the if in parantheses, so it doesn't iterate through every element.您将返回 True/False 响应的条件放在括号中的 if 之后,因此它不会遍历每个元素。 You are basicly writing "if there is an x=="100" in string_list, do this".您基本上是在写“如果 string_list 中有 x==”100”,则执行此操作”。 For it to work for every element, just write it:为了让它适用于每个元素,只需编写它:

for x in string_list:
  if x == "100":
    dec_to_bin = ...
  else:
    dec_to_bin = ...

When you write your if statement by passing (x == "100" for x in string_list) in the condition you are creating a generator which is not None , hence trigerring the if statement.当您在条件下通过传递(x == "100" for x in string_list)来编写if语句时,您正在创建一个不是None的生成器,因此会触发if语句。

It is not iterating over each element as you are hoping it would.它并没有像您希望的那样遍历每个元素。 For it to iterate over each element, write the if statement inside the for loop.为了让它遍历每个元素,在 for 循环中编写 if 语句。

The else statement is never executed. else语句从不执行。 Your fist statement is an if statement.你的第一个声明是一个if声明。

You want to assess is a specific item is equal to '100' , and do something if it is.您要评估某个特定项目是否等于'100' ,如果是,则执行某项操作。 However the line if (x == "100" for x in string_list) actually generates an item called generator which is not None .然而,行if (x == "100" for x in string_list)实际上生成了一个名为generator的项目,它不是None Hence the fact that the if statement is always executed: your generator is always truthy.因此 if 语句总是被执行的事实:你的生成器总是真实的。

You should try and parse the list, and only then for each item use an if/else statement:您应该尝试解析列表,然后才对每个项目使用if/else语句:

for item in string_list :
  if item == '100':
    # action
  else :
    # other action

To get that output from those input data then:要从这些输入数据中获取 output,则:

string_list = ["100", "115", "130", "145"]

for e in string_list:
    n = 6 if e == '100' else 8
    print(f'{e} {n}bits')

Output: Output:

100 6bits
115 8bits
130 8bits
145 8bits

Alternatively:或者:

string_list = ["100", "115", "130", "145"]

for e in string_list:
    n = 6 if e == '100' else 8
    print(f'{e} {int(e):0{n}b}')

Output: Output:

100 1100100
115 01110011
130 10000010
145 10010001

Note:笔记:

Not sure of the relationship between "100" and 6 because the binary representation of 100 (base 10) requires 7 bits不确定“100”和 6 之间的关系,因为 100(基数 10)的二进制表示需要 7 位

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

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