Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/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
Python 带撇号和列表的错误_Python_Arrays_Python 3.x_List_Dictionary - Fatal编程技术网

Python 带撇号和列表的错误

Python 带撇号和列表的错误,python,arrays,python-3.x,list,dictionary,Python,Arrays,Python 3.x,List,Dictionary,我试图在一个列表的开始和结束处插入括号,该列表由两个列表组成,通过.extend将它们放在一起,这样它就会像这样打印:“dog”,“cat”,“mousepig”,“cow”,“sheep”。然而,我得到的输出是‘狗’、‘猫’、‘老鼠’、‘猪’、‘牛’、‘羊’。虽然可以通过多种方式插入括号,如chr40等,但明显的缺陷是括号以引号形式输出 .加入我知道可以用于整个列表。然而,我还没有找到一种方法将其用于一个项目——是否存在这种方法 我的代码如下所示: all_animals= [] house_

我试图在一个列表的开始和结束处插入括号,该列表由两个列表组成,通过.extend将它们放在一起,这样它就会像这样打印:“dog”,“cat”,“mousepig”,“cow”,“sheep”。然而,我得到的输出是‘狗’、‘猫’、‘老鼠’、‘猪’、‘牛’、‘羊’。虽然可以通过多种方式插入括号,如chr40等,但明显的缺陷是括号以引号形式输出

.加入我知道可以用于整个列表。然而,我还没有找到一种方法将其用于一个项目——是否存在这种方法

我的代码如下所示:

all_animals= []
house_animals= ['dog','cat','mouse']
farm_animals= ['pig', 'cow', 'sheep']
all_animals.extend(house_animals)
all_animals.extend(farm_animals)
print(str(all_animals)[1:-1])
print(''.join(['{0}'.format(tuple(animal)) for animal in all_animals]))
编辑

类似地,如果字典中有一个撇号“[bear with me:it gets to List],那么输出会受到影响,因为它会用引号而不是普通的撇号打印特定的单词。示例:living_beings={爬行动物:蛇,哺乳动物:鲸,其他:鸟}如果您使用以下代码,我需要:

new= []
for i in living_beings:
    r=living_beings[i]
    new.append(r)

然后输出是snake的,'whale','bird',注意第一个输出和其他输出之间的差异。所以我的问题是:如何阻止撇号影响输出。

您可以将列表转换为元组,并在打印前将它们放入列表中:

house_animals = ['dog','cat','mouse']
farm_animals = ['pig', 'cow', 'sheep']
all_animals = [tuple(house_animals), tuple(farm_animals)]

print(''.join(str(x) for x in all_animals))
输出:

('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')
('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')
('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')
替代解决方案更接近您的方法,但带有附加

输出:

('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')
('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')
('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')
编辑 这正是Python表示字符串的方式:

>>> "snake's"
"snake's"
>>> "whale"
'whale'
它与词典或列表没有什么特别的关系

例如:

living_beings= {"Reptile":"Snake's","mammal":"whale", "Other":"bird"} 
new= []
for i in living_beings:
    r=living_beings[i]
    new.append(r)
您可以设置字符串的格式,以将引号全部删除:

print('[{}]'.format(', '.join(new)))

[Snake's, whale, bird]

您可以将列表转换为元组,并在打印前将其放入列表中:

house_animals = ['dog','cat','mouse']
farm_animals = ['pig', 'cow', 'sheep']
all_animals = [tuple(house_animals), tuple(farm_animals)]

print(''.join(str(x) for x in all_animals))
输出:

('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')
('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')
('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')
替代解决方案更接近您的方法,但带有附加

输出:

('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')
('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')
('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')
编辑 这正是Python表示字符串的方式:

>>> "snake's"
"snake's"
>>> "whale"
'whale'
它与词典或列表没有什么特别的关系

例如:

living_beings= {"Reptile":"Snake's","mammal":"whale", "Other":"bird"} 
new= []
for i in living_beings:
    r=living_beings[i]
    new.append(r)
您可以设置字符串的格式,以将引号全部删除:

print('[{}]'.format(', '.join(new)))

[Snake's, whale, bird]

<>首先考虑将农场/家畜作为一个列表,而不是每个动物单独:

all_animals = []
all_animals.append(house_animals)
all_animals.append(farm_animals)
然后,您要查找的打印语句可以如下所示:

all_animals= []
house_animals= ['dog','cat','mouse']
farm_animals= ['pig', 'cow', 'sheep']
all_animals.extend(house_animals)
all_animals.extend(farm_animals)
print(str(all_animals)[1:-1])
print(''.join(['{0}'.format(tuple(animal)) for animal in all_animals]))

<>首先考虑将农场/家畜作为一个列表,而不是每个动物单独:

all_animals = []
all_animals.append(house_animals)
all_animals.append(farm_animals)
然后,您要查找的打印语句可以如下所示:

all_animals= []
house_animals= ['dog','cat','mouse']
farm_animals= ['pig', 'cow', 'sheep']
all_animals.extend(house_animals)
all_animals.extend(farm_animals)
print(str(all_animals)[1:-1])
print(''.join(['{0}'.format(tuple(animal)) for animal in all_animals]))

另一种方法可以是:

"".join(
     [str(tuple(house_animals)), str(tuple(farm_animals))]
)
输出:

('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')
('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')
('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')

另一种方法可以是:

"".join(
     [str(tuple(house_animals)), str(tuple(farm_animals))]
)
输出:

('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')
('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')
('dog', 'cat', 'mouse')('pig', 'cow', 'sheep')