0

I'm rather new at this (it's my first question ha!), and my english isn't as good and technical as yours so I apologize in advance. That being said, I'm trying to fit string variables to an array but I can't get it done. Here are the variables:

basket1 = {'vanilla wafers', 'bananas' , 'dog food'}
basket2 = {'bananas', 'bread', 'yogurt'}
basket3 = {'bananas','apples','yogurt'}
basket4 = {'vanilla wafers','bananas','whipped cream'}
basket5 = {'bread', 'vanilla wafers' , 'yogurt'}
basket6 = {'milk', 'bread', 'bananas'}
basket7 = {'vanilla wafers', 'apples' , 'bananas'}
basket8 = {'yogurt', 'apples', 'vanilla wafers'}
basket9 = {'vanilla wafers', 'bananas' , 'milk'}
basket10 =  {'bananas', 'bread', 'peanut butter'}

Any ideas? Should I use a list instead? It must have a 10x3 shape though.

2
  • You're right, you (and everyone else), have no need to apologize for their english. Do your best (as you've done) and hope for fair treatment. Anyone not giving fair treatment is failing in their compassion, and that isn't your fault. Commented Feb 17, 2020 at 3:53
  • Note: what you’re creating there are sets, not lists or arrays. In Python, a set is an unordered collection of items where each item can only appear once (so {'apples', 'bananas', 'bananas'} is the same as {'bananas', 'apples'}). Use [] to create a list instead (which is equivalent to an array in other languages). Commented Feb 17, 2020 at 3:55

2 Answers 2

2

Replace the brackets with parentheses, so like this

basket1 = ['bread', 'cake', 'soap']

For multi-dimensional arrays, write

baskets = [['bread', 'cake', 'soap'], ['carrot', 'milk', 'wheat']]
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I was thinking more of a loop thing; I have a lot of those variables, all starting with 'basket' and they're consecutive. To be more specific, final array should be of nx3 shape.
0

You could use declare a baskets list and iterate over a for loop to append values to the end of the list.

You can modify the temp list and fill it with values accordingly each time.

baskets = list()

for i in range(n):
   temp = [value1,value2,value3]
   baskets.append(temp)

print(baskets)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.