Comparison between string characters within a list [duplicate]

2024/11/24 9:02:28

Having a Python list, containing same length strings, like the following one:

input_list = [ "abc", "def", "ghi" ]

How can I compare character by character all the strings and do the difference between them? Each string has to compare the other once.

list[0] with list[1]

list[0] with list[2]

list[1] with list[2]

Example of a comparison:

"a" with "d"
"b" with "e"
"c" with "f" 

The number of string-type elements in this list may change, but the length of the strings will always be the same.

I have been struggling a lot trying to find a way to do this by either turning each element into a sub-list so I could compare them but the comparison would always give me errors in the loops such as "index out of range".

I also tried using dictionaries:

for i in range(len(list1)):n_dict["Player%s" %i] = list(list1[I])

This would give me the following output:

{'Player0': ['a', 'b', 'c'],'Player1': ['d', 'e', 'f'],'Player2': ['g', 'h', 'i']}

but then again, comparing was even more complicated with loops. I also tried by simply using list indexes such as

for i in range(len(list1):for j in range(len(list1):if ord(list[i][j]) - ord(list[i][j]) < k:player0 += 1else:player1 +=1

but always index out of range. Can someone please help me out?

Answer
l = ["abc","def","abc","def","abc"]i1 = 0
i2 = 1
for i in l[:-1]:for j in l[i2:]:while len(i) > i1:print(i[i1]==j[i1])i1 += 1i1 = 0i2 += 1

You can compare strings character by character but don't get it what do you mean by do the difference ?

https://en.xdnf.cn/q/120553.html

Related Q&A

Pyspark filling missing dates by group and filling previous values

Spark version 3.0. I have two dataframes. I create one dataframe with date columns using pandas date range. I have a 2nd spark dataframe contains the company name, dates and value. I want to merge the …

How to loop in the opposite order?

I am a beginner programmer. Here is my code:n = int(input()) from math import* for i in range(n):print(n, "\t", log10(n))i = i + 1n = n - 1Its output is:10 1.0 9 0.9542425094393249 8 …

Override methods with same name in Python programming [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicate: How do I use method overloading in Python?I am new to Python programming, and I like to write multiple methods with the …

TypeError: function object is not subscriptable in Python 3.4.3?

I have a food menu and the stock and prices are in separate dictionaries.Food Stock:Food_Stock = {Chips : 15,Bagels : 27,Cookies : 25}#Food Stock.Food Prices:Food_Prices = {#Food Prices.Chips : 1,Bagel…

Insert a value in date format dd-mm-yyyy in dictionary in python

I am creating a dictionary having some values including a date of birth of a person. But when I run my code, it is giving an error "datetime.datetime has no attribute datetime" . Here is my …

Collisions arent registering with Python Turtles

Im creating a simple 2D shooter following an online tutorial, and the enemy sprites (except 1, there are 5 total) are not abiding by my collision code. Everything works except the bullet object that th…

Function should clean data to half the size, instead it enlarges it by an order of magnitude

This has been driving me nuts all week weekend. I am trying merge data for different assets around a common timestamp. Each assets data is a value in dictionary. The data of interest is stored in lists…

is it possible to add colors to python output? [duplicate]

This question already has answers here:How do I print colored text to the terminal?(66 answers)Closed 10 years ago.so i made a small password strength tester for me, my friends and my family, as seen …

Tkinter Function attached to Button executed immediately [duplicate]

This question already has answers here:How can I pass arguments to Tkinter buttons callback command?(2 answers)Closed 8 years ago.What I need is to attach a function to a button that is called with a …

Error!!! cant concatenate the tuple to non float

stack = []closed = []currNode = problem.getStartState()stack.append(currNode)while (len(stack) != 0):node = stack.pop()if problem.isGoalState(node):print "true"closed.append(node)else:child =…