How to Write 2 Lists of Items in 2 Columns Instead of 2 Arrays

How to write 2 lists of items in 2 columns instead of 2 arrays?

You can use zip. Change:

data=[price(html),postcode(html)]

to:

data=zip(price(html),postcode(html))

PYTHON : Make a list of 4 columns with 2 lists of 2 columns

You can do this with zip and then flatten it to get items back. You can zip the 2 lists element-wise (corresponding elements of both lists added to a tuple together.

Post that you can use [item for sublist in list for item in sublist] which lets you flatten and break the tuples into corresponding items.

List this -

Mouse = [['x1', 'y1'], ['x2', 'y2']]
Target = [['xA', 'yA'], ['xB', 'yB']]

[item for sublist in zip(Mouse, Target) for item in sublist]
[['x1', 'y1'], ['xA', 'yA'], ['x2', 'y2'], ['xB', 'yB']]

EDIT:

Based on your updated question, you can do this -

Mouse = [['x1', 'y1'], ['x2', 'y2']]
Target = [['xA', 'yA'], ['xB', 'yB']]

out = [i+j for i,j in zip(Mouse, Target)]
out
[['x1', 'y1', 'xA', 'yA'], ['x2', 'y2', 'xB', 'yB']]

Print 2 lists side by side

You can use the zip() function to join lists together.

a = ['a', 'b', 'c']
b = ['1', '0', '0']
res = "\n".join("{} {}".format(x, y) for x, y in zip(a, b))

The zip() function will iterate tuples with the corresponding elements from each of the lists, which you can then format as Michael Butscher suggested in the comments.

Finally, just join() them together with newlines and you have the string you want.

print(res)
a 1
b 0
c 0

List of 2-element lists from first 2 Dataframe columns

Select first 2 columns and convert output to numpy array and then to list:

L = df.iloc[:, :2].to_numpy().tolist()

How to create 2 column binary numpy array from string list?

Using List comprehension

Code:

import numpy
lst = ['a', 'a', 'a', 'b', 'b', 'a', 'b']
numpy.array([[1,0] if val =="a" else [0,1]for val in lst])

Output:

array([[1, 0],
[1, 0],
[1, 0],
[0, 1],
[0, 1],
[1, 0],
[0, 1]])

Note:

  • Rather then appending to a list\numpy array, creating a list is faster

V-if inside v-for - display list of items in two columns

What I would do is create a computed property dividing (or chunking) the items array into the appropriate number of columns.

Here's an example that uses a flexbox layout and one extra column element.

new Vue({  el: 'main',  data: {    items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6', 'Item 7'],    cols: 2  },  computed: {    columns () {      let columns = []      let mid = Math.ceil(this.items.length / this.cols)      for (let col = 0; col < this.cols; col++) {        columns.push(this.items.slice(col * mid, col * mid + mid))      }      return columns    }  }})
.container {  display: flex;  border: 1px solid;}.col {  margin: 10px;  border: 1px solid;  flex-grow: 1;  display: flex;  flex-direction: column;}.item-container {  border: 1px solid;  padding: 5px;  margin: 5px;}
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.min.js"></script><main><p><label>Columns:<label> <input type="number" v-model="cols"></p><div class="container">  <div class="col" v-for="column in columns">    <div class="item-container" v-for="item in column">{{item}}</div>  </div></div></main>

How to change output of example data to 2 columns instead of 1

Slap something like this to break up the loop. Bit crappy if you ever want to use your keys for something other than indexes though. Could use a normal for loop. Or split the array.

<?php
foreach ($json_taps as $key => $beverage) {
...
if($key == 12){
echo "</div>"
echo "<div>"
}
...
?>

Creating a table from two given lists

Given a collection of numbers and a collection of strings, with the same number of elements, you could do something nasty like:

select a.column_value as num, b.column_value as str
from (
select rownum as rn, column_value
from table(sys.odcinumberlist(1,2,3,4,5,6))
) a
join (
select rownum as rn, column_value
from table(sys.odcivarchar2list('1.1','1.2','1.3','1.4','1.5','x.y'))
) b
on a.rn = b.rn;

NUM STR
---------- ------------------------------
1 1.1
2 1.2
3 1.3
4 1.4
5 1.5
6 x.y

This relies on the collection unnesting preserving the order of the elements in both table collecton expressions, so the rn values stay in sync; as far as I'm aware that isn't guaranteed but I haven't seen it not do that, for whatever that is worth. A more involved approach would be to use PL/SQL to process to two varrays into a single combine collection, but that rather complicates using the result in a query, unless you can create new schema-level objects and collection types. Or to do that processing on the Java side and populate a two-dimensional schema-level collection type directly.

Anyway, you can supply the array values from Java as shown here and elsewhere, via bind variables, making that query:

select a.column_value as num, b.column_value as str
from (
select rownum as rn, column_value
from table(?)
) a
join (
select rownum as rn, column_value
from table(?)
) b
on a.rn = b.rn;

And you can use that as an inline view, or a CTE, as part of a larger query.

Split a Pandas column of lists into multiple columns

You can use the DataFrame constructor with lists created by to_list:

import pandas as pd

d1 = {'teams': [['SF', 'NYG'],['SF', 'NYG'],['SF', 'NYG'],
['SF', 'NYG'],['SF', 'NYG'],['SF', 'NYG'],['SF', 'NYG']]}
df2 = pd.DataFrame(d1)
print (df2)
teams
0 [SF, NYG]
1 [SF, NYG]
2 [SF, NYG]
3 [SF, NYG]
4 [SF, NYG]
5 [SF, NYG]
6 [SF, NYG]


df2[['team1','team2']] = pd.DataFrame(df2.teams.tolist(), index= df2.index)
print (df2)
teams team1 team2
0 [SF, NYG] SF NYG
1 [SF, NYG] SF NYG
2 [SF, NYG] SF NYG
3 [SF, NYG] SF NYG
4 [SF, NYG] SF NYG
5 [SF, NYG] SF NYG
6 [SF, NYG] SF NYG

And for a new DataFrame:

df3 = pd.DataFrame(df2['teams'].to_list(), columns=['team1','team2'])
print (df3)
team1 team2
0 SF NYG
1 SF NYG
2 SF NYG
3 SF NYG
4 SF NYG
5 SF NYG
6 SF NYG

A solution with apply(pd.Series) is very slow:

#7k rows
df2 = pd.concat([df2]*1000).reset_index(drop=True)

In [121]: %timeit df2['teams'].apply(pd.Series)
1.79 s ± 52.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [122]: %timeit pd.DataFrame(df2['teams'].to_list(), columns=['team1','team2'])
1.63 ms ± 54.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)


Related Topics



Leave a reply



Submit