简体   繁体   English

Python中字符串的条件显示

[英]conditional display of string in Python

I have a program for enumerating the users with administrative privileges on Windows. 我有一个程序用于在Windows上枚举具有管理权限的用户。 I also want to display the number of accounts found which is stored in a variable called num_administrator . 我还想显示找到的帐户数量,该帐户存储在名为num_administrator的变量中。 I have the following piece of code: 我有以下代码:

if num_administrators > 1:
    print("[*] {} accounts with administrative privileges found:\n".format(num_administrators))
    show_admins()
elif num_administrators == 1:
    print("[*] {} account with administrative privileges found:\n".format(num_administrators))
    show_admins()
else:
    print("[*] No accounts with administrative privileges found.\n")

If there aren't admins I would like to print [*] No accounts with administrative privileges found. 如果没有管理员我想打印[*] No accounts with administrative privileges found. If there is 1 or more admins the message to display is almost the same, the only difference is the display of account or accounts according to the number. 如果有一个或多个管理员要显示的消息几乎相同,唯一的区别是根据号码显示accountaccounts It's only a matter of 1 letter ( s ). 它只有1个字母(的事s )。 Can I achieve the conditional print just by using a unique statement or anyway in a simpler way? 我是否可以通过使用唯一的声明或以更简单的方式实现条件打印? Is it possible to print something like: 是否可以打印如下内容:

print("[*] {} account".format(num_administrators) + if num_administrators > 1 "s" + "with administrative privileges found:\n")

Don't mind my code above, I don't know the syntax if what I'm doing makes sense, it's just to give you an idea of what I'm thinking and you can tell me if it's doable or not. 不要介意上面的代码,我不知道语法,如果我正在做的事情有意义,它只是让你知道我在想什么,你可以告诉我它是否可行。

Besides I'm calling the function show_admins() 3 times (for printing the admin accounts) but actually I can call it just once at the end I guess. 除了我调用函数show_admins() 3次(用于打印管理员帐户),但实际上我可以在最后调用它一次,我猜。

Here's a crafty one-liner I made: 这是我制作的狡猾的单线:

"[*] {} account{} with administrative privileges found.\n".format("No" if num_administrators == 0 else str(num_administrators), "s" if num_administrators != 1 else "")

PS As for readability, I don't know... I maybe be wrong, but I think my eyes are bleeding PS至于可读性,我不知道......我可能错了,但我认为我的眼睛正在流血

If you want to go with one line solution, that's already been answered here. 如果您想使用单线解决方案,那么这里已经得到了解答。 However, I'd recommend using if/else statements because that way, the solution is more readable: 但是,我建议使用if / else语句,因为这样,解决方案更具可读性:

output = "[*] "

if num_administrators > 1:
    output += "{} accounts ".format(num_administrators)
elif num_administrators == 1:
    output += "1 account "
else:
    output += "No accounts "

output += "with administrative privileges found:\n"

You can do something like 你可以做点什么

 if num_administrators > 1:
       print "The total number of administrators is %d"%(num_administrators)

or, if you want it to be a string: 或者,如果你想要它是一个字符串:

 if num_administrators > 1:
       print "The total number of administrators is %s"%(str(num_administrators))

So here the simplest way is to use lambda: 所以这里最简单的方法是使用lambda:

l = lambda x:'No' if x == 0 else x
print("[*] {} accounts with adminstrative priveleges".format(l(num_admin)))

I took a look at dmitryro 's solution and I like the use of lambda for displaying different strings according to a condition. 我看了一下dmitryro的解决方案,我喜欢使用lambda根据条件显示不同的字符串。 I also would like to take into account the case of num_admin == 1 though. 我还想考虑num_admin == 1的情况。 I edited the code and I got this: 我编辑了代码,我得到了这个:

l = lambda x:"No accounts" if x == 0 else ("accounts" if x > 1 else "account")

Then I edited a bit also the print statement: 然后我还编辑了一下print语句:

print("[*] {} {} with administrative privileges found".format(num_admin, l(num_admin)))

I had to add another placeholder {} for displaying the number and I added another parameter num_admin in the format() function. 我不得不添加另一个占位符{}来显示数字,我在format()函数中添加了另一个参数num_admin

Now It's almost perfect but there is still a problem: 现在它几乎完美但仍有问题:

example: 例:

for num_admin = 1 I get: [*] 1 account with administrative privileges found 对于num_admin = 1我得到: [*] 1 account with administrative privileges found

for num_admin == 2 I get: [*] 2 accounts with administrative privileges found 对于num_admin == 2我得到: [*] 2 accounts with administrative privileges found

but for num_admin == 0 I get: [*] 0 No accounts with administrative privileges found 但对于num_admin == 0我得到: [*] 0 No accounts with administrative privileges found

I would like to avoid the display of the number 0 in front of the sentence, anyway I think I'm complicating my life and it'd be still acceptable. 我想避免在句子前面显示数字0 ,无论如何我认为我使我的生活变得复杂并且仍然可以接受。 Besides I should consider readability as an important aspect. 此外,我应该将可读性视为一个重要方面。


I took a look at On a Friday 's answer again and it actually addresses this problem: 我再次看一下星期五的答案,它实际上解决了这个问题:

I think in fact 事实上我认为

print("[*] {} account{} with administrative privileges found.\\n".format("No" if num_administrators == 0 else str(num_administrators), "s" if num_administrators != 1 else ""))

seems a perfect one-liner, although I agree readability might be an issue. 虽然我同意可读性可能是一个问题,但它似乎是一个完美的单行。 It certainly doesn't adhere to PEP 8 : line too long (175 > 79 characters). 它肯定不符合PEP 8 line too long (175 > 79 characters).

I also like this solution because it avoids using lambda and uses conditional expressions , I think it's simpler. 我也喜欢这个解决方案,因为它避免使用lambda并使用条件表达式 ,我认为它更简单。

See here for a reference: 见这里参考:

- Conditional expressions - 条件表达式

- Is there an equivalent of C's ”?:” ternary operator? - 是否有相当于C的“?:”三元运算符?

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

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