Python 从列表中提取某些元素

Python 从列表中提取某些元素,python,join,Python,Join,我对Python一无所知,并开始在一些文件上使用它。我设法找到了如何做所有我需要的事情,除了两件事 1st >>>line = ['0', '1', '2', '3', '4', '5', '6'] >>>#prints all elements of line as expected >>>print string.join(line) 0 1 2 3 4 5 6 >>>#prints the first two ele

我对Python一无所知,并开始在一些文件上使用它。我设法找到了如何做所有我需要的事情,除了两件事

1st

>>>line = ['0', '1', '2', '3', '4', '5', '6']
>>>#prints all elements of line as expected
>>>print string.join(line)
0 1 2 3 4 5 6

>>>#prints the first two elements as expected
>>>print string.join(line[0:2])
0 1

>>>#expected to print the first, second, fourth and sixth element;
>>>#Raises an exception instead
>>>print string.join(line[0:2:4:6])
SyntaxError: invalid syntax
我希望它的工作方式类似于
awk'{print$1$2$5$7}
。我怎样才能做到这一点

第二名


如何删除行的最后一个字符?还有一个额外的
我不需要。

您不想为此使用
join
。如果只想打印列表中的某些位,请直接指定所需的位:

print '%s %s %s %s' % (line[0], line[1], line[4], line[6])

您不想为此使用
join
。如果只想打印列表中的某些位,请直接指定所需的位:

print '%s %s %s %s' % (line[0], line[1], line[4], line[6])

对第二个问题的答复:

如果字符串包含在
myLine
中,只需执行以下操作:

myLline = myLine[:-1]
删除最后一个字符

或者您也可以使用:

Ipadd = string.join([line[i] for i in indices])

对第二个问题的答复:

如果字符串包含在
myLine
中,只需执行以下操作:

myLline = myLine[:-1]
删除最后一个字符

或者您也可以使用:

Ipadd = string.join([line[i] for i in indices])
或者

"{l[0]} {l[1]} {l[4]} {l[5]}".format(l=line)
请停止使用
%
运算符设置字符串格式

或者

"{l[0]} {l[1]} {l[4]} {l[5]}".format(l=line)

请停止使用
%
运算符设置字符串格式。

假设
变量应包含一行单元格,以逗号分隔

您可以使用
map
进行以下操作:

line = "1,2,3,4,5,6"
cells = line.split(",")
indices=[0,1,4,6]
selected_elements = map( lambda i: cells[i], indices )
print ",".join(selected_elements)

map
函数将为list参数中的每个索引执行即时功能。(根据您的喜好重新排序)

假设
变量应该包含一行单元格,用逗号分隔

您可以使用
map
进行以下操作:

line = "1,2,3,4,5,6"
cells = line.split(",")
indices=[0,1,4,6]
selected_elements = map( lambda i: cells[i], indices )
print ",".join(selected_elements)

map
函数将为list参数中的每个索引执行即时功能。(根据您的喜好重新排序)

您可以使用以下列表理解:

indices = [0,1,4,6]
Ipadd = string.join([line[i] for i in xrange(len(line)) if i in indices])
注意:您还可以使用:

Ipadd = string.join([line[i] for i in indices])

但是您需要一个排序的索引列表,当然不需要重复。

您可以使用以下列表理解:

indices = [0,1,4,6]
Ipadd = string.join([line[i] for i in xrange(len(line)) if i in indices])
注意:您还可以使用:

Ipadd = string.join([line[i] for i in indices])

但是你需要一个排序的索引列表,当然不需要重复。

这里的连接只是为了有一个好的字符串作为结果打印或存储(使用coma作为分隔符,在OP示例中,它应该是字符串中的任何内容)

A、 B

A、 B、C、E、F、G

在这两种情况下,您都要从初始列表中提取一个子列表。第一个使用切片,第二个使用列表。正如其他人所说,您也可以一个接一个地访问元素,上面的语法只是以下内容的简写:

print ','.join ([line[0], line[1]])
A、 B

A、 B、C、E、F、G

我相信一些关于列表切片的简短教程会有所帮助:

  • l[x:y]
    是列表l的一个“切片”。它将获得位置x(包括)和位置y(排除)之间的所有元素。位置从0开始。如果y不在列表中或缺失,它将包含所有列表,直到结束。如果使用负数,则从列表末尾开始计数。如果您想以固定的间隔“跳过”某些项目(而不是将它们放入切片),还可以使用第三个参数,如
    l[x:y:step]
