Python 将字符串类型数组转换为数组
我有这个:Python 将字符串类型数组转换为数组,python,python-2.7,Python,Python 2.7,我有这个: [s[8] = 5, s[4] = 3, s[19] = 2, s[17] = 8, s[16] = 8, s[2] = 8, s[9] = 7, s[1] = 2, s[3] = 9, s[15] = 7, s[11] = 0, s[10] = 9, s[12] = 3, s[18] = 1, s[0] = 4, s[14] = 5, s[7] = 4, s[6] = 2, s[5] = 7, s[13]
[s[8] = 5,
s[4] = 3,
s[19] = 2,
s[17] = 8,
s[16] = 8,
s[2] = 8,
s[9] = 7,
s[1] = 2,
s[3] = 9,
s[15] = 7,
s[11] = 0,
s[10] = 9,
s[12] = 3,
s[18] = 1,
s[0] = 4,
s[14] = 5,
s[7] = 4,
s[6] = 2,
s[5] = 7,
s[13] = 9]
如何将其转换为python数组,在该数组中可以对x:中的项执行
?假设这是一个巨大的字符串,如果希望print s[0]
打印4
,则需要用逗号将其拆分,然后迭代每个项
import re
data = """[s[8] = 5,
s[4] = 3,
s[19] = 2,
s[17] = 8,
s[16] = 8,
s[2] = 8,
s[9] = 7,
s[1] = 2,
s[3] = 9,
s[15] = 7,
s[11] = 0,
s[10] = 9,
s[12] = 3,
s[18] = 1,
s[0] = 4,
s[14] = 5,
s[7] = 4,
s[6] = 2,
s[5] = 7,
s[13] = 9]"""
d = {int(m.group(1)): int(m.group(2)) for m in re.finditer(r"s\[(\d*)\] = (\d*)", data)}
seq = [d.get(x) for x in range(max(d))]
print(seq)
#result: [4, 2, 8, 9, 3, 7, 2, 4, 5, 7, 9, 0, 3, 9, 5, 7, 8, 8, 1]
inputArray = yourInput[1:-1].replace(' ','').split(',\n\n')
endArray = [0]*20
for item in inputArray:
endArray[int(item[item.index('[')+1:item.index(']')])]= int(item[item.index('=')+1:])
print endArray
假设这是一个巨大的字符串,如果要打印print s[0]
以打印4
,则需要用逗号将其拆分,然后遍历每个项目
inputArray = yourInput[1:-1].replace(' ','').split(',\n\n')
endArray = [0]*20
for item in inputArray:
endArray[int(item[item.index('[')+1:item.index(']')])]= int(item[item.index('=')+1:])
print endArray
一种简单的方法,虽然比Kevin的解决方案(不使用正则表达式)效率低,但可以是以下方法(其中some_array
是您的字符串):
输出样本:
{'s[5]': 7, 's[16]': 8, 's[0]': 4, 's[9]': 7, 's[2]': 8, 's[3]': 9, 's[10]': 9, 's[15]': 7, 's[6]': 2, 's[7]': 4, 's[14]': 5, 's[19]': 2, 's[17]': 8, 's[4]': 3, 's[12]': 3, 's[11]': 0, 's[13]': 9, 's[18]': 1, 's[1]': 2, 's8]': 5}
7
一种简单的方法,虽然比Kevin的解决方案(不使用正则表达式)效率低,但可以是以下方法(其中some_array
是您的字符串):
输出样本:
{'s[5]': 7, 's[16]': 8, 's[0]': 4, 's[9]': 7, 's[2]': 8, 's[3]': 9, 's[10]': 9, 's[15]': 7, 's[6]': 2, 's[7]': 4, 's[14]': 5, 's[19]': 2, 's[17]': 8, 's[4]': 3, 's[12]': 3, 's[11]': 0, 's[13]': 9, 's[18]': 1, 's[1]': 2, 's8]': 5}
7
如果希望数组具有与输入字符串中相同的名称,可以使用exec。这不是很像蟒蛇,但它适用于简单的东西
string = ("[s[8] = 5, s[4] = 3, s[19] = 2,"
"s[17] = 8, s[16] = 8, s[2] = 8,"
"s[9] = 7, s[1] = 2, s[3] = 9,"
"s[15] = 7, s[11] = 0, s[10] = 9,"
"s[12] = 3, s[18] = 1, s[0] = 4,"
"s[14] = 5, s[7] = 4, s[6] = 2,"
"s[5] = 7, s[13] = 9]")
items = [item.rstrip().lstrip() for item in string[1:-1].split(",")]
name = items[0].partition("[")[0]
# Create the array
exec("{} = [None] * {}".format(name, len(items)))
# Populate with the values of the string
for item in items:
exec(items[0].partition("[")[0] )
这将生成一个名为“s”的数组,如果缺少索引,它将被初始化为“无”如果希望该数组与输入字符串中的名称相同,可以使用exec。这不是很像蟒蛇,但它适用于简单的东西
string = ("[s[8] = 5, s[4] = 3, s[19] = 2,"
"s[17] = 8, s[16] = 8, s[2] = 8,"
"s[9] = 7, s[1] = 2, s[3] = 9,"
"s[15] = 7, s[11] = 0, s[10] = 9,"
"s[12] = 3, s[18] = 1, s[0] = 4,"
"s[14] = 5, s[7] = 4, s[6] = 2,"
"s[5] = 7, s[13] = 9]")
items = [item.rstrip().lstrip() for item in string[1:-1].split(",")]
name = items[0].partition("[")[0]
# Create the array
exec("{} = [None] * {}".format(name, len(items)))
# Populate with the values of the string
for item in items:
exec(items[0].partition("[")[0] )
这将生成一个名为“s”的数组,如果缺少索引,它将初始化为“无”这是什么样的数据结构?它周围有列表括号,但包含在dict中设置键和值的符号?您确定它是原始代码块吗?这是代码中的字符串吗?然后编辑您的答案,将其称为字符串<代码>“
围绕每个项目预期的输出是什么?请同时提及这是什么类型的数据结构?它周围有列表括号,但包含用于在dict中设置键和值的符号?您确定它是原始代码块吗?这是您代码中的字符串吗?然后编辑您的答案,将其作为字符串。”
每个项目的预期输出是什么?还请提及