-2

I would like to store a list of elements based on the requirements like this:

  1. Loop the list and check each string
  2. If this string, then store the other strings within the list except the current string.
a = ["I","have","something","to","buy"]

When loop to "I" or "have" or "something" or "buy", then other elements will be stored inside the list except the current looped element. For example, we loop to "something" so "I", "have", "to", "buy" will be stored.

My code:

store = []
for x in a:
    if x:
        #I stuck here, I am really sorry, I know I should give more example,
        #but I really cant continue after here.

My expected output:

[["have","something","to","buy"], ["I","something","to","buy"], ["I","have","to","buy"], ["I","have","something","buy"], ["I","have","something","to"]]
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Beginner
  • 147
  • 8

6 Answers6

1
a = ["I","have","something","to","buy"]
store = []
for x in a:
    s = []
    for i in a:
        if i == x:
            continue
        else:
            s.append(i)
    store.append(s)
print(store)

Try this

Ninad Gaikwad
  • 4,272
  • 2
  • 13
  • 23
1

You are essentially looking for all combinations of 4 elements (without replacements) from list of 5 elements.

Use itertools.combinations:

from itertools import combinations

a = ["I", "have", "something", "to", "buy"]
print(list(combinations(a, 4)))
# [('I', 'have', 'something', 'to'), ('I', 'have', 'something', 'buy'),
#  ('I', 'have', 'to', 'buy'), ('I', 'something', 'to', 'buy'),
#  ('have', 'something', 'to', 'buy')]
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
1

since you're only checking words that are already in the list, you could just reduce the problem down to:

wordLists = [a[:w]+a[w+1:] for w in range(len(a))]

output:

[['have', 'something', 'to', 'buy'], ['I', 'something', 'to', 'buy'], ['I', 'have', 'to', 'buy'], ['I', 'have', 'something', 'buy'], ['I', 'have', 'something', 'to']]
Zinki
  • 446
  • 4
  • 10
1

Use itertools.combinations like this instead:

import itertools
a = ["I","have","something","to","buy"]

res = list(map(list, itertools.combinations(a, 4)))
print(res)             


# [['I', 'have', 'something', 'to'], ['I', 'have', 'something', 'buy'], ['I', 'have', 'to', 'buy'], ['I', 'something', 'to', 'buy'], ['have', 'something', 'to', 'buy']]                           

Note that itertools.combinations will generate tuples by default. Which I've converted to list type objects using map.

BlackBeard
  • 10,246
  • 7
  • 52
  • 62
1
a = ["I","have","something","to","buy"]

[a[:idx]+a[idx+1:] for idx, v in enumerate(a)]

Output

[["have","something","to","buy"], ["I","something","to","buy"], ["I","have","to","buy"], ["I","have","something","buy"], ["I","have","something","to"]]

Try this simple code

  • Can I ask another one more simple questions? How can I print something like this print("element_{}".format(*a)). I wanted to get output such as element_I, element_have, element_to, element_buy,element_something. by using one line like this? I do not wish to use for loop to loop the a one by one but to do it directly inside the .format(HERE) – Beginner Oct 10 '18 at 07:51
  • You can try map function like map("element_{}".format, a) and output will be ['element_I', 'element_have', 'element_something', 'element_to', 'element_buy'] – Anil Kumar Gupta Oct 10 '18 at 08:41
0

try this

for i in a[:]:
    tmp = a[:]
    a.remove(i)
    print(a)
    a = tmp
Ysh Xiong
  • 77
  • 4