一些例子:

l = range(1, 100) # create a list of 99 integers from 1 to 99
l[:]    # resulting slice is a copy of the list
l[0:]   # another way to get a copy of the list
l[0:99] # as we know the number of items, we could also do that
l[0:0]  # a new empty list (remember y is excluded]
l[0:1]  # a new list that contains only the first item of the old list
l[0:2]  # a new list that contains only the first two items of the old list
l[0:-1] # a new list that contains all the items of the old list, except the last
l[0:len(l)-1] # same as above but less clear 
l[0:-2] # a new list that contains all the items of the old list, except the last two
l[0:len(l)-2] # same as above but less clear
l[1:-1] # a new list with first and last item of the original list removed
l[-2:] # a list that contains the last two items of the original list
l[0::2] # odd numbers
l[1::2] # even numbers
l[2::3] # multiples of 3

如果获取项目的规则更复杂,您将使用一个而不是一个切片,但它是另一个主题集。这就是我在第二个连接示例中使用的方法。

这里的连接只是为了打印或存储一个漂亮的字符串作为结果(使用coma作为分隔符,在OP示例中,它应该是字符串中的任何内容)

A、 B

A、 B、C、E、F、G

在这两种情况下,您都要从初始列表中提取一个子列表。第一个使用切片,第二个使用列表。正如其他人所说,您也可以一个接一个地访问元素,上面的语法只是以下内容的简写:

print ','.join ([line[0], line[1]])
A、 B

A、 B、C、E、F、G

我相信一些关于列表切片的简短教程会有所帮助:

  • l[x:y]
    是列表l的一个“切片”。它将获得位置x(包括)和位置y(排除)之间的所有元素。位置从0开始。如果y不在列表中或缺失,它将包含所有列表,直到结束。如果使用负数,则从列表末尾开始计数。如果您想以固定的间隔“跳过”某些项目(而不是将它们放入切片),还可以使用第三个参数,如
    l[x:y:step]
一些例子:

l = range(1, 100) # create a list of 99 integers from 1 to 99
l[:]    # resulting slice is a copy of the list
l[0:]   # another way to get a copy of the list
l[0:99] # as we know the number of items, we could also do that
l[0:0]  # a new empty list (remember y is excluded]
l[0:1]  # a new list that contains only the first item of the old list
l[0:2]  # a new list that contains only the first two items of the old list
l[0:-1] # a new list that contains all the items of the old list, except the last
l[0:len(l)-1] # same as above but less clear 
l[0:-2] # a new list that contains all the items of the old list, except the last two
l[0:len(l)-2] # same as above but less clear
l[1:-1] # a new list with first and last item of the original list removed
l[-2:] # a list that contains the last two items of the original list
l[0::2] # odd numbers
l[1::2] # even numbers
l[2::3] # multiples of 3

如果获取项目的规则更复杂,您将使用一个而不是一个切片,但它是另一个主题集。这就是我在第二个连接示例中使用的内容。

元组中的第二项不应该是
行[1]
?(假设他想要列表中的第二个元素)嗨,谢谢,到目前为止这很有帮助!但我之前做了一次拆分,并考虑在那之后使用联接。谢谢你的快速回答@tiger-你可以使用
加入
,但我认为这会让你的代码变得更复杂,而不是更少。
%
正在被弃用,不应该被使用。是的,谢谢你的提示。这行有几个单元格或字段,如果我只想打印其中的一部分,我想我需要拆分它们。在那之后,我似乎有了加入他们的逻辑:-)脚本现在有19个代码行,所以我认为它并不复杂!我只需要添加一些东西将结果写入文件,然后我就完成了。但这是python中的第一件事,所以我很高兴它能工作!元组中的第二项不应该是
行[1]
?(假设他想要列表中的第二个元素)嗨,谢谢,到目前为止这很有帮助!但我之前做了一次拆分,并考虑在那之后使用联接。谢谢你的快速回答@tiger-你可以使用
加入
,但我认为这会让你的代码变得更复杂,而不是更少。
%
正在被弃用,不应该被使用。是的,谢谢你的提示。这行有几个单元格或字段,如果我想打印出来的话,只能这样