How can I get only lower case words in one list and upper case words in one list , from a list using python

huangapple go评论72阅读模式
英文:

How can I get only lower case words in one list and upper case words in one list , from a list using python

问题

我使用的代码:

import re
import pandas as pd
import csv

list = ['ArvindBholaGujarat...creativedirectorSubhashGhai...director']

items = []
for i in list:
  item = i.split('...')
  items.append(item)
items

cap = []
small = []
for i in items:
   caps = re.findall('[A-Z][^A-Z]*', str(i))
   low = re.findall('[a-z][^a-z]*', str(i))
   cap.append(caps)
   print(caps)
lower_words = []
for i in cap:
   if str(i) == str(i).islower():
      lower_words.append(i)

我从代码中期望的输出是:
列表:
ArvindBholaGujarati creativedirector
SubhashGhai director

所以基本上,这里识别的模式是具有大写字母的单词放在一个列表中,而另一个列表包含所有小写字母的单词。

这就是我想要实现的内容。我已经用Python编写了所有这些代码,如果社区成员能提供帮助,我将不胜感激。

英文:

The code I have used:

import re
import pandas as pd
import csv

list=['ArvindBholaGujarat...creativedirectorSubhashGhai...director']

items=[]
for i in list:
  item=i.split('...')
  items.append(item)
items

cap=[]
small=[]
for i in items:
   caps=re.findall('[A-Z][^A-Z]*',str(i))
   low=re.findall('[a-z][^a-z]*',str(i))
   cap.append(caps)
   print(caps)
lower_words=[]
for i in cap:
   if str(i)==str(i).islower():
      lower_words.append(i)

I have tried using the split() in python and using the islower() and isupper() in python but the required data is not coming.

**The output I expect from the code is:
List:
ArvindBholaGujarati creativedirector
SubhashGhai director
**

So basically, the pattern identified here is words with captial letters are put in one list whereas the other list contains all the lower lettered words.

This is what I would like to achieve.
I have written all this code in python and would like to get the help needed if any community member can help.

答案1

得分: 1

import re
import pandas as pd

names = 'ArvindBholaGujarat...creativedirectorSubhashGhai...director'

items = names.split('...')
print(items)

cap = []
small = []
for i in items:
caps = re.findall('[A-Z][a-z]*', str(i))
if caps:
cap.extend(caps)

low = re.findall('^[a-z]+', str(i))
if low:
    small.extend(low)

print(f'大写开头的名字有 {cap},小写的单词有 {small}')

英文:

I think this is what you're looking for

import re
import pandas as pd

names = 'ArvindBholaGujarat...creativedirectorSubhashGhai...director'

items = names.split('...')
print(items)

cap = []
small = []
for i in items:
	caps = re.findall('[A-Z][a-z]*', str(i))
	if caps:
		cap.extend(caps)

	low = re.findall('^[a-z]+', str(i))
	if low:
		small.extend(low)
	
print(f'Caps are {cap}, and lowers are {small}')

Output:

['ArvindBholaGujarat', 'creativedirectorSubhashGhai', 'director']
Caps are ['Arvind', 'Bhola', 'Gujarat', 'Subhash', 'Ghai'], and lowers are ['creativedirector', 'director']

In caps list, there are names starting with capital letters and in lowers words without any capital letters.

答案2

得分: 0

你可以使用re.findall和交替模式,首先是完全小写的单词,然后是以大写字母开头的单词(用管道操作符连接)。然后,你可以使用输出单词的第一个字母来确定要将其添加到哪个列表中,具体取决于它是大写字母单词还是全小写单词。

import re

data = ['ArvindBholaGujarat...creativedirectorSubhashGhai...director']
cap, small = lists = [[], []]
for item in data:
    for word in re.findall(r'[a-z]+|[A-Z]\w*', item):
        lists[word[:1].islower()].append(word)

输出:

print(small)
['creativedirector', 'director']
print(cap)
['ArvindBholaGujarat', 'SubhashGhai']
英文:

You could use re.findall with alternate patterns, first a fully lowercase word and then a word starting with uppercase letter (joined with a pipe operator). You can then use the first letter of the output word to determine which list to add it to depending on whether it is a capitalized word or all lowercase.

import re

data      = ['ArvindBholaGujarat...creativedirectorSubhashGhai...director']
cap,small = lists = [[],[]]
for item in data:
    for word in re.findall(r'[a-z]+|[A-Z]\w*',item):
        lists[word[:1].islower()].append(word)

output:

print(small)
['creativedirector', 'director']
print(cap)    
['ArvindBholaGujarat', 'SubhashGhai']

huangapple
  • 本文由 发表于 2023年7月10日 14:20:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定