Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从python列表中获取所需的输出_Python - Fatal编程技术网

无法从python列表中获取所需的输出

无法从python列表中获取所需的输出,python,Python,我对下面的python代码有一点疑问。 我有一个包含以下元素的列表。此列表也可能为空,没有内容 如您所见,这是我在2019年1月27日购买的第一支股票,它是编码的,我必须对它进行解码以删除b'。当我在mylist上执行拆分操作时,列表中的第一项是',我不知道为什么 mylist = "$17$b'This is my first stock I bought 01.27.2019'" The fields in mylist are seperated by a $ rather than a

我对下面的python代码有一点疑问。 我有一个包含以下元素的列表。此列表也可能为空,没有内容

如您所见,这是我在2019年1月27日购买的第一支股票,它是编码的,我必须对它进行解码以删除
b'
。当我在mylist上执行拆分操作时,列表中的第一项是
'
,我不知道为什么

mylist = "$17$b'This is my first stock I bought 01.27.2019'"
The fields in mylist are seperated by a $ rather than a ,
我希望我的
res['myinfo']
“{{{17,这是我在2019年1月27日买的第一支股票,},{,},},{,}

有时,如果
mylist=[“”]
,则
res['myinfo']
可能只是“{}”

我不知道如何修复我的代码,如有任何帮助,将不胜感激。

首先

mylist = "$17$b'This is my first stock I bought 01.27.2019'"
应该是

mylist = "17$b'This is my first stock I bought 01.27.2019'"
正如@takendarkk所说的

这可能是因为字符串中的第一个字符是 分隔符($)。在这之前是什么?什么都没有

至于解码字符串,假设它们都像:“b'某个字符串,并且前面有一个年龄,则必须解码列表中每个奇数索引元素。一个非常严格的解决方案可能是:

def decodeString(stringToDecode):
    decodedList = []
    for i in range(2, len(stringToDecode)-1):  #every character except the first 2 and the last one
        decodedList.append(stringToDecode[i])
    decodedString = ''.join(decodedList)
    return decodedString

for i in range(len(tmp_list)):
    if i % 2 == 1:                                 #every odd indexed element
        decodedString = decodeString(tmp_list[i])
        tmp_list[i] = decodedString
而且这里似乎还需要一层“{}”:

res['myinfo']= '{' + '},{'.join(f'{n},{s}' for n, s in zip(tmp_iter, tmp_iter)) + '}'
这样,你的
res['myinfo']
就是
{17,这是我2019年1月27日买的第一支股票,{18,这是我2019年1月28日买的第二支股票}
但是如果你想成为
{17,这是我2019年1月27日买的第一支股票,{18,这是我2019年1月27日买的第二支股票}
,正如你所说的:

res['myinfo']= '{' + '{' + '},{'.join(f'{n},{s}' for n, s in zip(tmp_iter, tmp_iter)) + '}' + '}'

mylist=[$17$b'这是我在2019年1月27日买的第一支股票']
那不是有效的Python代码。那些美元符号在做什么?这些是我列表中的内容。不确定它的无效代码是什么意思?我列表中的字段用
$
分隔,而不是
。这会导致
语法错误
。我猜你实际上有类似
mylist=”$17$b‘这是我在2019年1月27日买的第一支股票’
@RobBricheno正好!!更新了问题“当我在mylist上执行拆分操作时,我将”“作为列表中的第一项”,这可能是因为字符串中的第一个字符是您的分隔符(
$
)。这之前是什么?什么都没有。
res['myinfo']= '{' + '{' + '},{'.join(f'{n},{s}' for n, s in zip(tmp_iter, tmp_iter)) + '}' + '}'