这是列表理解吗? [英] Do this as a list comprehension?

查看:46
本文介绍了这是列表理解吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为此编写一个列表推导,以便生成一个两项元组的

列表?


base_scores = range(8 ,19)

score_costs = [0,1,1,1,1,1,1,2,2,3,3]

print zip(base_scores,score_costs )


在这种情况下我无法想到列表理解的结构如何工作

,因为它似乎需要迭代超过两个单独的

序列来生成元组中的每个项目。


zip似乎无论如何都能正常工作,但我的直觉本能是尝试一下

list comprehension(直到我无法弄清楚如何!)。如果列表组合能够完成拉链可以做的所有事情,我不确定




谢谢。

Is it possible to write a list comprehension for this so as to produce a
list of two-item tuples?

base_scores = range(8, 19)
score_costs = [0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3]
print zip(base_scores, score_costs)

I can''t think of how the structure of the list comprehension would work
in this case, because it seems to require iteration over two separate
sequences to produce each item in the tuple.

zip seems to work fine anyway, but my immediate instinct was to try a
list comprehension (until I couldn''t figure out how!). And I wasn''t sure
if list comps were capable of doing everything a zip could do.

Thanks.

推荐答案

6月5日,10:42 ??? pm,John Salerno< johnj ... @gmailNOSPAM.comwrote:
On Jun 5, 10:42???pm, John Salerno <johnj...@gmailNOSPAM.comwrote:

是否可以为此编写一个列表解析,以便生成一个两项元组的

列表?


base_scores = range( 8,19)

score_costs = [0,1,1,1,1,1,1,2,2,3,3]

print zip(base_scores, score_costs)


在这种情况下我无法想到列表理解的结构如何工作

,因为它似乎需要迭代超过两个单独

序列来生成元组中的每个项目。


zip似乎无论如何都能正常工作,但我的直觉本能是尝试一下

列表理解(直到我无法弄清楚如何!)。如果列表组合能够完成拉链可以做的所有事情,我不确定




谢谢。
Is it possible to write a list comprehension for this so as to produce a
list of two-item tuples?

base_scores = range(8, 19)
score_costs = [0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3]
print zip(base_scores, score_costs)

I can''t think of how the structure of the list comprehension would work
in this case, because it seems to require iteration over two separate
sequences to produce each item in the tuple.

zip seems to work fine anyway, but my immediate instinct was to try a
list comprehension (until I couldn''t figure out how!). And I wasn''t sure
if list comps were capable of doing everything a zip could do.

Thanks.



base_scores = range(8,19)

score_costs = [0,1,1,1,1,1,1,2 ,2,3,3]

print zip(base_scores,score_costs)


s = [(i + 8,j)for i,j in enumerate( [0,1,1,1,1,1,1,2,2,3,3])]

打印s


##> ;>>

## [(8,0),(9,1),(10,1),(11,1),(12,1),(13,1) ),(14,1),(15,

2),(16,2),(17,3),(18,3)]

## [(8,0),(9,1),(10,1),(11,1),(12,1),(13,1),(14,1),(15,

2),(16,2),(17,3),(18,3)]

##>>>

base_scores = range(8, 19)
score_costs = [0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3]
print zip(base_scores, score_costs)

s = [(i+8,j) for i,j in enumerate( [0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3])]
print s

##>>>
##[(8, 0), (9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1), (15,
2), (16, 2), (17, 3), (18, 3)]
##[(8, 0), (9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1), (15,
2), (16, 2), (17, 3), (18, 3)]
##>>>




" Mensanator" < me ******** @ aol.comwrote in message

news:bb ********************** ************ @ a1g2000h sb.googlegroups.com ...

| 6月5日晚上10点42分,John Salerno< johnj ... @gmailNOSPAM.com写道:

|是否有可能为此编写列表理解以便生成

a

|两项元组列表?

| >

| base_scores = range(8,19)

| score_costs = [0,1,1,1,1,1,1,2,2,3,3]

