How do I split a list of numbers into a phone number in python?
The solution for your problem is pretty simple:
a = '8005551212'
print(f'{a[:3]}-{a[3:6]}-{a[6:]}')
Splitting a Phone Number into a List of Digits: Python
Iterate over string.
Ex:
unformattedPhone = "123 456-7890"
numbersList = [int(s) for s in unformattedPhone if s.isdigit()]
print(numbersList)
Output"
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
How to split name and phone number in a list in python?
If all phone numbers contain only digits and dashes, you can use:
import re
rgx = re.compile(r'\s+(?=[\d-]+$)')
result = [rgx.split(x,1) for x in input]
Where input
thus is your list of input strings. It generates the following list:
>>> [rgx.split(x,1) for x in input]
[['Jackson, Janet', '313-1352'], ['James, Lebron', '457-6221'], ['Manfredi, Ralph', '872-2221'], ['Prakash, Varun', '312-5398']]
But as said before, it only works properly given the above stated assumption. Best thing is, if the phone number contains spaces, then it can still work:
import re
rgx = re.compile(r'\s+(?=[\d\s-]+$)')
result = [rgx.split(x,1) for x in input]
For input with spaces in the phone number, it gives:
>>> input = ['Jackson, Janet 313-13 52', 'James, Lebron 457-6 22 1', 'Manfredi, Ralph 872-222 1', 'Prakash, Varun 312-5 3 9 8']
>>> [rgx.split(x,1) for x in input]
[['Jackson, Janet', '313-13 52'], ['James, Lebron', '457-6 22 1'], ['Manfredi, Ralph', '872-222 1'], ['Prakash, Varun', '312-5 3 9 8']]
python - Splitting a list of integers into list of digits
This is one way:
qc = [11221427, 23414732, 144443277]
lst = [list(map(int, i)) for i in zip(*map(str, qc))]
# [[1, 2, 1],
# [1, 3, 4],
# [2, 4, 4],
# [2, 1, 4],
# [1, 4, 4],
# [4, 7, 3],
# [2, 3, 2],
# [7, 2, 7]]
If you really need these as separate variables, either use lst[idx]
or a dictionary {i: j for i, j in enumerate(lst, 1)}
.
Given a phone number in words, convert the number to digits
Create another mapping for the quantity modifiers, and multiply the digit string by the preceding modifier.
digits = {
"zero": "0",
"one": "1",
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
"seven": "7",
"eight": "8",
"nine": "9",
}
modifiers = {
"double": 2,
"triple": 3,
}
number = ""
n = 1
for word in input("? ").split():
if word in modifiers:
n *= modifiers[word]
else:
number += n * digits[word]
n = 1
print(number)
? five eight double two double two four eight five six
5822224856
How to remove the "-" from phone numbers and choose the phone numbers from a list of strings?
Pandas series has .str
which has function .split()
to split the string and .join()
to join a list. You can first split the contact using .split('-')
and then join it using .join('')
.
To get separate dataframes for contact and name, create a mask which is True
for rows which are contact. .str.contains(pattern)
is a function to check if regex pattern
is present in string or not. For matching the contact, we can check if row only consists of digit or not, to match row with only digit, pattern is: ^[0-9]+$
.
After creating mask use df[mask]
and df[~mask]
to get the dataframes.
Use:
df['contact'] = df.contact.str.split("-").str.join('')
mask = df.contact.str.contains("^[0-9]+$")
df_contact = df[mask]
df_name = df[~mask]
Output:
>>> df_contact
contact
0 1235463454
3 2458703433
4 7898624860
>>> df_name
contact
1 Sarah
2 Cyrus
5 Juan
Related Topics
How to Count the Number of Messages
How to Get Max Output from a While Loop
Discord Bot Messaging a User With a Specific User Id
Pandas Dataframe Calculations With Previous Row
How to Convert Data from Txt Files to Excel Files Using Python
How to Write List Elements into a Tab-Separated File
Possible to Loop Through Excel Files With Differently Named Sheets, and Import into a List
How to Retrieve SQL Result Column Value Using Column Name in Python
How to Bold Text in Telepot Telegram Bot
How to Mention a User in Discord.Py
How to Do a Conditional Count After Groupby on a Pandas Dataframe
How to Delete the Words Between Two Delimiters
Checking If Particular Value (In Cell) Is Nan in Pandas Dataframe Not Working Using Ix or Iloc
Use Variable as Key Name in Python Dictionary
How to Determine Whether a Pandas Column Contains a Particular Value