Python Forum
Working with lists homework
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working with lists homework
#1
Define 2 lists. The first one must contain the integer values 1, 2 and 3 and the second one the string values a, b and c. Iterate through both lists to create another list that contains all the combinations of the A and B elements. The final list should look like one of the 2 lists:
1. [1a, 1b, 1c, 2a, 2b, 2c, 3a, 3b, 3c]
2. [a1, a2. a3, b1, b2, b3, c1, c2, c3]
BONUS: Make the final list contain all possible combinations : [1a, a1, 1b, b1, 1c, c1, 2a, a2, 2b, b2, 2c, c2, 3a, a3, 3b, b3, 3c, c3]

Help me !
Reply
#2
This is not how it works. We are not going to do your homework for you. You need to make effort, post your code in python tags, if you get error, post full traceback in error tags and ask specific questions.
Reply
#3
Also,in the future make your thread titles more descriptive.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
No problem, friend!
>>> import itertools
>>> a = 'abc'
>>> b = '123'
>>> items = list(map("".join, itertools.product(a, b)))
>>> items
['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3']
>>> bonus = items + [item[::-1] for item in items]
>>> bonus
['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3', '1a', '2a', '3a', '1b', '2b', '3b', '1c', '2c', '3c']
Reply
#5
(Mar-29-2018, 03:18 PM)nilamo Wrote: No problem, friend!
>>> import itertools
>>> a = 'abc'
>>> b = '123'
>>> items = list(map("".join, itertools.product(a, b)))
>>> items
['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3']
>>> bonus = items + [item[::-1] for item in items]
>>> bonus
['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3', '1a', '2a', '3a', '1b', '2b', '3b', '1c', '2c', '3c']

Define two lists and the first one must contain integers. Join will fail
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
Do you know how to define a list?
if not then here:
list_entry = ["Here ", "is ", "How ", "We", "Make ", "A ", "List"] #Defines a list.

for i in range(0, len(list_entry)):
    print(list_entry[i]) #Prints the list.
P.S. it does not want to make this look like code...

Paste this into your IDE and run it to see how it works.
Reply
#7
(Apr-04-2018, 05:12 AM)groovydingo Wrote:
for i in range(0, len(list_entry)):
    print(list_entry[i]) #Prints the list.

OP, and others finding this later, please don't do this. It's not pythonic, and is actively bad practice for iterating over lists. Just iterate over them directly, ie:
for entry in list_entry:
    print(entry)
Reply
#8
Great Thank เกม สล็อต
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Homework on addition with working paperplanexx 1 2,486 Aug-19-2018, 12:40 PM
Last Post: j.crater
  Working with lists homework (2) codellino 7 4,482 Feb-28-2018, 11:23 AM
Last Post: buran
  Lists Homework kekev1997 1 2,587 Nov-19-2017, 03:07 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020