| print zip(base_scores,score_costs)

| >

|我无法想到列表理解的结构如何工作

|在这种情况下,因为它似乎需要迭代两个单独的

|生成元组中每个项目的序列。


这正是zip的目的,或者它的特化枚举!


| zip无论如何似乎工作正常,但我的直接本能是尝试一个

|列表理解(直到我无法弄清楚如何!)。而且我没有确定

|如果list comps能够做拉链可以做的所有事情。

|

| base_scores = range(8,19)

| score_costs = [0,1,1,1,1,1,1,2,2,3,3]

| print zip(base_scores,score_costs)

|

| s = [(i + 8,j)表示i,j表示枚举([0,1,1,1,1,1,1,2,2,3,3])]

|打印s

|

| ##>>>

| ## [(8,0),(9,1),(10,1),(11,1),(12,1),(13,1),(14,1),(15,< />
| 2),(16,2),(17,3),(18,3)]

| ## [(8,0),(9,1),(10,1),(11,1),(12,1),(13,1),(14,1),(15,< />
| 2),(16,2),(17,3),(18,3)]

| ##>>>


当然,枚举(可迭代)只是拉链上的一个外观(itertools.count(),

可迭代)


"Mensanator" <me********@aol.comwrote in message
news:bb**********************************@a1g2000h sb.googlegroups.com...
| On Jun 5, 10:42?pm, John Salerno <johnj...@gmailNOSPAM.comwrote:
| Is it possible to write a list comprehension for this so as to produce
a
| list of two-item tuples?
| >
| base_scores = range(8, 19)
| score_costs = [0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3]
| print zip(base_scores, score_costs)
| >
| I can''t think of how the structure of the list comprehension would work
| in this case, because it seems to require iteration over two separate
| sequences to produce each item in the tuple.

Which is exactly the purpose of zip, or its specialization enumerate!

| zip seems to work fine anyway, but my immediate instinct was to try a
| list comprehension (until I couldn''t figure out how!). And I wasn''t
sure
| if list comps were capable of doing everything a zip could do.
|
| base_scores = range(8, 19)
| score_costs = [0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3]
| print zip(base_scores, score_costs)
|
| s = [(i+8,j) for i,j in enumerate( [0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3])]
| print s
|
| ##>>>
| ##[(8, 0), (9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1), (15,
| 2), (16, 2), (17, 3), (18, 3)]
| ##[(8, 0), (9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1), (15,
| 2), (16, 2), (17, 3), (18, 3)]
| ##>>>

Of course, enumerate(iterable) is just a facade over zip(itertools.count(),
iterable)


6月6日,8:44 * am,Terry Reedy < tjre ... @ udel.eduwrote:
On Jun 6, 8:44*am, "Terry Reedy" <tjre...@udel.eduwrote:

>

当然,枚举(iterable)只是拉链上的一个外观(itertools.count(),

可迭代)
>
Of course, enumerate(iterable) is just a facade over zip(itertools.count(),
iterable)



所以你可以写:

gen =(x对于x in itertools.izip(itertools.count(8),[0,1,1,1,1,

1,1,2,2,3,3])) >
打印列表(gen)


使用zip就像你自己的例子是最好的选择。


如果你有一个巨大的数据量只想迭代

结果,使用生成器可能更好:

gen =(x表示迭代工具中的x(itertools.count( 8),[0,1,1,1,1,

1,1,2,2,3,3]))

for i,j in gen :

...你的代码在这里......

So you could write:
gen = (x for x in itertools.izip(itertools.count(8), [0, 1, 1, 1, 1,
1, 1, 2, 2, 3, 3]))
print list(gen)

Using zip like you own example is the best option.

If you have a huge amount of data and only want to iterate over the
result, using a generator is probably better:
gen = (x for x in itertools.izip(itertools.count(8), [0, 1, 1, 1, 1,
1, 1, 2, 2, 3, 3]))
for i, j in gen:
... your code here ...


这篇关于这是列表理解吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