英文:
Getiing a list [1,1,2,2,..] and add it to a column of a dataframe
问题
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, ..., n, n] 的单行代码列表推导式如下:
[elem for i in range(1, n+1) for elem in [i, i]]
英文:
I want to have a one-liner code using list comprehension which prints this sequence for an arbitrary n
:
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, ..., n, n]
答案1
得分: 2
让我们试试这个:
n = 10
result = [i//2 + 1 for i in range(n*2)]
print(result)
这段代码使用了列表推导:range(n*2)
函数生成了一个从0到 n*2-1 的数字范围,i//2 + 1
给出了包含当前数字 i
的一对数字的索引。
英文:
Let's try this:
n = 10
result = [i//2 + 1 for i in range(n*2)]
print(result)
This code uses list comprehension: the range(n*2)
function generates a range of numbers from 0 to n*2-1 and i//2 + 1
gives the index of the pair of numbers that contains the current number, i
.
答案2
得分: 2
[e for l in [[x]*2 for x in range(1, 10)] for e in l]
可以翻译为:
[e for l in [[x]*2 for x in range(1, 10)] for e in l]
[[x]*2 for x in range(1, 10)]
可以翻译为:
[[x]*2 for x in range(1, 10)]
[[x]*2 for x in range(1, 10)]
可以翻译为:
[[x]*2 for x in range(1, 10)]
[[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7], [8, 8], [9, 9]]
可以翻译为:
[[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7], [8, 8], [9, 9]]
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9]
可以翻译为:
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9]
[n for i in range(1, 10) for n in [i] * 2]
可以翻译为:
[n for i in range(1, 10) for n in [i] * 2]
英文:
[e for l in [[x]*2 for x in range(1, 10)] for e in l]
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9]
Could be read as follows :
element
for each list
in list of list
as we know list of list
is [[x]*2 for x in range(1, 10)]
>>> [[x]*2 for x in range(1, 10)]
[[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7], [8, 8], [9, 9]]
In definitive, another way to flatten a 2-levels list in one line without using for instance tools like numpy.ndarray.flatten
or numpy.ravel
Modified by Accdias (reduction of for
loops and then time consumption) :
[n for i in range(1, 10) for n in [i] * 2]
Indeed excellent, because uses the property of created elements which are lists and then could be iterated in the run process :
- creation of the list-like little unit
[i] * 2
- iteration over the creation with the nearest
for
loop - and flatten process ends with the last
for
loop
答案3
得分: 1
如果需要向现有DataFrame附加新列:
使用numpy.repeat
和numpy.arange
,通过筛选来分配偶数或奇数行数:
df = pd.DataFrame({'a': range(4)})
n = df.shape[0]
df['new'] = np.repeat(np.arange(1, n // 2 + 2), 2)[:n]
print (df)
a new
0 0 1
1 1 1
2 2 2
3 3 2
df = pd.DataFrame({'a': range(5)})
n = df.shape[0]
df['new'] = np.repeat(np.arange(1, n // 2 + 2), 2)[:n]
print (df)
a new
0 0 1
1 1 1
2 2 2
3 3 2
4 4 3
如果始终有偶数行数,解决方案是:
df = pd.DataFrame({'a': range(8)})
df['new'] = np.repeat(np.arange(1, df.shape[0] // 2 + 1), 2)
print (df)
a new
0 0 1
1 1 1
2 2 2
3 3 2
4 4 3
5 5 3
6 6 4
7 7 4
英文:
If need append new column to existing DataFrame:
Use numpy.repeat
with numpy.arange
with filtering for assign even or odd number of rows:
df = pd.DataFrame({'a': range(4)})
n = df.shape[0]
df['new'] = np.repeat(np.arange(1, n // 2 + 2), 2)[:n]
print (df)
a new
0 0 1
1 1 1
2 2 2
3 3 2
df = pd.DataFrame({'a': range(5)})
n = df.shape[0]
df['new'] = np.repeat(np.arange(1, n // 2 + 2), 2)[:n]
print (df)
a new
0 0 1
1 1 1
2 2 2
3 3 2
4 4 3
If always even number of rows, solution is:
df = pd.DataFrame({'a': range(8)})
df['new'] = np.repeat(np.arange(1, df.shape[0] // 2 + 1), 2)
print (df)
a new
0 0 1
1 1 1
2 2 2
3 3 2
4 4 3
5 5 3
6 6 4
7 7 4
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论