gpt4 book ai didi

Python仅获取1到255之间的变量值

转载 作者:行者123 更新时间:2023-12-01 02:03:44 24 4
gpt4 key购买 nike

data = """
abcd1 1
abcd2 2
abcd3 3
abcd4 4
abcd5 5
abcd6 6
abcd7 7
abcd8 8
abcd9 9
.
.
.
abcd256 1
abcd257 2
abcd258 3
abcd259 4
abcd260 5
abcd261 6
abcd262 7
abcd263 8
abcd264 9
"""

if abcd1, then Get value 1,
if abcd2, then Get value 2,...so on

if abcd256, then Get value 1,
if abcd257, then Get value 2,

条件值必须在 1 到 255 之间

检查数据变量中是否已存在字符串。我使用了下面的代码:

check = set()
for line in data.split("\n"):
if len(line.split()) > 1:
line = line.strip()
check.add(line.split()[0])

if not "abcd264" in check:
print "Not exist":
value = 9#Help required to get value here

else:
print "Its already exist. Program exit"
sys.exit()

在其他帖子中建议使用 Pandas,但我需要在不使用 Pandas 的情况下实现

最佳答案

如果你想用纯Python来做,你可以尝试这样做:

data = """
abcd1 1
abcd2 2
abcd3 3
abcd4 4
abcd5 5
abcd6 6
abcd7 7
abcd8 8
abcd9 9
abcd256 1
abcd257 2
abcd258 3
abcd259 4
abcd260 5
abcd261 6
abcd262 7
abcd263 8
abcd264 9
"""

data = data.replace(" ","").replace(" "," ").split("\n")[1:-1]
for d in data:
number = int(d.split()[0][4:])
print("For number %d the result is: %d" % (number,number % 255))

输出:

For number 1 the result is: 1
For number 2 the result is: 2
For number 3 the result is: 3
For number 4 the result is: 4
For number 5 the result is: 5
For number 6 the result is: 6
For number 7 the result is: 7
For number 8 the result is: 8
For number 9 the result is: 9
For number 256 the result is: 1
For number 257 the result is: 2
For number 258 the result is: 3
For number 259 the result is: 4
For number 260 the result is: 5
For number 261 the result is: 6
For number 262 the result is: 7
For number 263 the result is: 8
For number 264 the result is: 9

关于Python仅获取1到255之间的变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49277109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com