Posts: 1
Threads: 1
Joined: Feb 2018
Feb-26-2018, 02:00 AM
(This post was last modified: Feb-26-2018, 01:51 PM by sparkz_alot.)
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 !
Posts: 8,160
Threads: 160
Joined: Sep 2016
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.
Posts: 1,298
Threads: 38
Joined: Sep 2016
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
Posts: 3,458
Threads: 101
Joined: Sep 2016
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']
Posts: 2,953
Threads: 48
Joined: Sep 2016
(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
Posts: 7
Threads: 3
Joined: Mar 2018
Apr-04-2018, 05:12 AM
(This post was last modified: Apr-04-2018, 05:12 AM by groovydingo.)
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.
Posts: 3,458
Threads: 101
Joined: Sep 2016
(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)
Posts: 7
Threads: 0
Joined: Mar 2018
|