简体   繁体   English

在python中制作清单

[英]making list in python

When i executed the following python script 当我执行以下python脚本时

list= (1,2,3,4,1,2,7,8)

for number in list:
    item1= number
    item2= list[list.index(item1)+2]
    couple= item1, item2
    print couple

the goal is to link each number with the second following I obtain this result 我的目标是将每个数字与以下的第二个链接

(1, 3)
(2, 4)
(3, 1)
(4, 2)
(1, 3)
(2, 4)

(and then the index gets out of range but this is not the problem) (然后索引超出范围,但这不是问题)

My question is why the number 1 in the fifth line is still coupled to the number 3 and how can i make that it is coupled to the number 7; 我的问题是,为什么第五行中的数字1仍然与数字3耦合,又如何使它与数字7耦合? idem for the number 2 in the sixth line that should be coupled to the number 8. 第六行中数字2的同上,应该与数字8耦合。

additional question what do I do if i only want to make a list of the couples that start with 1: [(1,3), (1,7)] 另一个问题,如果我只想列出以1开头的夫妇,该怎么办?[(1,3),(1,7)]

list.index returns the offset of the first occurrence of the value in the list - thus if you do [1,1,1].index(1), the answer will always be 0, even though 1 and 2 are also valid answers. list.index返回值在列表中第一次出现的偏移量-因此,如果执行[1,1,1] .index(1),则答案将始终为0,即使1和2也是有效答案。

Instead, try: 相反,请尝试:

from itertools import islice, izip, ifilter

mylist = [1,2,3,4,1,2,7,8]
for pair in ifilter(lambda x: x[0]==1, izip(mylist, islice(mylist, 2, None))):
    print pair

results in 结果是

(1, 3)
(1, 7)

xs.index(x) gives you the index of the first occurence of x in xs . xs.index(x)给出xxs首次出现的索引。 So when you get to the second 1 , .index gives you the index of the first 1 . 因此,当您到达第二个1.index会为您提供第一个1的索引。

If you need the index alongside the value, use enumerate : for i, number in enumerate(numbers): print number, numbers[i+2] . 如果在值旁边需要索引,请使用enumeratefor i, number in enumerate(numbers): print number, numbers[i+2] ,使用enumerate for i, number in enumerate(numbers): print number, numbers[i+2]

Note that I deliberately didn't use the name list . 请注意,我故意不使用名称list It's the name of a built-in, you shouldn't overwrite it. 这是内置名称,您不应覆盖它。 Also note that (..., ...) is a tuple (and therefore can't be changed), not a list (which is defined in square brackets [..., ...] and can be changed). 还要注意, (..., ...)是一个元组 (因此不能更改),而不是列表(在方括号[..., ...]并且可以更改)。

You have duplicates in the list so index always returns the first index. 列表中有重复项,因此index始终返回第一个索引。

Start your program with for index in range(len(list) - 1) for index in range(len(list) - 1)启动程序

You are using .index which returns the first occurrence of number . 您正在使用.index返回第一个出现的number

consider: 考虑:

for number in range(len(list)):
    item1= list[number]
    item2= list[number+2]
    couple= item1, item2
    print couple
>>> zip(lst, lst[2:])
[(1, 3), (2, 4), (3, 1), (4, 2), (1, 7), (2, 8)]

To get only pairs (1, X): 仅获取对(1,X):

>>> [(a, b) for (a, b) in zip(lst, lst[2:]) if a == 1]
[(1, 3), (1, 7)]

Recommended reading: 推荐阅读:

http://docs.python.org/tutorial/datastructures.html http://docs.python.org/tutorial/datastructures.html

http://docs.python.org/howto/functional.html http://docs.python.org/howto/functional.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM