Thank you for browsing. There is a stumbling block in the Python array, and I would appreciate it if you could teach me.
lst = ['Shiritori. Apple. gorilla. ','Apple. gorilla. ','gorilla. ',' Cat. Dog. Rabbits. ','Rabbits. ']
If there is such an array, I would like to get the shortest element with partial matching of elements in the following array.
lst = ['Gorilla. ','Rabbits. ']
I'm sorry I couldn't explain it well. Thank you.The problem i am having
I researched it myself and tried to program it, but I couldn't get the result I wanted.
I think there is a problem with the way of thinking and approach.
I don't know if it will be helpful, but I will list the source code.
Sorry for the unsightly source code.
lst1 =  lst2 = ['Shiritori. Apple. gorilla. ','Apple. gorilla. ','gorilla. ',' Cat. Dog. Rabbits. ','Rabbits. '] lst2.insert (0,'') for i in range (1, len (lst2) -1): if (lst2 [i] not in lst2 [i-1]): lst1.append (lst2 [i]) list = set (lst2) --set (lst1) l = [x for x in list if x] print (l) >>['Rabbit. ', 'Apple. gorilla. ',' Gorilla. ']
Answer # 1
What do you want to do?
lst2 = ['Shiritori. Apple. gorilla. ','Apple. gorilla. ','gorilla. ',' Cat. Dog. Rabbits. ','Rabbits. '] lst1 = [s for i, s in enumerate (lst2) for t in lst2 [: i] + lst2 [i + 1:] if s in t] lst1 = list (set (lst1)) lst3 = [s for i, s in enumerate (lst1) for t in lst1 [: i] + lst1 [i + 1:] if t in s] for s in lst3: lst1.remove (s) print (lst1) # ['Gorilla. ','Rabbits. ']
list = set (lst2) --set (lst1)
If you use the variable name list, you will not be able to use list as a class, so stop it.If you want to extract an element that does not contain other elements
(You can use it if you del, but it is difficult to understand)
I also feel that the name of list in the object of set is confusing.
lst2 = ['Shiritori. Apple. gorilla. ','Apple. gorilla. ','gorilla. ',' Cat. Dog. Rabbits. ','Rabbits. ','bird. monkey. pheasant. '] lst1 = lst2.copy () lst3 = [t for i, s in enumerate (lst2) for t in lst1 [: i] + lst1 [i + 1:] if s in t] lst3 = list (set (lst3)) for s in lst3: lst1.remove (s) print (lst1) # ['Gorilla. ','Rabbits. ','bird. monkey. pheasant. ']
- python - [beginner] i want to delete line breaks/n after reading and outputting a file
- i want to see the change of one element of a recurrence formula including a python matrix
- adding an element to a particular line in a python 2d list
- python - delete bom on the command line
- python - count by element with pivot_table in pandas
- python - i want to add an element to a double list
- python - i want to retrieve the element above the element searched by soupfind_all in beautiful soup
- python - how to delete an apostrophe in the list
- python - i can't get the element with selenium
- python3 extract and delete duplicate data of date and time
- python - [ajax] i want to get and send the value of the button element in the form
- python - how to delete vertices that meet the conditions in a graph using networkx
- python - the table element generated by the django template variable is output at the bottom of the page
- i want to delete a specific word from a string using a regular expression python
- python - regarding image template matching that is robust against noise and scale conversion
- i want to input an element and extract one element using the python3 library openpyxl
- how to multiply each element by a number in a python 2d list
- [python] cannot insert or delete to text box
- python - i want to assign an element of one list to an element of another list
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- i want to call a child component method from a parent in vuejs
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- xcode - pod install [!] no `podfile 'found in the project directory