Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List joining
#11
(Dec-15-2022, 04:16 PM)deanhystad Wrote:
from itertools import cycle
baskets = []
items = []
  
while basket := input("Basket: "):
    baskets.append([basket])
  
while item := input("Items: "):
    items.append(item)
  
for basket, item in zip(cycle(baskets), items):
    basket.append(item)
  
print(baskets)
Output:
Basket: A Basket: B Basket: Items: 1 Items: 2 Items: 3 Items: 4 Items: 5 Items: [['A', '1', '3', '5'], ['B', '2', '4']]

That of course works great!! Thank you, now I have to study more on zip and cycle to see why it works...

Thanks again for your help and patience
DeatZ
Reply
#12
(Dec-05-2022, 07:50 PM)deatz Wrote: Example
Task list: a, b, c
People list: John, Joe, Karen, Billy, Bob, Lisa, Derek

Desired Output: a: John, Billy, Derek
b: Joe, Bob
c: Karen, Lisa


Is this True?:
  • Pop the first and last item from the list
  • if the remaining list has an odd length, pop the middle part.
  • Repeat this, until the list is empty

I think you swapped accidentally Bob with Lisa.
I solved it without itertools. Instead, I used the pop method on the list.

my_list = ["first", "middle", "last"]

first = my_list.pop(0) # pop element from index 0
last = my_list.pop(-1) # pop element from index -1, which is the last element
To find out, if a list has odd or even elements inside, you should use the modulo % operator.

value = 2

if value % 2 == 0:
    print("value % 2 == rest is 0. Even elements")
else:
    print("value % 2 == non-zero. Odd elements")
I got this solution:
Output:
{'a': ('John', 'Billy', 'Derek'), 'b': ('Joe', 'Lisa'), 'c': ('Karen', 'Bob')}
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#13
(Dec-15-2022, 04:16 PM)deanhystad Wrote:
from itertools import cycle
baskets = []
items = []
  
while basket := input("Basket: "):
    baskets.append([basket])
  
while item := input("Items: "):
    items.append(item)
  
for basket, item in zip(cycle(baskets), items):
    basket.append(item)
  
print(baskets)
Output:
Basket: A Basket: B Basket: Items: 1 Items: 2 Items: 3 Items: 4 Items: 5 Items: [['A', '1', '3', '5'], ['B', '2', '4']]

So, I know I'm missing a point, like when the baskets/items are entered they are committed to memory somewhere as basket and item which is why when I substitute an actual list, I get a memory location output (I think anyway) or an attribute error. I can't seem to put together a combination of variables to make it work.

from itertools import cycle

baskets = [
    'Hangar',
    'Trash',
    'Sweep',
    'Mop',
    'Labs'
]

items = [
    'John',
     'Kat',
     'Emile',
     'Noble 6',
     'Echo 216',
     'Billy',
     'Lenny',
     'Larry',
     'Sammi',
     'Ray-Ray',
     'Angel',
     'Kashaa',
     'Bri',
     'Amy',
     'Kipper',
     'Bernard'
]


# baskets = []
# items = []

# while basket := input("Basket: "):
#     baskets.append([basket])
#
# while item := input("Items: "):
#     items.append(item)

for basket, item in zip(cycle(baskets), items):
    basket.append(item)

print(baskets)
Error:
Traceback (most recent call last): File "D:\Dropbox\Programming\Python\venv\AnotherItertoolTest.py", line 31, in <module> task.append(name) AttributeError: 'str' object has no attribute 'append'
I seem to have trouble digesting what's going on behind the curtain.
TIA
Reply
#14
baskets in my example is a list of lists. In your example it is a list of strings.
Reply
#15
(Dec-20-2022, 11:40 PM)deanhystad Wrote: baskets in my example is a list of lists. In your example it is a list of strings.

OHHH .... MY .... GOSH!!!!

So freaking simple!

Thank you so much for all your patience with me on this!

You've restored my faith that there are good peeps in the forums, not just rude jerks!

Be blessed!
DeatZ
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using a function: splitting, joining, and slicing a string Drone4four 2 4,782 Dec-27-2018, 07:52 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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