Home > OS >  I have a list of list. Need to merge all the elements that is only "a" letters into a sing
I have a list of list. Need to merge all the elements that is only "a" letters into a sing

Time:12-03


 
x1 = ['a','1','2','b','4']
x2 = ['a','a','2','b','4']
x3 = ['a','a','a','b','4']
x4 = ['a','1','2','b','4']
xxxx = x1,x2,x3,x4
name2f = []

for i in xxxx:
    a1 = i[0]
    b1 = i[1]
    c1 = i[2]

    if  a1.isalpha:
        if  b1.isalpha:
            if  c1.isalpha:
                print("false 3")
                p = i[0] " " i[1] " " i[2]
                i.remove(a1)
                i.remove(b1)
                i.remove(c1)
                name2f.append(p)
    elif a1.isalpha:
        if b1.isalpha:
            p = i[0] " " i[1]
            i.remove(a1)
            i.remove(b1)
            name2f.append(p)
    elif  a1.isalpha:
        name2f.append(a1)
        i.remove(a1)
        print("false 1")
    else: print("broken")

isalpha and isdigit route does not seem to work nor does regex, not sure what is up. My results are print3 down the line. Not sure where the issue lies.

CodePudding user response:

Figured it out, went through the elements as a range and it worked:

 item = []
    for items in xxxx:
        for i in items[0:3]:
            if re.match(r'[A-Z]', i) and bool(re.search(r'[0-9]', i)) == False:
                item.append(i)
                items.remove(i)

    w = " ".join(item)
    print(w)
    print("")
    print(items)

CodePudding user response:

The first problem with code shown is isalpha() is a function. The function object isalpha is always non falsy.

Secondly, each of your if conditions check the exact same thing, so only the top one will run.

Also, your logic is only trying to generate something like name2f = ['a', 'aa', 'aaa', 'a'] since you only check first 3 elements, so the b strings are never looked at.


Break your code into two parts.

You can filter non-digits simply with

all_chars = [x for x in lst if not x.isdigit()]

Then, write a function to join all adjacent characters, such as

def collect_chars(lst):
  combined = lst[:1]
  result = []
  for val in lst[1:]:
    if val != combined[-1]:
      # current char not last seen char, dump the collected values so far
      result.append(''.join(combined))
      # and start a new string
      combined = [val]
    else:
      # keep adding matching strings
      combined.append(val)
  # if reached end of string, dump what has been collected so far
  result.append(''.join(combined))
  del combined

  return result

Then run both over the original list.

for i in xxxx:
  all_chars = [x for x in i if not x.isdigit()]
  lst = collect_chars(all_chars)
  print(lst)

Outputs

['a', 'b']
['aa', 'b']
['aaa', 'b']
['a', 'b']

CodePudding user response:

The if, elif, elif are all checking the same value, a1.isalpha:. That value will be True for each list per the code. It appears you mean to implement some and logic. if a1.isalpha and b1.isalpha and c1.isalpha:

x1 = ['a', '1', '2', 'b', '4']
x2 = ['a', 'a', '2', 'b', '4']
x3 = ['a', 'a', 'a', 'b', '4']
x4 = ['a', '1', '2', 'b', '4']
xxxx = x1, x2, x3, x4
name2f = []

for i in xxxx:
    a1 = i[0]
    b1 = i[1]
    c1 = i[2]

    if a1.isalpha and b1.isalpha and c1.isalpha:
        print("false 3")
        p = i[0]   " "   i[1]   " "   i[2]
        i.remove(a1)
        i.remove(b1)
        i.remove(c1)
        name2f.append(p)
    elif a1.isalpha and b1.isalpha:
            p = i[0]   " "   i[1]
            i.remove(a1)
            i.remove(b1)
            name2f.append(p)
    elif a1.isalpha:
        name2f.append(a1)
        i.remove(a1)
        print("false 1")
    else:
        print("broken")
  • Related