'Creating a 2D list of recursive patterns without slicing or list()
I need to write a function or a for-loop that receives or loop through a list of strings and returns this pattern:
[['alpha'], ['alpha', 'beta'], ['alpha', 'beta', 'gamma'], ['alpha', 'beta', 'gamma', 'delta']]
I'm not allowed to use list slicing or list(). The main purpose is to explore all the solutions with and without copy. Here is what I have tried but with list():
input = [['alpha',[]], ['beta',[]], ['gamma',[]], ['delta',[]]]
df = pd.DataFrame(input, columns=['Sequence','DNA'])
knn, dna_list=[],[]
for j, row in df.iterrows():
knn.append(row['Sequence'])
dna_list[j:] = [list(knn)]
Solution 1:[1]
you can do that with a nested for loop
input = [['alpha', []], ['beta', []], ['gamma', []], ['delta', []]]
result = []
for i in range(len(input)):
result.append([input[j][0] for j in range(0,i+1)])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Aninda |
