避免在 for 循环中重复
Avoiding repetition in for loop
给定此数据结构(大小可变):
items = [(u'Triathalon ', u' Teenager'), (u'The Airplanes ', u' Paper Hearts'), (u"Holy '57 ", u' Island Kids'), (u'Yohuna ', u' Apart'), (u'Moon Bounce ', u' Shake'), (u'Miami Horror ', u' Wild Motion (Set It Free)'), (u'Colleagues ', u' Somewhere'), (u'Poor Spirits ', u' BwooKlyn'), (u'Air Review ', u' Young'), (u'Radiohead', u'Karma Police')]
我想这样做:
if len(items) > 10:
for artist, track in random.sample(items, 10):
# do a lot of things in many lines of code
elif len(items) < 10:
for artist, track in items:
# do the exact same thing
但这有点多余。
在不重复自己的情况下实现相同结果的最简单方法是什么?
也许你想要numpy.random.choice
?
import numpy.random as npr
slice = npr.choice(items, 10, replace=False)
for artist, track in slice:
# do whatever
简单的方法是无条件地使用 sample
,但根据输入的长度限制样本大小(因此 sample
只是打乱小的输入而不减少):
for artist, track in random.sample(items, min(len(items), 10)):
行为不同,因为它也会随机化小列表,但您显然不关心排序。
使用min
(是的,min
,不是max
)设置最大值。
for artist, track in random.sample(items, min(10, len(items))):
或者,您可以先保存您感兴趣的可迭代对象:
if len(items) > 10:
i = random.sample(items, 10)
else:
i = items
for artist, track in i:
请注意,对于不同长度的 items
,您的代码实际上具有不同的行为,因为较长的 items
会随机采样,而较短的代码会按其原始顺序迭代。
你可以把 random.sample 放在公共代码之前。
items = [...]
if len(items) > 10:
real_items = random.sample(items, 10):
else:
real_items = items
然后用real_items
做任何你想做的事
你可以试试:
for artist, track in random.sample(items,min(10,len(items))):
# do something
这对你有用吗?
samplesize = 10 if len(items) > 10 else len(items)
sample = random.sample(items, samplesize)
for artist, track in sample:
....
给定此数据结构(大小可变):
items = [(u'Triathalon ', u' Teenager'), (u'The Airplanes ', u' Paper Hearts'), (u"Holy '57 ", u' Island Kids'), (u'Yohuna ', u' Apart'), (u'Moon Bounce ', u' Shake'), (u'Miami Horror ', u' Wild Motion (Set It Free)'), (u'Colleagues ', u' Somewhere'), (u'Poor Spirits ', u' BwooKlyn'), (u'Air Review ', u' Young'), (u'Radiohead', u'Karma Police')]
我想这样做:
if len(items) > 10:
for artist, track in random.sample(items, 10):
# do a lot of things in many lines of code
elif len(items) < 10:
for artist, track in items:
# do the exact same thing
但这有点多余。
在不重复自己的情况下实现相同结果的最简单方法是什么?
也许你想要numpy.random.choice
?
import numpy.random as npr
slice = npr.choice(items, 10, replace=False)
for artist, track in slice:
# do whatever
简单的方法是无条件地使用 sample
,但根据输入的长度限制样本大小(因此 sample
只是打乱小的输入而不减少):
for artist, track in random.sample(items, min(len(items), 10)):
行为不同,因为它也会随机化小列表,但您显然不关心排序。
使用min
(是的,min
,不是max
)设置最大值。
for artist, track in random.sample(items, min(10, len(items))):
或者,您可以先保存您感兴趣的可迭代对象:
if len(items) > 10:
i = random.sample(items, 10)
else:
i = items
for artist, track in i:
请注意,对于不同长度的 items
,您的代码实际上具有不同的行为,因为较长的 items
会随机采样,而较短的代码会按其原始顺序迭代。
你可以把 random.sample 放在公共代码之前。
items = [...]
if len(items) > 10:
real_items = random.sample(items, 10):
else:
real_items = items
然后用real_items
做任何你想做的事你可以试试:
for artist, track in random.sample(items,min(10,len(items))):
# do something
这对你有用吗?
samplesize = 10 if len(items) > 10 else len(items)
sample = random.sample(items, samplesize)
for artist, track in sample:
....