Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.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_List - Fatal编程技术网

Python 列表以奇怪的方式打印出来

Python 列表以奇怪的方式打印出来,python,list,Python,List,我有一张这样的清单 [79682911591162] 我也有这样一个列表: ['144154','145151','145152','145153','145154','146152','146153','146154','147153','147154'] 这些都不是按比例的 我要做的是使用第一个列表元素作为最后一个数组的索引 我试过这段代码: contacts = [] for i in findres: contacts += rulelines[i] print contacts

我有一张这样的清单

[79682911591162]

我也有这样一个列表:

['144154','145151','145152','145153','145154','146152','146153','146154','147153','147154']
这些都不是按比例的

我要做的是使用第一个列表元素作为最后一个数组的索引

我试过这段代码:

contacts = []
for i in findres:
    contacts += rulelines[i]
print contacts
其中,
findres
是第一个列表,
rulelines
是最后一个列表 但是,这会奇怪地打印出联系人列表:

['5','7','2','5','1','0','5','7','1','5','0','7','1','5','3']

我相信这很容易,但我哪里做错了

我认为理想的输出是
['572'、'5105'、'7150'、7153']


我没有列出所有的列表元素,因为每个列表元素中有100多个元素。当您分配
contacts=rulelines[I]
时,您实际上是在分配
rulelines[I]
字符串。您应该执行
contacts.append(rulelines[i])
将联系人添加到列表中,否则您会在上一次分配中不断覆盖该联系人。

看起来,当您分配
contacts=rulelines[i]
时,您实际上是在分配
rulelines[i]
字符串。您应该执行
contacts.append(规则行[i])
将联系人添加到列表中,否则您会在上一次分配中不断覆盖该联系人。

将其用作模板:

findres = [5, 7, 15, 22]
contacts = list('abcdefghijklmnopqrstuvwxyz') # dummy list

result = [ contacts[index] for index in findres ]
print result

# ['f', 'h', 'p', 'w']

将其用作模板:

findres = [5, 7, 15, 22]
contacts = list('abcdefghijklmnopqrstuvwxyz') # dummy list

result = [ contacts[index] for index in findres ]
print result

# ['f', 'h', 'p', 'w']

如果我理解正确,您需要首先提取
findres
的每个元素中的第一个数字。然后,使用这些提取的数字作为另一个数组的索引

>>> findres = ['144 154', '145 151', '145 152', '145 153', '145 154', '146 152', '146 153', '146 154', '147 153', '147 154']
>>> first_elements = [c.split()[0] for c in findres]
>>> print first_elements 

['144', '145', '145', '145', '145', '146', '146', '146', '147', '147']

>>> contact = []
>>> for i in first_elements:
        contacts.append(rulelines[i])

如果我理解正确,您需要首先提取
findres
的每个元素中的第一个数字。然后,使用这些提取的数字作为另一个数组的索引

>>> findres = ['144 154', '145 151', '145 152', '145 153', '145 154', '146 152', '146 153', '146 154', '147 153', '147 154']
>>> first_elements = [c.split()[0] for c in findres]
>>> print first_elements 

['144', '145', '145', '145', '145', '146', '146', '146', '147', '147']

>>> contact = []
>>> for i in first_elements:
        contacts.append(rulelines[i])

你能告诉我你想要的输出是什么吗?好吧,如果第一个列表是
findres
,第二个列表是
rulelines
,那么这就不会产生你给出的输出。你能在你的代码中也包括
规则行
findres
吗?你确定findres索引在规则行边界内吗?你能告诉我你想要的输出是什么吗?好吧,如果第一个列表是
findres
,第二个列表是
规则行
,那么这将不会产生您给定的输出。您能否在代码中也包含
规则行
findres
?您确定findres索引在规则行边界内吗?