将循环中的值存储在 Python 中的列表或元组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33553046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing values from loop in a list or tuple in Python
提问by abhishek nair
Im very new to python and playing around with loops and stuck on a basic doubt
我对 python 非常陌生,并且在玩循环并坚持一个基本的疑问
Im trying to perform the following:
我正在尝试执行以下操作:
tup1=()
for i in range(1,10,2):
tup1=(tup1,i)
print tup1
I expect the out put to be the sequence 1 to 10. However i end up with the following:
我希望输出是 1 到 10 的序列。但是我最终得到以下结果:
((((((), 0), 2), 4), 6), 8)
Could you please help me as to if this approach is correct or are there better ways to meet the requirement ?
您能否帮助我了解这种方法是否正确或是否有更好的方法来满足要求?
采纳答案by chucksmash
If you just want an iterable with the even numbers 1 to 10 then the simplest way to do it:
如果你只想要一个偶数 1 到 10 的迭代,那么最简单的方法是:
seq = range(2, 11, 2)
If you are doing this as a means of learning Python and you want to build up your own data structure, use a list:
如果您将此作为学习 Python 的一种方式,并且想要构建自己的数据结构,请使用列表:
l = []
for i in range(2, 11, 2):
l.append(i)
The above for loop can be rewritten as a list comprehension:
上面的 for 循环可以重写为列表推导式:
l = [i for i in range(2, 11, 2)]
or using an if clause in the loop comprehension:
或在循环理解中使用 if 子句:
l = [ i for i in range(1, 11) if i % 2 == 0]
回答by Raul Cabrera A.
it's something like this:
它是这样的:
print range(1, 11)
回答by Mr. E
Tuples are immutable objects in Python. Thus means you can't modify them. What you're doing right now is creating a new tuple with the previous one inside
元组是 Python 中的不可变对象。这意味着您无法修改它们。您现在正在做的是创建一个新元组,其中包含前一个元组
You could do:
你可以这样做:
lst = []
for i in range(1,10,2):
lst.append(i)
tup = tuple(lst) #If you really want a tuple
print tup
But lst = range(1,10,2)
or tup = tuple(range(1,10,2))
is much better (Unless you want to use append for some reason)
但是lst = range(1,10,2)
ortup = tuple(range(1,10,2))
好多了(除非您出于某种原因想使用 append )
回答by rfloresx
You can append an item to a tuple using the +=
operator.
您可以使用+=
运算符将项目附加到元组。
tup1=()
for i in range(1,10,2):
tup1+= (i,)
print tup1
This prints (1, 3, 5, 7, 9)
这打印 (1, 3, 5, 7, 9)
回答by Tomasz Jakub Rup
Read about List Comprehension
阅读列表理解
tuple(i for i in range(1, 10, 2))
Or
或者
tup1 = ()
for i in range(1, 10, 2):
tup1 += (i,)
print tup1
回答by Cam Beatty
You are skipping by two by using for i in range(1,10,2):
if you use for i in range(1,11):
if will increment by 1. As for tup1=(tup1,i)
you are constantly adding a tuple to each other which is creating the weird output. You could use a list if you want to store them. Otherwise using will do it just fine:
for i in range(1,10,2):
如果使用for i in range(1,11):
if 将增加 1 ,则使用跳过两个。至于tup1=(tup1,i)
您不断向彼此添加一个元组,这会产生奇怪的输出。如果要存储它们,可以使用列表。否则使用就可以了:
print(range(10))
回答by apocalypse1212
List item
项目清单
For appending into list or tuple you can use append() function or you can use += operator which does the same. s=()
要附加到列表或元组中,您可以使用 append() 函数,也可以使用 += 运算符来执行相同的操作。s=()
for sequence of numbers from 1 to 10
对于从 1 到 10 的数字序列
for i in range(1,11): s+=(i,)
对于 i in range(1,11): s+=(i,)
print(s) #(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
打印#(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
for sequence of numbers from 1 to 10 with step size 2
用于从 1 到 10 步长为 2 的数字序列
x=()
x=()
for i in range(1,11,2): x+=(i,)
对于 i in range(1,11,2): x+=(i,)
print(x) #odd nos from 1-9 (1, 3, 5, 7, 9)
打印(x)#1-9的奇数(1、3、5、7、9)
x=()
x=()
for i in range(2,11,2): x+=(i,)
对于 i in range(2,11,2): x+=(i,)
print(x) #even nos from 2-10 (2, 4, 6, 8, 10)
print(x) #even nos from 2-10 (2, 4, 6, 8, 10)
- List item
- 项目清单
回答by rahul ranjan
Storing values from loop in a list or tuple in Python by following ways -
通过以下方式将循环中的值存储在 Python 中的列表或元组中 -
-> By appending the value in the list (here new_data1) as join will not work here.
-> 通过在列表中附加值(这里是 new_data1),因为 join 在这里不起作用。
new_data1 = []
for line in all_words:
new_data=' '.join(lemmatize_sentence(line))
new_data1.append(new_data)
#print (new_data)
print (new_data1)
P.S. - This is just a snapshot of a code just for hint . Hope this helps!!
PS-这只是提示的代码快照。希望这可以帮助!!