VERSION = ["'pilot-2'", "'pilot-1'"]
VERSIONS_F = []
for item in VERSION:
temp = item.replace('"','')
VERSIONS_F.append(temp)
print (VERSIONS_F)
在上面的代码块VERSIONS_F
中也打印了相同的["'pilot-2'", "'pilot-1'"]
,但是我需要类似['pilot-2', 'pilot-1']
的东西。我什至尝试了strip('"')
,但没有看到我想要的东西。
答案 0 :(得分:1)
您可以执行以下几行操作:
from torchvision import datasets, models, transforms
model_ft = models.vgg16_bn(pretrained=True)
model_ft.classifier[6] = nn.Linear(in_features=4096,
out_features=1024,
bias=True)
model_ft.classifier[7] = nn.ReLU()
model_ft.classifier[8] = nn.Dropout(p=0.5)
model_ft.classifier[9] = nn.Linear(in_features=1024,
out_features=10,
bias=True)
Traceback (most recent call last):
File "<ipython-input-14-519d30b6f8d1>", line 5, in <module>
model_ft.classifier[7] = nn.ReLU()
File "C:\Users\frmas\anaconda3\envs\tfgpu\lib\site-packages\torch\nn\modules\container.py", line
86, in __setitem__
key = self._get_item_by_idx(self._modules.keys(), idx)
File "C:\Users\frmas\anaconda3\envs\tfgpu\lib\site-packages\torch\nn\modules\container.py", line
74, in _get_item_by_idx
raise IndexError('index {} is out of range'.format(idx))
IndexError: index 7 is out of range
输出:
VERSION = ["'pilot-2'", "'pilot-1'"]
VERSIONS_F = [item [1:-1] for item in VERSION]
print(VERSIONS_F)
通过这种方式,可以简单地从字符串中切出第一个和最后一个字符,前提是假定“”始终位于第一个和最后一个位置。
注意:Grismar也很好地概述了幕后发生的事情
答案 1 :(得分:0)
打印列表时,Python将打印列表的表示形式,因此列表内部的字符串不会像通常的字符串那样打印:
>>> print('hello')
hello
相比:
>>> print(['hello'])
['hello']
添加不同的引号会使Python选择相反的引号来表示字符串:
>>> print(['\'hello\''])
["'hello'"]
>>> print(["\"hello\""])
['"hello"']
Python程序员经常会犯这样的错误:将控制台上显示的内容与实际值混淆。 print(x)
不会向您显示x
的实际值(无论是多少),但是会显示其文本字符串表示形式。
例如:
>>> x = 0xFF
>>> print(x)
255
在这里,一个值被分配为其十六进制表示形式,但是实际值当然只有255(以十进制表示形式),而十进制表示形式是打印整数值时选择的标准表示形式。
变量的“实际”值是一个抽象的数字值,表示该变量时所做的选择不会影响该值。
在您的情况下,您使用VERSION = ["'pilot-2'", "'pilot-1'"]
将字符串定义为在字符串中包含单引号。因此,如果要删除这些单引号,可以:
VERSION = ["'pilot-2'", "'pilot-1'"]
VERSIONS_F = []
for item in VERSION:
temp = item.replace("'",'')
VERSIONS_F.append(temp)
print (VERSIONS_F)
结果:
['pilot-2']
['pilot-2', 'pilot-1']
或更简单地说:
VERSIONS_F = [v.strip("'") for v in VERSION]
针对评论:
VERSION = ["'pilot-2'", "'pilot-1'"]
temp_list = ['pilot-1', 'test-3']
print(any(x in [v.strip("'") for v in VERSION] for x in temp_list))
答案 2 :(得分:0)
尝试一下:
VERSION = ["'pilot-2'", "'pilot-1'"]
VERSIONS_F = []
for item in VERSION:
temp = item.replace("'",'')
VERSIONS_F.append(temp)
print (VERSIONS_F)
它将打印 ['pilot-2','pilot-1']