python - How to find repeating individual strings in a list? -
I tried this solution: but it was not working from my list format. Was showing the wrong answer. I used the same code and I used to store all the lists with a key:
common_suffix = os.path.commonprefix (for list item [list [ - [1 -]] [[- - 1] stripped_titles = [item [: - for items in the list] [transactions] Print lain (stripped_titles)
The answer I get 0.
These are the lists I have already created these lists with the CSV file data:
list1 = ['a1', 'b2', 'c4 ',' Y7 ',' u5 '] list2 = [' b4 ['b2', 'a 1 '] list 4 = [' B2 ',' A1 '] list 5 = [' R4 ',' C4 ',', 'T5' ',' G1]] 3 = ['B2 Here I want to search for the repeated string, say, I have three elements: I want to take a1, b2, c4
(from list 1), I have to find out how many times these three strings are in other lists. The order does not matter, string items should be in other lists In this case, it exists in a1, b2, c4
list 1, list 3, and list5. So I want to answer that "first three elements of list 1 first repeat 1 3 twice". How can this be done with the following attribute: I want to be able to choose a number, say that k = 3
, then the first three elements of all the lists will be Tested for repetitive patterns and output will be done,
a1, b2, c4 = 3
and so on
If I understand what you are doing (count each list where all the items are included earlier), you do so Can have :
lists = [['a1', 'b2', 'c4', 'y7', 'u5'], ['b4', 't5' 'g1' ], ['B2', 'C4', 'F6', 'A1'], ['B2', 'A1'], ['R4', 'C4', 'A1 ',' B2 ']] K = 3 tgt = tuple (lists [0] [0: k]) li = {tgt: 1} in lists for li [1:]: if all (for li in E E in TGT): di [tgt] + = 1 print (d)
print:
{('a1', 'b2', ' C4 '): 3}
If you want to simplify in a line:
di = {tgt: sum (all in li For the TGT in the list) li)}
Then if you want your exact output:
gt; & Gt; Print '{} = {}'. Format (',' .join (tgt), di [tgt]) a1, b2, c4 = 3
Comments
Post a Comment