How to split a string into a list?

I want my python function to split a sentence (input) and store each word in a list. The code that I've written so far splits the sentence, but does not store the words as a list. How do I do that? def split_line(text): # split the text words = text.split() # for each word in the line: for word in words: # print the word print(word) text.split() This sho

如何将一个字符串分割成一个列表?

我希望我的python函数能够分割一个句子(输入)并将每个单词存储在一个列表中。 我迄今为止写的代码会分割句子,但不会将这些单词存储为列表。 我怎么做? def split_line(text): # split the text words = text.split() # for each word in the line: for word in words: # print the word print(word) text.split() 这应该足以将每个单词存储在列表中。 words已经是句子中单词的列表

Convert List to a list of tuples python

I am newbie to python and need to convert a list to dictionary. I know that we can convert list of tuples to dictionary. This is the input list: L = [1,term1, 3, term2, x, term3,... z, termN] and I want to convert this list to a list of tuples (OR to a dictionary) like this: [(1, term1), (3, term2), (x, term3), ...(z, termN)] How can we do that easily python? >>> L = [1, "term1",

将列表转换为元组列表python

我是Python的新手,需要将列表转换为字典。 我知道我们可以将元组列表转换为字典。 这是输入列表: L = [1,term1, 3, term2, x, term3,... z, termN] 我想将这个列表转换为一个元组列表(或者是一个字典),如下所示: [(1, term1), (3, term2), (x, term3), ...(z, termN)] 我们如何轻松地做到这一点蟒蛇? >>> L = [1, "term1", 3, "term2", 4, "term3", 5, "termN"] # Create an iterator >>> it =

Unzipping and the * operator

The python docs gives this code as the reverse operation of zip: >>> x2, y2 = zip(*zipped) In particular "zip() in conjunction with the * operator can be used to unzip a list". Can someone explain to me how the * operator works in this case? As far as I understand, * is a binary operator and can be used for multiplication or shallow copy...neither of which seems to be the c

解压缩和*运算符

python文档给出了这个代码作为zip的反向操作: >>> x2, y2 = zip(*zipped) 特别是“zip()与*操作符一起可用于解压缩列表”。 有人可以向我解释*操作符在这种情况下的工作原理吗? 据我所知,*是一个二元运算符,可用于乘法或浅拷贝......这两种情况似乎都不是这种情况。 当像这样使用时,*(星号,在一些圈子中也被称为“splat”运算符)是从列表中解压参数的信号。 有关示例的更完整定义,请参见http://docs.pyth

Python: yield

How do I yield an object from a generator and forget it immediately, so that it doesn't take up memory? For example, in the following function: def grouper(iterable, chunksize): """ Return elements from the iterable in `chunksize`-ed lists. The last returned element may be smaller (if length of collection is not divisible by `chunksize`). >>> print list(grouper(xr

Python:产量

我如何从发生器产生一个对象并立即忘记它,以免它占用内存? 例如,在以下函数中: def grouper(iterable, chunksize): """ Return elements from the iterable in `chunksize`-ed lists. The last returned element may be smaller (if length of collection is not divisible by `chunksize`). >>> print list(grouper(xrange(10), 3)) [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]] """ i

Python CSV Reader: No Get Line Function?

This question already has an answer here: Accessing the index in 'for' loops? 17 answers The reader object is an iterator, so you can always use enumerate to get the line number: reader = csv.DictReader(readFile) for line_number, row in enumerate(reader): # some code enumerate also lets you specify a custom starting index: for line_number, row in enumerate(reader, 2): # l

Python CSV阅读器:没有获得线路功能?

这个问题在这里已经有了答案: 访问'for'循环中的索引? 17个答案 阅读器对象是一个迭代器,因此您可以始终使用enumerate来获取行号: reader = csv.DictReader(readFile) for line_number, row in enumerate(reader): # some code enumerate还可以让你指定一个自定义的起始索引: for line_number, row in enumerate(reader, 2): # line number starts at 2 enumerate不仅限于csv模块的reader对象,

How to return index of lists in Python

This question already has an answer here: Accessing the index in 'for' loops? 17 answers 使用enumerate : >>> a = [[0,1,2],[0,0,0],[1,1,1]] >>> [i for i, x in enumerate(a, 1) if 2 not in x] [2, 3] Use enumerate : [i for i, j in enumerate(a) if 2 not in j] note that list indices in Python start from 0 , not from 1 你可以使用index [U.index(l) for l in U if not 2 in

如何在Python中返回列表的索引

这个问题在这里已经有了答案: 访问'for'循环中的索引? 17个答案 使用enumerate : >>> a = [[0,1,2],[0,0,0],[1,1,1]] >>> [i for i, x in enumerate(a, 1) if 2 not in x] [2, 3] 使用enumerate : [i for i, j in enumerate(a) if 2 not in j] 请注意Python中的列表索引从0开始,而不是从1 你可以使用index [U.index(l) for l in U if not 2 in l]

How do I display the the index of a list element in Python?

This question already has an answer here: Accessing the index in 'for' loops? 17 answers Assuming that you are in Python 3 : hey = ["lol","hey","water","pepsi","jam"] for item in hey: print(hey.index(item)+1,item) If you are in Python 2 , replace the print() with just the print statement: hey = ["hey","water","pepsi","jam"] for item in hey: print hey.index(item)+1,item

如何在Python中显示列表元素的索引?

这个问题在这里已经有了答案: 访问'for'循环中的索引? 17个答案 假设你在Python 3 : hey = ["lol","hey","water","pepsi","jam"] for item in hey: print(hey.index(item)+1,item) 如果你在Python 2 ,用print语句替换print() : hey = ["hey","water","pepsi","jam"] for item in hey: print hey.index(item)+1,item 使用<list>.index(<item>)可以获得该项目的索引。 但是,正如前

Find all index of list containing a value Python

This question already has an answer here: Accessing the index in 'for' loops? 17 answers You might use something like ind = [[i for i, value in enumerate(lst) if value == x] for x in lst2] I don't know a purpose of this operation, but it might be more useful to use dict comprehension: ind = {x: [i for i, value in enumerate(lst) if value == x] for x in lst2} because in this ca

查找包含值Python的列表的所有索引

这个问题在这里已经有了答案: 访问'for'循环中的索引? 17个答案 你可能会使用类似的东西 ind = [[i for i, value in enumerate(lst) if value == x] for x in lst2] 我不知道这个操作的目的,但使用dict理解可能更有用: ind = {x: [i for i, value in enumerate(lst) if value == x] for x in lst2} 因为在这种情况下,您将得到如下输出: {1: [0, 5, 7], 2: [1, 6, 8], 3: [2, 9]} ,这可能更易于使用。 这

Printing list in Python with serial numbers

This question already has an answer here: Accessing the index in 'for' loops? 17 answers Use enumerate a = ['dwe','fw4fe4f','gvfes','fw4f',44,'vwe4d','sv','vsed'] for i,each in enumerate(a,start=1): print ("{}.{}".format(i,each)) Output .. 1.dwe 2.fw4fe4f 3.gvfes 4.fw4f 5.44 6.vwe4d 7.sv 8.vsed a = ['dwe','fw4fe4f','gvfes','fw4f',44,'vwe4d','sv','vsed'] for i,j in enumerate(a

使用序列号在Python中打印列表

这个问题在这里已经有了答案: 访问'for'循环中的索引? 17个答案 使用enumerate a = ['dwe','fw4fe4f','gvfes','fw4f',44,'vwe4d','sv','vsed'] for i,each in enumerate(a,start=1): print ("{}.{}".format(i,each)) 输出 1.dwe 2.fw4fe4f 3.gvfes 4.fw4f 5.44 6.vwe4d 7.sv 8.vsed a = ['dwe','fw4fe4f','gvfes','fw4f',44,'vwe4d','sv','vsed'] for i,j in enumerate(a): print "%s. %s"%(i+1,j) &

for with counter

Possible Duplicate: Accessing the index in Python for loops I wonder: does Python have something like? for (i=0; i < length; i += 1){ .... } Of course, I might say i = 0 for item in items: #..... i += 1 but I think there should be something similar to for(i = 0;...) , shouldn't it? Use the enumerate() function: for i, item in enumerate(items): print i, item or use range(

与柜台

可能重复: 在Python中访问循环的索引 我想知道:Python有类似的东西吗? for (i=0; i < length; i += 1){ .... } 当然,我可能会说 i = 0 for item in items: #..... i += 1 但我认为应该有类似于for(i = 0;...) ,不是吗? 使用enumerate()函数: for i, item in enumerate(items): print i, item 或使用range() : for i in range(len(items)): print i (在python 2上,你会使用xrange()来代替