关于扁平化哈希的话题很多,但是我找不到关于我的情况的任何信息。
我有哈希表等的哈希表...
类似:
[:medical_address,:address_1, nil, :city, nil, :state, nil, :zip5, nil, :zip9, nil, :pharmacy_address, :address_1, nil, :city, nil, :state, nil, :zip5, nil, :zip9, nil,, :address,, :member_phone, "8000000000", :provider_phone, nil, :phone,"800-000-0000", "8000000000", "800-000-0000", "8000000000", "800-624-5060", "8006245060", "804-673-1678", "8046731678", "888-258-3432", "8882583432", "800-000-0000", "8000000000", "800-000-0000", "8000000000", "800-000-0000", "8000000000", "800-624-5060", "8006245060", "804-673-1678", "8046731678", "888-258-3432", "8882583432", :website,"www.fopblue.org", "www.fepblue.org", "www.fepblue.org", "www.fopblue.org"]
我想将其展平为单个数组。
有人方便地使用了一个很好的简单递归函数吗? 我希望结果看起来像这样:
# NODE consist of value and pointer to the next node
class node():
def __init__(self,x):
self.val=x
self.next=None
list1=node('m')
list1.next=node('a')
list1.next.next=node('d')
list1.next.next.next=node('a')
list1.next.next.next.next=node('m')
# head is declared as a global variable for checking purpose
# i and j will keep track of the linked list length
# for the odd number of nodes i==j
# for the even number of nodes i>j
# we will check until i>=j(EXIT CONDITION)
head=list1
i,j=0,0
def palindrome(list):
# base condition
if list is None:
return 0
# j variable will keep track of the nodes
global j
j+=1
x = palindrome(list.next)
#if we get a single FALSE then there is no need to check further
# we will return FALSE in each case
if x is False:
return False
global head,i
i+=1
j-=1
#EXIT CONDITION
if i>=j:
return True
#if the value is evaluated to be false then return false
if head.val is list.val:
head=head.next
return True
else:
return False
print(palindrome(list1))
答案 0 :(得分:1)
如果您适合使用宝石,则可以使用多功能Iteraptor
宝石。
input = [your structure]
input.iteraptor.flatten
# ⇒ {"medical_address.0.address_1"=>nil,
# "medical_address.0.city"=>nil,
# "medical_address.0.state"=>nil,
# "medical_address.0.zip5"=>nil,
# "medical_address.0.zip9"=>nil,
# "pharmacy_address.0.address_1"=>nil,
# "pharmacy_address.0.city"=>nil,
# "pharmacy_address.0.state"=>nil,
# "pharmacy_address.0.zip5"=>nil,
# "pharmacy_address.0.zip9"=>nil,
# "member_phone"=>"8000000000",
# "provider_phone"=>nil,
# "phone.0"=>"800-000-0000",
# "phone.1"=>"8000000000",
# "phone.2"=>"800-000-0000",
# "phone.3"=>"8000000000",
# "phone.4"=>"800-624-5060",
# "phone.5"=>"8006245060",
# "phone.6"=>"804-673-1678",
# "phone.7"=>"8046731678",
# "phone.8"=>"888-258-3432",
# "phone.9"=>"8882583432",
# "phone.10"=>"800-000-0000",
# "phone.11"=>"8000000000",
# "phone.12"=>"800-000-0000",
# "phone.13"=>"8000000000",
# "phone.14"=>"800-000-0000",
# "phone.15"=>"8000000000",
# "phone.16"=>"800-624-5060",
# "phone.17"=>"8006245060",
# "phone.18"=>"804-673-1678",
# "phone.19"=>"8046731678",
# "phone.20"=>"888-258-3432",
# "phone.21"=>"8882583432",
# "website.0"=>"www.fopblue.org",
# "website.1"=>"www.fepblue.org",
# "website.2"=>"www.fepblue.org",
# "website.3"=>"www.fopblue.org"}
请注意,拼合是可逆的,result.recoger
将为您返回哈希值。