LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2020, 01:45 PM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 531

Rep: Reputation: 74
combine list in python3


This is not a typical combination.

Start with two list of different length were the second list is always double the length of the first list.

Example:
list1 = ["a", "b", "c", "d"]
list2 = [0, 1, 2, 3, 4, 5, 6, 7]

The result I am looking for is:
listR = ["a", 0, "a", 1 "b", 2, "b", 3 "c", 4, "c", 5 "d", 6, "d", 7]

I got this to work in python2:

Code:
list1 = ["a", "b", "c", "d"]
list2 = [0, 1, 2, 3, 4, 5, 6, 7]
listR = []

for index2 in range(len(list2)):
  index1 = int(round(index2/2))
  listR.append(list1[index1])
  listR.append(list2[index2])

print(listR)
The result of the print statement is exactly what I'm looking for. But in python3 the index for list1 goes out of range because the rounding is different.

If I print just the index1 in python2 I see 0 0 1 1 2 2 3 3 but in python3 I get 0 0 1 2 2 2 3 4.

There are ways to work around this to accomplish my goal but besides the rounding issue I don't like the brute force nature of the code as is and was wondering if a more elegant python solution was available.

Thanks to all in advance.
 
Old 05-28-2020, 01:58 PM   #2
sharky
Member
 
Registered: Oct 2002
Posts: 531

Original Poster
Rep: Reputation: 74
I think I got it.

Modified the calculation of index1.

Old: index1 = int(round(index2/2))
New: index1 = int(round(index2/2,2))
 
Old 05-28-2020, 02:04 PM   #3
shruggy
Member
 
Registered: Mar 2020
Posts: 994

Rep: Reputation: Disabled
Quote:
Originally Posted by sharky View Post
But in python3 the index for list1 goes out of range because the rounding is different.
Perhaps floor division will help?
Code:
index2//2
Quote:
Originally Posted by sharky View Post
was wondering if a more elegant python solution was available
Have you considered using list comprehension?
Code:
[i for t in zip([i for t in zip(list1,list1) for i in t],list2) for i in t]

Last edited by shruggy; 05-28-2020 at 03:00 PM.
 
Old 05-28-2020, 02:51 PM   #4
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 305

Rep: Reputation: Disabled
You could invert the logic:
Code:
l1 = ["a", "b", "c", "d"]
l2 = [0,1,2,3,4,5,6,7,]
lr = []

for i in range(len(l1)):
    lr.append (l1[i])
    lr.append (l2[i * 2])
    lr.append (l1[i])
    lr.append (l2[i * 2 + 1])
 
Old 05-28-2020, 03:32 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,605

Rep: Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279
I see nothing wrong with your approach, apart from the division and rounding. The operation you want is:

Code:
>>> 5 // 2
2
Anyway, here's my implementation, which isn't as cool as shruggy's list comprehension:

Code:
list1 = ["a", "b", "c", "d"]
list2 = [0, 1, 2, 3, 4, 5, 6, 7]
listR = []

offset = 0
for i in range(len(list1)):
    for j in range(2):
        listR.append(list1[i])
        listR.append(list2[offset])
        offset += 1

print(listR)

Last edited by dugan; 05-28-2020 at 03:33 PM.
 
Old 05-28-2020, 05:02 PM   #6
shruggy
Member
 
Registered: Mar 2020
Posts: 994

Rep: Reputation: Disabled
@dugan, SoftSprocket. Let's combine your solutions into one:
Code:
list1 = ["a", "b", "c", "d"]
list2 = [0, 1, 2, 3, 4, 5, 6, 7]
listR = []

for i in range(len(list1)):
    for j in (0, 1):
        listR.append(list1[i])
        listR.append(list2[i*2 + j])

print(listR)
@OP. So, now you have three solutions. The one above. Then yours:
Code:
list1 = ["a", "b", "c", "d"]
list2 = [0, 1, 2, 3, 4, 5, 6, 7]
listR = []

for i in range(len(list2)):
    listR.append(list1[i//2])
    listR.append(list2[i])

print(listR)
And mine:
Code:
list1 = ["a", "b", "c", "d"]
list2 = [0, 1, 2, 3, 4, 5, 6, 7]

def combine(list1, list2):
    return [item for pair in zip(list1, list2) for item in pair]

listR = combine(combine(list1, list1), list2)
print(listR)
They all look OK to me. Choose what fits you best.

Last edited by shruggy; 05-29-2020 at 02:14 AM.
 
Old 06-01-2020, 05:16 PM   #7
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: Currently: openSUSE, Raspbian, Slackware. Formerly: CentOS, MacOS, Red Hat. Other: Solaris, Tru64
Posts: 1,941

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by dugan View Post
...

Anyway, here's my implementation, which isn't as cool as shruggy's list comprehension:

...
"Cool" or "maintainable by mere mortals": pick one.

One-liners are impressive but sometimes you gotta just keep it simple.
 
  


Reply

Tags
python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python3 -V returns Python 2.7.12, can't get python3 to work. gene_ Ubuntu 15 03-18-2019 10:39 AM
[SOLVED] making python3.6.4 default python3 Astral Axiom Linux - Newbie 17 04-14-2018 10:55 AM
[SOLVED] how to start python3.6 interpreter just by typing python in terminal not python3.6 bmohanraj91 Linux - Newbie 4 05-10-2017 07:51 AM
After upgrade python3.4 to python3.5.1 , not able to install packages "request" though pip3 YOGESHAS87 Linux - Software 1 08-03-2016 10:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration